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: Beginner:
about $_

 



kevindai
Novice

May 26, 2009, 11:34 AM

Post #1 of 4 (405 views)
about $_ Can't Post

I am trying to pass argument to a function. and within function I want to use the passed argument.
eg:
somefunc()
{
&calculate($value);
}

sub calculate()
{
my $temp = $_;
# do calculation using the value passed from above and return
}
My question is .....Do i have to declare "$_"? or how do i do this
have perl warnings On.

In Reply To

Quote


1arryb
User

May 26, 2009, 1:26 PM

Post #2 of 4 (403 views)
Re: [kevindai] about $_ [In reply to] Can't Post

Hi kevin,

Function arguments are passed as an array, not a scalar. Try:

Code
my $temp = $_[0];

Or, more idiomatically:

Code
my $temp = shift;


Also, your program won't compile, as written. Subroutine definitions don't take '()' after the subroutine name.


Cheers,

Larry


FishMonger
Veteran / Moderator

May 26, 2009, 3:38 PM

Post #3 of 4 (401 views)
Re: [1arryb] about $_ [In reply to] Can't Post


Quote
Subroutine definitions don't take '()' after the subroutine name.


Yes they do...that's a prototype definition, which you almost never want to use, and definitely not the way they're being used here.

http://www.perl.com/doc/manual/html/pod/perlsub.html


kevindai
Novice

May 26, 2009, 10:19 PM

Post #4 of 4 (396 views)
Re: [FishMonger] about $_ [In reply to] Can't Post

Thank you! Larry and FishMonger!


(This post was edited by kevindai on May 26, 2009, 10:20 PM)

 
 


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

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