CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Passing arguments to subroutines

 



d1zz13
User

Aug 31, 2000, 12:25 AM

Post #1 of 2 (269 views)
Passing arguments to subroutines Can't Post

Is it possible to pass global variables to a subroutine, if so how?

I have a script that parses the details from the form and then I want it to call a subroutine to print the details. I have this line of code that calls the subroutine. (It's part of an if statement)

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


elsif ($INPUT{'chead'}) {&change_head; }
</pre><HR></BLOCKQUOTE>

'chead' is the name of the Submit button, but the form also has a field in it called new_head. So I've changed the code to this.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


elsif ($INPUT{'chead'}) {&change_head ($new_head);}
</pre><HR></BLOCKQUOTE>

But this doesn't work, the subroutine looks like this.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


&change_head{
print "Content-type: text/html\n\n";
print "You typed $new_head Thanks.\n";
}
</pre><HR></BLOCKQUOTE>

All I'm getting is:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


You typed Thanks
</pre><HR></BLOCKQUOTE>

Any help with this matter would be much appreciated.

Regards
Richard Benwell

------------------
Dizzie Scripts


japhy
Enthusiast

Aug 31, 2000, 3:48 AM

Post #2 of 2 (269 views)
Re: Passing arguments to subroutines [In reply to] Can't Post

Check the perlsub documentation on how to write functions.

Aside from that, if you want to pass values to a function, you do it like so:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


function($this, @that, %those);
</pre><HR></BLOCKQUOTE>

Arguments are then stored in the @_ array in the function. The argument list is flattened -- meaning:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$name = "Jeff";
@langs = qw( Perl C++ Python );
person(@langs, $name);
sub person {
my (@languages, $person) = @_;
print "$person uses: @languages\n";
}
</pre><HR></BLOCKQUOTE>

Does not pass @langs as one argument, but rather unrolls the list as if I had said

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


person("Perl", "C++", "Python", "Jeff");
</pre><HR></BLOCKQUOTE>

In addition, saying

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


(@this,$that) = (1,2,3,4);
</pre><HR></BLOCKQUOTE>

Puts (1,2,3,4) in @this, and leaves $that empty -- so our function should PROBABLY be rewritten as:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


person($name, @langs);
sub person {
my ($who, @proglangs) = @_;
print "$who uses: @proglangs\n";
}
</pre><HR></BLOCKQUOTE>

Anyway, arguments are in @_. You can access them individually via $_[0], $_[1], etc. IF YOU MODIFY $_[$x], then the ACTUAL ARGUMENT YOU PASSED GETS MODIFIED. Example:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$name = "jeff";
change($name);
print $name; # Ffej
sub change {
$_[0] = ucfirst reverse $_[0]
}
</pre><HR></BLOCKQUOTE>

That's all for now.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives