Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
pattern matching with $_ Posted by gregster on 24 Nov 2003 at 10:32 AM
What difference should putting the "print $_;" statement after the next statement make?

1st version

while (<$client_sock>) {
print $_;
next if (/Proxy-Connection:/);
print $server_sock $_;
last if ($_ =~ /^[\s\x00]*$/);
}

2nd version

while (<$client_sock>) {
next if (/Proxy-Connection:/);
print $_;
print $server_sock $_;
last if ($_ =~ /^[\s\x00]*$/);
}

The 1st version works for me, the 2nd doesn't. To me, they both do the same thing. Can someone explain to me the difference? My confusion is probably due to a misunderstanding of how $_ works.

Thanks
Greg
Report
Re: pattern matching with $_ Posted by Jonathan on 24 Nov 2003 at 1:41 PM
: What difference should putting the "print $_;" statement after the
: next statement make?
The order in which they are executed. If you're writing print $_; then you're not really taking advantage of $_. You could just put print; - $_ is the "default variable" for many operations. Having said that, the print $server_sock $_ line is a place you want to be explicit about it.

: 1st version
:
: while (<$client_sock>) {
: print $_;
: next if (/Proxy-Connection:/);
: print $server_sock $_;
: last if ($_ =~ /^[\s\x00]*$/);
: }
Will print the contents of $_ out, then test if it contains Proxy-Connection: and skip the rest of the loop if it does, going to the next line of input that can be read from $client_sock.

: 2nd version
:
: while (<$client_sock>) {
: next if (/Proxy-Connection:/);
: print $_;
: print $server_sock $_;
: last if ($_ =~ /^[\s\x00]*$/);
: }
This will check if $_ contains Proxy-Connection: and if it does jump over the statements in the while loop and try to fetch the next line.

: The 1st version works for me, the 2nd doesn't. To me, they both do
: the same thing.
It's like asking whether:-
print "hello!";
die;
And:-
die;
print "hello!";
Do the same thing really. Statements are executed in the order you write them.

: Can someone explain to me the difference? My confusion is probably
: due to a misunderstanding of how $_ works.
$_ is the default variable. Examples:-

while (<$some_handle>) {
    # $_ will contain each line we read from $some_handle
    s/foo/bar/; # Replace all instances of foo with bar in $_
    print; # Print the contents of $_ (with foos and bars).
}

# Also good in for loops.  This prints all elements in an
# array (bit of a waste...).
for (@array) {
    print;
}

# Or is you're lazy like me:-
print for (@array);


Hope this helps,

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.