chuloon has asked for the wisdom of the Perl Monks concerning the following question:
How does the new regular expression (if (/under/) know what to grab from? Is it just the most recent object that was defined? I apologize for (probably) asking such a silly question. I feel like I'm missing something very obvious and fundamental. Thank you in advance for any help you can give.We could use a conditional as if ($sentence =~ /under/) { print "We're talking about rugby\n"; } which would print out a message if we had either of the following $sentence = "Up and under"; $sentence = "Best winkles in Sunderland"; But it's often much easier if we assign the sentence to the special va +riable $_ which is of course a scalar. If we do this then we can avoi +d using the match and non-match operators and the above can be writte +n simply as if (/under/) { print "We're talking about rugby\n"; } The $_ variable is the default for many Perl operations and tends to b +e used very heavily.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: The $_ Special Variable
by wind (Priest) on Jun 21, 2011 at 17:46 UTC | |
by chuloon (Initiate) on Jun 21, 2011 at 17:52 UTC | |
by ikegami (Patriarch) on Jun 21, 2011 at 17:57 UTC | |
by wind (Priest) on Jun 21, 2011 at 17:58 UTC | |
by chuloon (Initiate) on Jun 21, 2011 at 18:03 UTC | |
by wind (Priest) on Jun 21, 2011 at 18:20 UTC | |
| |
|
Re: The $_ Special Variable
by jwkrahn (Abbot) on Jun 21, 2011 at 21:52 UTC |