Here's a list of perl builtins that default to act on $_ when given no argument.

There are other operations that use $_ implicitly. For example, regexps, substitutions, and transliterations (m, s, tr, y) act on $_ unless a different string is given by the =~ operator; map, grep, the for statement modifier, and for loops without explicit loop variable specified use $_ as the loop variable; a diamond operator in a while loop stores the line to $_.

Below are some functions that do not default to $_.

Note that a few functions behave differently if they are called with an empty pair of parenthesis (usually taken as an empty list).

The following functions accept a filehandle, directory handle, label, or bareword as an argument, and thus don't default to $_. (I do not list those functions that need an array or hash argument, like pop.)

I've compiled these lists using both the perldoc perlfunc and testing how the functions actually behave with perl (5.8.8 i686-linux). For this reason, there can be errors and omissions in this list. If you know about any omission, please contact me.

Update: added readmore.

Update 2012-12-27: added the new evalbytes function.

Update 2014-01-29: added changes to mkdir and unpack.

Replies are listed 'Best First'.
Re: Builtin functions defaulting to $_
by ikegami (Patriarch) on Mar 23, 2007 at 15:51 UTC

    How to make your own function that defaults to $_:

    sub myfunc { my $arg = @_ ? $_[0] : $_; for ($arg) { ... } }

    The for is a convenient way to alias $_. (local $_ is buggy. It doesn't work well if $_ is tied or aliased to someting that's tied, and it doesn't protect pos($_).) Of course, you could just work with $arg directly instead of aliasing $_ to it.

    The use of a lexical ($arg) allows us to modify $_ without affecting the argument in the caller. If you don't modify $_, the above can be simplified to

    sub myfunc { for (@_ ? $_[0] : $_) { ... } }
Re: Builtin functions defaulting to $_
by brian_d_foy (Abbot) on Mar 24, 2007 at 03:04 UTC

    I went through perlfunc during a plane trip and tried to sort the functions and classify them. This is as far as I ever got, and I didn't go back over it to make sure I got it right or to see if I could sort them into better categories.

    No default

    different action without arg

    Misc

    $_

    optional args

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Builtin functions defaulting to $_
by Mutant (Priest) on Mar 23, 2007 at 14:56 UTC
    Some things in this list might change in 5.10 (at least mkdir() will). Who knows when that might come out tho :)
      The list will certainly change for Perl 6, insofar as it will be an empty list. If you want to default a function call to $_ you use the "unary dot" form:
      for 1..10 { .say }
      Of course, pattern matching and topicalizers still implicitly deal with $_, but you no longer have to memorize a long list of functions, nor worry about the difference between
      rand > 0.5 # means rand($_) > 0.5 rand < 0.5 # parse error looking for fileglob
      since in Perl 6 that is unambiguously
      .rand < 0.5 # always means rand($_) < 0.5
      You don't have to memorize which symbols are true globals either. As for Perl 5's magical use of $a and $b, the less said the better... :-)
        You don't have to memorize which symbols are true globals either.
        which will relieve a lot of grief. Like with $! and $@ which may get overwritten if not dealt with in a timely manner. If I well remember, $! will be just a reference to what the current error looks like; therefore the question: what's $@ in Perl 6, if it exists at all? I could think of that as a list of messages of what things blew up from there to here (ie some "exception stack trace excerpt") - but then, that would be more appropriately @$! (in perl5 syntax, that is)...

        update: reading again, not "have(ing) to memorize which symbols are true globals" means: there are no true global symbols, right?

        --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}
Re: Builtin functions defaulting to $_
by Steve_p (Priest) on Mar 25, 2007 at 13:49 UTC

    Actually, just yesterday there was a new addition to the list, readpipe.

    Change 30747 by rgs@benny on 2007/03/24 16:46:02 Make readpipe default to $_

    Of course, you'll need to wait a bit for Perl 5.10 before you can use it.



    Test your modules with bleadperl!

      rsync -avz rsync://public.activestate.com/perl-current/ .
      ./Configure -des -Dusedevel -Dprefix=/path/to/test/perl
      make test
      make install
    

    Now, please test you modules! If you have test failures that don't happen with Perl 5.8.8, send a simplified test case to

    perlbug at perl.org