Okay, first things first: What is $_? Simply put, it's a variable. In that sense, it's no different than $foo or $bar. What makes it special is that it's used it by default in many situations, if you don't supply another variable. The chomp() function, for example, which removes $/ (which is typically \n) from the end of one or more variables. If you write chomp($foo), it will affect $foo; but if you write chomp(), it will affect $_ by default.

Another example of using $_ is the foreach() loop in your own code. Foreach creates a local alias for each element in the given list. Because you didn't specify a name for that alias, it defaults to using $_. But, you could have written that as "foreach my $thisName (@myNames)", in which case the $thisName variable would be used as the alias instead.

The problem in your loop actually isn't the pattern match - that does just what you think it should, matching only strings that contain "jackson". The problem is that you've left out the curly braces. Unlike C, Perl's curly braces are always required for compound statements. So you need to write that if() like this:

if ($_ =~ "jackson") { print $_ . "\n"; }

That's only relevant when you're using a statement that requires a code block, though, as in "if() { ... }" - there are alternatives you can use that wouldn't require one, such as a logical-and operator or statement modifiers. (Note that these examples take advantage of the fact that the match operator m// also defaults to matching against $_!)

m/jackson/ and print $_ . "\n"; # logical-and print $_ . "\n" if (m/jackson/); # statement modifier
Finally (whew!) the . operator does string concatenation. That is, "foo" . "bar" will result in "foobar".

In reply to Re: On patterns and more by Sherm
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.