Perl: How To Include Subroutine Files And Pass Values To Subroutines In PERL
Subroutines in PERL is just like standard Functions and
they act the same (Passing and receiving values). To call external files
with Perl subroutines use : require "" then call the subroutine by
: &(param1,param2...).
eg:-
##This is main file "data.pl"
#!/usr/bin/perl -w
$last = "SAMMY";
$ph = "12466";
require "show.pl"; ## This is the external subroutine file.
&test($last,$ph); ## This is the subroutine name;
we pass two arguments or parameters to it.
##
This is the subroutine inside "show.pl sub test
{
print "Last
name is :$_[0] Phone is :$_[1]";
}
1;
Note : Here the passed args($last and $ph) is reffered to as "$_[0]" and
"$_[1]" respectively. And end of this subroutine file must end with an "1;"
, otherwise it will show an error like - "show.pl did not return a true
value".
|