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

Dear Monks:

I have a subroutine that I call several times in my program. For some reason, when I call it with no arguments at one point @_ is set to ('main'). What's going on? This occures from the initiation of the subroutine. it's driving me insane..
I haven't been able to reproduce the results with test programs, but here's the line from which the subroutine is called, with a surrogate subroutine for all it's worth...

(( &token( "next", "marker" ))[0] eq ';') && &token; sub token{ print "args: @_\n"; return 1; } sub error{ return; }
much thanks.

20040105 Edit by ysth: p, br, and code tags

Replies are listed 'Best First'.
Re: Has Perl gone completely insane on me?
by ikegami (Patriarch) on Jan 06, 2005 at 06:40 UTC

    When you use & and no arguments, @_ is preserved.

    sub func { print(scalar(@_), $/); } @_ = qw(a b c); &func; # 3 func(); # 0 func; # 0 &func(1, 2); # 2 func(1, 2); # 2 func 1, 2; # 2

    There's rarely any need to use the ampersand notation. I think you shouldn't use it unless necessary.

      There's rarely any need to use the ampersand notation. I think you shouldn't use it unless necessary.
      Some people feel more comfortable using &. It leads to less action at a distance (where perl parses differently depending on whether it's already seen a sub declaration). But if you do choose to use &, always use () with it.