Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: First char out of a string & surprises with errors on chop reverse $string (that doesn't work!)

by merlyn (Sage)
on Dec 26, 2001 at 21:43 UTC ( [id://134431]=note: print w/replies, xml ) Need Help??


in reply to First char out of a string & surprises with errors on chop reverse $string (that doesn't work!)

chop() wants an lvalue (like a variable), because it modifies its argument. The result of reverse is not an lvalue, so you lose.

-- Randal L. Schwartz, Perl hacker

  • Comment on Re: First char out of a string & surprises with errors on chop reverse $string (that doesn't work!)

Replies are listed 'Best First'.
Re: Re: First char out of a string & surprises with errors on chop reverse $string (that doesn't work!)
by eduardo (Curate) on Dec 26, 2001 at 21:47 UTC
    Ok... we'd figured that far when jeffa got:
    sub bar { my $str = "foo"; return scalar reverse $str; } print chop bar(); captvanhalen: $ perl foo Can't modify non-lvalue subroutine call in chop at foo line 6, near " +);" Execution of foo aborted due to compilation errors.
    Now, my question is... that's not very "DWIM" of perl... (at least not to me!) Why does reverse not return an lvalue?
        Definite agreement... reverse $foo = "merlyn"; makes little to no sense. However, chop reverse $foo; does seem relatively reasonable... (though I see how it quickly goes to hell in a handbasket.) I remember when in my Data Structures in C++ class they tried explaining the reason to overload some operators as lvalues, and some not... it always seemed like you could *find* a scenario where you may want that functionality (though I doubt 2 + 2 = $foo; would ever be a good one :) Well... thanks to everyone for the answers! I am going to agree that it really makes very little sense to let reverse return an lvalue... though I think the obfuscation "value" alone may warrant it :)
      For the same reason 2 + 2 doesn't return an lvalue.
      Besides, there's no reason for having reverse return an lvalue. TMTOWTDI, and reverse chop was never supposed to be one of them. Still, there are a lot of other WTDI.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Log In?
Username:
Password:

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

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

    No recent polls found