in reply to the $_ Special Variable

Depending on your coding style, $_ can be ubiquitous. It's a default loop variable and default input for a number of functions. See the link for some general use scenarios.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: the $_ Special Variable
by tobyink (Canon) on Jul 06, 2012 at 15:52 UTC

    And you can write your own function that takes an implicit $_ using the (_) prototype.

    My favourite use for $_ is when defining a function that accepts a coderef. Example

    # This is a fairly useless function which is basically # just a copy of grep which enforces numeric context # on the list items... # sub filter_nums (&@) { my $coderef = shift; return grep { $coderef->() } map { 0 + $_ } @_ } my @even = filter_nums { not($_ % 2) } qw(1 2 3 4 potato);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'