Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Why does default $_ only work

by catfish1116 (Beadle)
on Aug 10, 2020 at 17:37 UTC ( [id://11120551]=perlquestion: print w/replies, xml ) Need Help??

catfish1116 has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a very simple script but dont understand why chomp($_ = <STDIN>) works and chomp(my $selection = <STDIN>) doesnt. Below is code written with chomp set to default.

print "Enter a string that will match the secret matching pattern \n" +; chomp ($_ = <STDIN>); if ($_ = (/(.)\1/)) { print "The match was perfect \n"; } else { print

TIA The Catfish

Replies are listed 'Best First'.
Re: Why does default $_ only work
by haukex (Archbishop) on Aug 10, 2020 at 18:31 UTC

    As LanX said, you haven't really given enough information to go on. Please see How do I post a question effectively?, I know what I mean. Why don't you?, and Short, Self-Contained, Correct Example.

    My guess would be that it has something to do with $_  =  (/(.)\1/), since that doesn't entirely make sense to me. It's the equivalent of $_ = ($_ =~ /(.)\1/), that is, you're matching the regular expression against $_ and then assigning the return value of the match back to $_. Similarly, if you wrote $selection = (/(.)\1/), then that is implicitly matching the regular expression against $_ and assigning the return value of the match to $selection. You probably meant to write $selection =~ /(.)\1/ instead.

Re: Why does default $_ only work
by LanX (Saint) on Aug 10, 2020 at 18:07 UTC
    Please show us the code that "doesn't work".

    Best an example which also compiles ... plus input.

    My guess is you only renamed one $_.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Why does default $_ only work
by catfish1116 (Beadle) on Aug 10, 2020 at 18:51 UTC

    Code that doesnt work:

    print "Enter a string that will match the secret matching pattern \ +n"; chomp (my $selection = <STDIN>); if ($selection = (/(.)\1/)) { print "The match was perfect \n"; } else { print "Match not found \n"; }
      Your problem has nothing to do with chomp, but rather the syntax of the match. Refer to the section "Regexp Quote-Like Operators" in perlop.
      use strict; use warnings; my $secret = \"AA"; close STDIN; open STDIN, '<', $secret or die $!; print "Enter a string that will match the secret matching pattern \n"; chomp (my $selection = <STDIN>); if ($selection =~ m/(.)\1/) { print "The match was perfect \n"; } else { print "Match not found \n"; }

      OUTPUT:

      Enter a string that will match the secret matching pattern The match was perfect

      Note that you first example only appears to "work". It gets the right answer for the wrong reason.

      Bill
      At first glance, it's appears to me as if it's working due to a side effect. chomp operates on $_ implicitly, which you are affecting in your example that works. I do not believe chomp is taking the LHS of the assignment and working with $_ like you think it is. Update - I stand corrected - You can actually chomp anything that's an lvalue, including an assignment:... in chomp. I do think it's related to you using $_ inconsistently when switching to $selection.

      For clarity you probably still want:

      my $selection = <STDIN>; chomp ($selection);
      And your regex seems incorrect, as pointed out by LanX. This may be where using $_ originally got you.
        I do not believe chomp is taking the LHS of the assignment and working with it that scalar.

        No, that's incorrect, chomp does modify its argument and the return value of the assignment my $selection = <STDIN> is the lvalue $selection, which chomp operates on. The use of chomp in this code is correct and has nothing to do with the problem, it's a = vs =~ problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11120551]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found