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

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);

Replies are listed 'Best First'.
Re^4: functions and arguments
by mercuryshipz (Acolyte) on Feb 20, 2008 at 15:45 UTC
    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.