$_ is a variable that is set to whatever Perl thinks you are most likely to want to use as a default command/subroutine argument. Many Perl commands will automatically use whatever value is assigned to $_ so you don't have to do so much typing:

defined #same as defined($_) print #same as print STDOUT $_ /\sjackson$/ #same as $_ =~ /\sjackson$/

The actual value assigned to $_ depends on the context because what you are "most likely to want to use as an argument" usually depends on what you are doing in your code at that moment.

In a for loop $_ is the current array element.

When using grep or map it is also the current array element:

@only_defined = grep { defined($_) } @somearray; %hash_defined = map { $_ => 1 } @only_defined; #and since $_ is the default argument, you can also write @only_defined = grep { defined } @somearray; @only_begins_with_a = grep { /^a/ } @somearray;

If you are reading from a file using the <> operator, it is the most recently read line, e.g.

while (<STDIN>) { chomp # same as chomp $_ if (/^#/) {....} # same as if ($_ =~ /^#/) { ....} }

You don't have to use $_ for loops. In fact, for production code, it is a good idea not to because every so often something you are doing inside the for loop might unexpectedly reset $_ (it shouldn't but you don't always have control over other people's code and how they use things like $_. The inside of loops often makes calls to other people's code). For example, your loop above could also have been written:

#Note this code still won't work - see sherm's explanation below. foreach my $name (@myNames) { if($name =~ "jackson") print $name . "\n"; #sic - should be { print $name . "\n" } }

The same caution about $_ also applies to $_ when reading from an input stream or any other use of $_. If you are doing something complicated, it is best to assign $_ to a variable with a name you control and to use that variable instead of $_. Thus a better way to write the code reading from STDIN above would be

while (my $line = <STDIN>) { chomp $line; if ($line =~ /^#/) {....} }

Update: added comment about non-working code and referenced sherm's explanation below.


In reply to Re: On patterns and more by ELISHEVA
in thread On patterns and more by Sary

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.