pattern matching with $_ - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

pattern matching with $_

gregstergregster Posts: 7Member
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 ($_ =~ /^[sx00]*$/);
}

2nd version

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

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

Comments

  • JonathanJonathan Posts: 2,914Member
    : 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 ($_ =~ /^[sx00]*$/);
    : }
    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 ($_ =~ /^[sx00]*$/);
    : }
    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:-

    [code]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);
    [/code]

    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.");

Sign In or Register to comment.