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

Hello monks,

I started to play with perl for the past few months during my free time. I picked up programming perl which I think is among the very best of programming books...

Coming to the point, I found something along the lines in programming perl and it works when I test it on my command line. I believe programming perl is for intermediate perl programmers which I am not. Can any one help me understand why the print from the book is working fine, where as my print command is bombing other than the fact that one was written by Larry Wall and other one by lame me?

perl -e 'my @scale=("A" .. "G"); my $note = -1; print (($note += 1) %= + @scale)' #Working fine 0 perl -e 'my @scale=("A" .. "G"); print (0 %= @scale);' # Not working Can't modify constant item in modulus (%) at -e line 1, near "@scale)" Execution of -e aborted due to compilation errors.
Jr. Monk

Replies are listed 'Best First'.
Re: perl behavior with print command
by NetWallah (Canon) on Feb 17, 2016 at 16:59 UTC
    ($note +=1) is an "lvalue" - i.e. a value that can be assigned to.

    "0" (a constant) cannot be assigned to (It cannot take a different value), so perl complains.

    Note "%=" is essentially an assignment operator.

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

      Thank you NetWallah. That explains the problem.
        next error will be print interpreted as a function.. because you have print (..) you can use the strange syntax print +(..) or the ugly print "",(..) one

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.