p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   passing parameter to a perl subroutine (http://p2p.wrox.com/showthread.php?t=50425)

crmpicco November 17th, 2006 08:37 AM

passing parameter to a perl subroutine
 
This is my script, i am having problems with the line
Code:

$resortid = $func->getresortid($resortname);
- i am trying to write out the resortid to the screen, with no luck however.

Code:

#!/usr/bin/perl

# Craig R Morton
# Last_Edit: 16-Nov-2006

use ready::Sql;
use ready::DataInterface::Locations;

$sql = new ready::Sql;
$newsql = new ready::Sql;
$func = new ready::DataInterfaces::Locations;

open(IN, "/usr/local/search/currenttables");
while(<IN>) {
    chomp;
    (undef, $table) = split(/:/);
    if ($table =~ /^package/) {
        push(@tables, $table);
    }
}
close(IN);

@cursor = $sql->query(q{SELECT DISTINCT(name) FROM `location`.`resorts`});
while ($returned = $cursor[0]->fetchrow_hashref) {

    $resortname = $returned->{name};
    $resortname =~ s/'/\\\'/;       
    $resortid = $func->getresortid($resortname);
    print "Resort ID: $resortid\n";
}

Locations.pm - module inside '/usr/lib/perl5/ready/DataInterface'
shown is just one subroutine from the module
Code:

sub getresortid {

    my $self                = shift;
    my $parameters            = shift;

    my (@cursor, $returned, @cursor2, $returned2, $resortid);

    # First try a basic name match
    @cursor = $self->{SQL}->query("select id from location.resorts where name = ?", $parameters->{name});
    if ($returned = $cursor[0]->fetchrow_hashref) {
        $resortid = $returned->{"id"};
    } else {
        # Try and see if we've got an alternate spelling
        @cursor2 = $self->{SQL}->query("select attachedtoid from location.altspellings where name = ? and attachedto = 'resort'", $parameters->{name});
        if ($returned2 = $cursor2[0]->fetchrow_hashref) {
            $resortid = $returned2->{"attachedtoid"};
        }
    }

    if ($resortid) {
        return $resortid;
    } else {
        return undef;
    }

}

www.crmpicco.co.uk
www.ie7.com

ciderpunx February 14th, 2007 09:27 AM

Code:

my $parameters            = shift;
...
@cursor = $self->{SQL}->query("select id from location.resorts where name = ?", $parameters->{name});

Hey Craig,

You've created a local variable called $parameters which is a scalar (because you've called it like this: $resortid = $func->getresortid($resortname); where $resortname is a scalar, not a hashref) then you treating it as if it was a hashref. Try something like:

Code:

@cursor = $self->{SQL}->query("select id from location.resorts where name = ?", $parameters


--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

crmpicco February 16th, 2007 08:07 AM

thanks Charlie, great!

www.crmpicco.co.uk
www.ie7.com


All times are GMT -4. The time now is 12:05 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.