Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables between functions 1

Status
Not open for further replies.

NashTrump

Technical User
Jul 23, 2005
38
GB
Hi all

Im fairly new to perl so bear with me! :)
Just wondered if someone can help me..

Ive got two perl subs and want to pass variables from one to the other and then return a result.

IE:

1st sub contains this code :

@linkage = findlinkage($url,$required,$title);

The findlinkage sub is below:

sub findlinkage($urla, $requireda, $titlea){

$ua = LWP::UserAgent->new;

# Set up a callback that collect image links
my @imgs = ();
my($tag, %attr) = @_;
return if $tag ne 'a'; # we only look closer at <img ...>
push(@imgs, values %attr);

# Make the parser. Unfortunately, we don't know the base yet
# (it might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback);

# Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),
sub {$p->parse($_[0])});

# Expand all image URLs to absolute ones
my $base = $res->base;
@imgs = map { $_ = url($_, $base)->abs; } @imgs;

# Print them out
print join("\n", @imgs), "\n";
return @alinks;

}

im getting an error of:

Illegal character in prototype for main::findlinkage : $urla,$requireda,$titlea at /perl/site/lib//findlinks.pm line 11.

Any one help me?
Line 11 is the first line in the sub: IE:sub findlinkage($urla, $requireda, $titlea){

Im a VB programmer and this is the way i pass arguments in VB, is Perl completely different? i cant manage to track down much documentation on this.

Thanks for any help!!

regards

Nash
 
That's because you defined a prototype instead of defining parameters.
Change:
Code:
sub findlinkage($urla, $requireda, $titlea){
to
Code:
sub findlinkage {
    my ($urla, $requireda, $titlea) = @_;

Good luck! :)


Trojan.
 
nice one!!

That sorted that ...

however im getting another error now:

findlinks.pm did not return a true value at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 7.
BEGIN failed--compilation aborted at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 7.


I thought it was because im using two variables which obviously arent the same

# Print them out
print join("\n", @imgs), "\n";
return @alinks;

so i changed them to '

# Print them out
print join("\n", @imgs), "\n";
return @imgs;

But it still doesnt make a difference..

any ideas?
I want the value of @linkage to be the same as @imgs..
 
Yea, dismiss the concept of of passing variables like this in your perl scripts

Code:
sub sub_routine_name(<list>) {

}

and do it how TrojanWB showed you above. Life will be much easier. :)

I'm not sure what you are trying to do here (in bold):

Code:
sub findlinkage($urla, $requireda, $titlea){

  $ua = LWP::UserAgent->new;

  # Set up a callback that collect image links
  my @imgs = ();
     [b]my($tag, %attr) = @_;[/b]
     return if $tag ne 'a';
     [b]push(@imgs, values %attr);[/b]

it could work, but if you pass three scalar variables to the sub routine, the last two will be turned into a hash with the first one the key and the second one the value. But in the context of the code you posted it makes little sense to create a hash with one key/value pair and only use it to push the one value into an array.
 
repost the script and point out which line is line 7.
 
#!/perl/bin/perl -w

use lib "/perl/site/lib/";
use strict;
use DBI;
use Switch;
use findlinks;

line 7 is use findlinks;

I was creating a new module and saving it under my perl/site/lib/

in that module i was saving the sub routine that i posted above

I was trying to treat it like a module in VB and just access it via calling the script name..
 
this is the entire script of databaseaccess.pl

#!/perl/bin/perl -w

use lib "/perl/site/lib/";
use strict;
use DBI;
use Switch;
use findlinks;
my $dbname = "betting";
my $host = "localhost";
my $user = "root";
my $pass = "";
my $table = "bookies";
my @row;
my $sth;
my $sql;
my $fields;
my $siteid;
my $url;
my $title;
my $dateage;
my $depth;
my $required;
my $disallowed;
my @linkage;
my $dbh = DBI->connect("DBI:mysql:$dbname", $user, $pass) or die $DBI::errstr;

$DBI::result = $dbh->prepare("select site_id,url,title,indexdate,spider_depth,required,disallowed from $table");
$DBI::result->execute();

$fields = $DBI::result->{NUM_OF_FIELDS};
while (my $ref = $DBI::result->fetchrow_arrayref) {
for (my $i=0; $i < $fields; $i++) {
switch ($i){
case 0 {$siteid = $$ref[$i]}
case 1 {$url = $$ref[$i]}
case 2 {$title = $$ref[$i]}
case 3 {$dateage = $$ref[$i]}
case 4 {$depth = $$ref[$i]}
case 5 {$required = $$ref[$i]}
case 6 {$disallowed = $$ref[$i]}
}
print "$i = $$ref[$i] ";
}

@linkage = findlinkage($url,$required,$title);
print "\n";
}

$dbh->disconnect;
 
OK, your findlinks module is not returing a true value, at the end of modules loaded with 'require' or 'use' it's always best to put as the last line:

1;

this way the module returns a value when perl loads it, which is a requirement of perl.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top