Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: functions and arguments

by mercuryshipz (Acolyte)
on Feb 18, 2008 at 20:53 UTC ( [id://668660]=note: print w/replies, xml ) Need Help??


in reply to Re: functions and arguments
in thread functions and arguments

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: functions and arguments
by hipowls (Curate) on Feb 18, 2008 at 21:31 UTC

    Not quite on topic, but why call your variables $var1 etc? Better would be to give them descriptive names. You can then dispense with the comments and don't have to refer elsewhere when they appear in the code.

    my $var1 = "dbname"; #Database Name my $var2 = "user"; #Username my $var3 = "pass"; #Password my $var4 = "select directory_path, keyword1, keyword2, keyword3 from +load_instance where rule_id = +'1'";
    becomes
    my $database = "dbname"; my $username = "user"; my $password = "pass"; my $query = "select directory_path, keyword1, keyword2, keyword3 from load_insta +nce where rule_id = '1'"; ... my $dbh = DBI->connect( "DBI:Oracle:", $database, $username, $password + ); ... $sth->prepare($query);

      The results of the function db_conn1 must be passed as arguments to the function search_phrase. But what i get is the results of the db_conn1 function and throws me an error, directory or path not found... but actually it does exists... this is true cuz when i run this function seperately it gives me the correct result... i actually stored the results of db_conn1 function's results in an array and passed it to the search_phrase function...

      #!/usr/bin/perl #use strict; use warnings; use DBI; my $db_name = "dbname"; #Database Name my $user_name = "myuser"; #Username my $passwd = "passswd"; #Password my $sql_query = "select directory_path, keyword1, keyword2, keyword3 +from load_instance where rule_id = '1'"; sub db_conn1 { my $dbh = DBI -> connect("DBI:Oracle:".$db_name,$user_name,$passwd) || + die ("not connected:DBI::errstr"); my $sth = $dbh -> prepare($sql_query); $sth->execute(); while(my @row=$sth->fetchrow_array()) #Fetch the number of rows { #returned by the query my $values=$row[0]; print "@row\n"; } } my @newarray = db_conn1($db_name,$user_name,$passwd,$sql_quer); sub search_phrase { my ( $inFile, @phrases ) = @_; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my $line; PHRASE: foreach my $phrase ( @phrases ) { my $rxPhrase = qr{\Q$phrase\E}; # keep reading down the file while ($line = <$inFH>) { # when one phrase matches, jump to the next next PHRASE if $line =~ /$rxPhrase/; } # end of file, and we haven't matched the last phrase return; } # We have just matched the last phrase. # The number we want is somewhere in $line. my ($number) = $line =~ /(\d+)/; return $number; } #my $result = search_phrase(db_conn1($var1,$var2,$var3,$var4)); my $result = &search_phrase('@newarray'); if (defined $result) { print "$result\n" } else { print "-1\n" }


      Errors: -------

      perl comb1.pl
      /users/myuser1/merc/test.txt total rows rejected: reject: total reject +ed rows: open: @newarray: A file or directory in the path name does not exist.


      Thanks.
        throws me an error, directory or path not found... but actually it does exists...
        Are you sure?
        $ cat 668658.pl use strict; use warnings; use Data::Dumper; sub f { my ($first, $second, $third) = @_; print "first:" . Dumper($first) . "\n"; print "second:" . Dumper($second) . "\n"; print "third:" . Dumper($third) . "\n"; } my @arr = qw ( one two three ); f('@arr'); f(@arr); __END__
        $ perl 668658.pl first:$VAR1 = '@arr'; second:$VAR1 = undef; third:$VAR1 = undef; first:$VAR1 = 'one'; second:$VAR1 = 'two'; third:$VAR1 = 'three';
        --
        Andreas
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: functions and arguments
by jrtayloriv (Pilgrim) on Feb 18, 2008 at 21:11 UTC
    Global symbol "$dbh" requires explicit package name at comb1.pl line 1 +3. Global symbol "$dbh" requires explicit package name at comb1.pl line 1 +4. Global symbol "@row" requires explicit package name at comb1.pl line 1 +6. Global symbol "$values" requires explicit package name at comb1.pl lin +e 18. Global symbol "@row" requires explicit package name at comb1.pl line 1 +8. Global symbol "@row" requires explicit package name at comb1.pl line 1 +9.

    These are telling you you need to use my and declare these things...

    For instance:

    $dbh = DBI -> connect("DBI:Oracle:".$var1,$var2,$var3) || die ("not connected:DBI::errstr");

    should be

    my $dbh = DBI -> connect("DBI:Oracle:".$var1,$var2,$var3) || die ("not connected:DBI::errstr");


    And what are you doing with this?:
    while(@row=$sth->fetchrow_array()) #Fetch the number of rows { #returned by the query $values=$row[0]; print "@row\n"; }

    I'm not too familiar w/ DBI, but it looks to me like are storing each row of the query results into @row , then storing first element of @row (which needs to be declared w/ my first) into $values (which also needs a my), but are not using $values for anything. Why did you do this?
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://668660]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-23 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found