Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Why does default $_ only work

by perlfan (Vicar)
on Aug 10, 2020 at 19:38 UTC ( [id://11120555]=note: print w/replies, xml ) Need Help??


in reply to Re: Why does default $_ only work
in thread Why does default $_ only work

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.

Replies are listed 'Best First'.
Re^3: Why does default $_ only work
by haukex (Archbishop) on Aug 10, 2020 at 19:43 UTC
    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.

      Yes, corrected this while you were likely replying. I should have confirmed before I sent the original guess. :) Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found