Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Subroutine Behaviour

by dReKurCe (Scribe)
on Sep 25, 2004 at 02:42 UTC ( [id://393736]=perlquestion: print w/replies, xml ) Need Help??

dReKurCe has asked for the wisdom of the Perl Monks concerning the following question:

Greetings monks: I have the following questions concerning the behaviour of the following code: I am concerned with the reasons that that they produce the following output:
#! /usr/bin/perl for $num(0..42){ foo($num); sub foo{ print "input to sub=@_\n"; if (@_== 0){ print "test for zero is accurate\n"; } elsif(@_== 1){ print "test for one is accurate\n"; } else{ print "Composing mortals with immortal fire. \n"; } } }
Produces the following output:
input to sub=0 test for one is accurate input to sub=1 test for one is accurate input to sub=2 test for one is accurate
Furthermore , using refs to the sub input as such :
if (\@_ == 0) and if (\@_ ==1)
generates:
input to sub=0 Composing mortals with immortal fire.
this is the same result for the entier range! Any input on the test and the nature of references would be greatly appreciated. Thanks Monks!

Replies are listed 'Best First'.
•Re: Subroutine Behaviour
by merlyn (Sage) on Sep 25, 2004 at 02:48 UTC
    You're always passing one item to your subroutine, so scalar @_ is always one. I'm not sure why you expected it to be different.

    And a reference to something is always true, and definitely not 0 and very unlikely to be 1, so that's why you end up further down.

    Perhaps you should study a bit more about subroutine arguments and things before trying skewy programs?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Subroutine Behaviour
by ikegami (Patriarch) on Sep 25, 2004 at 04:30 UTC

    An example says 1000 words?

    sub test { @a = @_; print(@a); # abc $a = scalar(@_); print($a); # 3 (number of elements) $a = @_; print($a); # 3 ($a is a scalar) $a = $_[0]; print($a); # a ($a) = @_; print($a); # a } $\ = "\n"; test('a', 'b', 'c');
      And:
      sub test { ($arg1, $arg2, $arg3) = @_; print $arg1; # a print $arg2; # b print $arg3; # c }
Re: Subroutine Behaviour
by TheEnigma (Pilgrim) on Sep 25, 2004 at 03:14 UTC
    @_ is an array, and trying to see if it's equal to something tests the size of the array, not the contents. You should put something like

    $num = shift;

    at the beginning of the subroutine, and then say things like

    if ($num == 0){

    That way you're testing the actual contents of the array. (Also note that the $num inside the subroutine is not the same variable as the $num outside the subroutine; although in this case they will always have the same value.)

    TheEnigma

Re: Subroutine Behaviour
by jdalbec (Deacon) on Sep 25, 2004 at 03:20 UTC
    @_ does what you want in string context as long as you pass only one argument. It is interpreted as the concatenation of all the arguments.

    When you compare @_ to 0 or 1 you're in scalar context and @_ is interpreted as the number of arguments.

    You could use $_[0] in place of @_ but it's probably better in the long run to start your subroutine with something like my $input=shift; and replace @_ by $input everywhere.

    Perl is very context-dependent. You'll get bitten by this a few times before you get the hang of it.

      Just a slight correction; when you say "@_ does what you want in string context", you really mean "when interpolated in a string". Usually the term "string context" is used along with numeric or boolean context, which is a very different kind of context.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://393736]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-25 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found