Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

The $_ Special Variable

by chuloon (Initiate)
on Jun 21, 2011 at 17:35 UTC ( #910779=perlquestion: print w/replies, xml ) Need Help??
chuloon has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I'm a very new user to perl, and naturally I have a very newbie question.. How does the special variable, ?_ work? I don't understand how it stores information, how you get it to store information, and what it stores exactly. In the tutorial I'm using, the following is all I have to go on in terms of ?_
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.
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.

Replies are listed 'Best First'.
Re: The $_ Special Variable
by wind (Priest) on Jun 21, 2011 at 17:46 UTC
    In that instance, $_ could be anything. But you can assign directly to $_ to let m// use it as default instead.
    $_= "Best winkles in Sunderland"; if (/under/) { print "We're talking about rugby\n"; }
    The more common use of $_ is when it's the default iterator in a for loop, or in a while loop when reading from a file.
    # Instead of having for my $iter, for uses $_ as the iterator below: for ("Up and under", "Best winkles in Sunderland") { if (/under/) { print "We're talking about rugby\n"; } }
      So the ?_ variable is more or less the same as any other variable? What you just did seemed as though you were just renaming something, i.e. $sentence to ?_ Is that wrong and it's doing something more?

        So the $_ variable is more or less the same as any other variable?

        Yes, there's nothing inherently special about it*. It's simply the variable many ops use if no alternate is specified.

        * — Like other punctuation variables, it transcends packages. That's a bit special, but it's not all that relevant in practice.

        $_ is just a special global variable as defined in perlvar.

        It is used as the default input for a lot of builtin functions when nothing else is defined. But yes, it can be thought of as just any other variable, just one that is declared automatically by perl to enable you to write tighter code when you want to.

Re: The $_ Special Variable
by jwkrahn (Monsignor) on Jun 21, 2011 at 21:52 UTC
    If we do this then we can avoid using the match and non-match operator +s and the above can +be written simply as if (/under/) { print "We're talking about rugby\n"; }

    The statement above is incorrect.    /under/ is the match operator.    The missing operator there is the binding operator.    And there is no such thing as a "non-match" operator.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://910779]
Approved by ikegami
[Petroza]: Hey there, I'm new here. I tried to post a question under ''Seekers of Perl Wisdom'' and when I tried to create it An Error appeared stating that I don't have permissions to post there. Do I need some sort of an Authorization?
[LanX]: no try again
[choroba]: Didn't you forget to add a title? Did you include links to the post?
[LanX]: choroba IIRC these errors are indicated
[1nickt]: Missing title suppresses the submit button, I think.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-10-17 15:15 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (233 votes). Check out past polls.