Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: passing variables to a subroutine

by moritz (Cardinal)
on Jul 04, 2012 at 09:02 UTC ( #979839=note: print w/ replies, xml ) Need Help??


in reply to passing variables to a subroutine

my $proph=@_;

Using an array (here @_) in scalar context returns the number of elements in the array , and is not what you want.

Typical solutions are (use only one of them)

my $proph = shift; # defaults to @_ my ($proph) = @_; # list context instead my $proph = $_[0]; # be explicit about wanting the first element

See perlintro and perlsub for more details.


Comment on Re: passing variables to a subroutine
Select or Download Code
Replies are listed 'Best First'.
Re^2: passing variables to a subroutine
by AWallBuilder (Beadle) on Jul 04, 2012 at 09:11 UTC
    thanks for the explanation, my $proph = $_[0]; # be explicit about wanting the first element helps, I now have some other errors to try and fix

      Just to pound the point completely into the ground:

      >perl -wMstrict -le "F('success'); ;; sub F { my $scalar_context = @_; my ( $list_context ) = @_; ;; print qq{in scalar context: '$scalar_context'}; print qq{in list context: '$list_context'}; } " in scalar context: '1' in list context: 'success'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (18)
As of 2015-07-31 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls