CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
$_

 



SaiyanM
User

Aug 14, 2002, 4:13 PM

Post #1 of 6 (946 views)
$_ Can't Post

Im still kindof a newbie,

what does the "$_" mean?

ive seen it being used all around

-------------
Whats 2 + 2 x 2?
Its not 8...
(Please Excuse My Dear Aunt Sally)...


Coderifous
journeyman

Aug 14, 2002, 5:46 PM

Post #2 of 6 (944 views)
Re: [SaiyanM] $_ [In reply to] Can't Post

$_ is the 'default' variable. Checkout perlvar in the perldocs. ("perldoc perlvar")

You aunt sally is excused, but that's beside the point on your second question.


Code
  

print 2 + 2 x 2; # outputs 24



The reason it's not 6 (as you might have expected) is becuase the 'x' operator you are looking at is not for multiplication. It's for repetition. And, it's precedence is higher than the addition operator. SO what it does In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In this case, that would be '22'.

After the repitition operator has its way with the operands, you are looking at something like:


Code
  

print 2 + 22;



SaiyanM
User

Aug 14, 2002, 6:44 PM

Post #3 of 6 (941 views)
Re: [Coderifous] $_ [In reply to] Can't Post

that question about the 2+2x2 was my signature... Pirate

-------------
Whats 2 + 2 x 2?
Its not 8...
(Please Excuse My Dear Aunt Sally)...

(This post was edited by SaiyanM on Aug 14, 2002, 6:44 PM)


Coderifous
journeyman

Aug 15, 2002, 3:22 PM

Post #4 of 6 (931 views)
Re: [SaiyanM] $_ [In reply to] Can't Post

Blushuhhh... right... of course... I knew that. Everything after the first line of my post is my signature... just an amazing coincidence... thats all!

Angelic I swear.

--jim


SaiyanM
User

Aug 15, 2002, 5:20 PM

Post #5 of 6 (927 views)
Re: [Coderifous] $_ [In reply to] Can't Post

That perldoc didnt help me really... lol
can u try to help me out a bit?
Cool please?

-------------
Whats 2 + 2 x 2?
Its not 8...
(Please Excuse My Dear Aunt Sally)...


davorg
Thaumaturge / Moderator

Aug 16, 2002, 1:29 AM

Post #6 of 6 (924 views)
Re: [SaiyanM] $_ [In reply to] Can't Post

What exactly do you need to know?

$_ is the default variable. It is used as the default input and/or output for a number of Perl operators and functions. Often when $_ is being used, it isn't seen. For example:

Code
while (<FILE>) { 
print if /some_pattern/;
}

In that code, $_ is used three times, but it doesn't appear at all.

1/ When used in a "while" loop, the file input operator writes its results to $_.
2/ By default, the match operator works on the contents of $_.
3/ If "print" is given no arguments, it will print the contents of $_.

Does that help?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives