in reply to How to pass by reference

You've already got a good answer explaining what you need to know.

I just wanted to add that for about the last ten years there have been very few occasions when you need to put the '&' on the front of a function call. Unless you know that you need it (and in 99% of cases - including this one - you don't) then you should leave it off.

And if you got that bad habit from a book or an online tutorial then you should view any other information that you got from the same place with deep suspicion.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: How to pass by reference
by tanyeun (Sexton) on Jul 31, 2006 at 08:45 UTC
    I learned that from Randal's Learning Perl
    it tells that '&' can be omit if
    there's no built-in function that
    has the same name of yours
    because I wasn't sure which subroutine name
    I use will clash that of the built-in function
    so I always add '&' in the front
    I don't know it is a bad habit.
    so why it is a bad habit anyway?

      Well, the (admittedly small) danger is that a call like &bar will pass on the current value of @_ to bar. That's almost never what you want. And even if it _is_ what you want, you'll confuse a maintenance programmer unless you write it as &bar(@_) (or bar(@_)).

      #!/usr/bin/perl sub foo { &bar(1, 2, 3); &bar; # silently passes on @_ } sub bar { print "@_\n"; } &foo('a', 'b', 'c');

      Also, in general, less punctuation is good - as the less there is to type, the less there is to get wrong :-)

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      From perlsub:
      &NAME; # Makes current @_ visible to called subroutine.
      and
      A subroutine may be called using an explicit "&" prefix. The "&" is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The "&" is not optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the "&$subref()" or "&{$subref}()" constructs, although the "$subref->()" notation solves that problem. See perlref for more about all that.

      Subroutines may be called recursively. If a subroutine is called using the "&" form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.

      &foo(1,2,3); # pass three arguments foo(1,2,3); # the same foo(); # pass a null list &foo(); # the same &foo; # foo() get current args, like foo(@_) !! foo; # like foo() IFF sub foo predeclared, else " foo"
      Not only does the "&" form make the argument list optional, it also disables any prototype checking on arguments you do provide. This is partly for historical reasons, and partly for having a convenient way to cheat if you know what you're doing. See Prototypes below.

      So, two points with '&' here: 1) propagates @_ implicitly, 2) bypasses prototype checking. If any of them is what you want, using '&' is fine; else not.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}