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

Pre-increment *and* Post-increment

by cog (Parson)
on Feb 01, 2005 at 17:37 UTC ( [id://426976]=perlquestion: print w/replies, xml ) Need Help??

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

Can anyone explain why you can't do ++$a++?

I tried several different syntaxes to achieve this one, but to no avail...

Replies are listed 'Best First'.
Re: Pre-increment *and* Post-increment
by ambrus (Abbot) on Feb 01, 2005 at 17:48 UTC

    It's because unlike += the ++ perl operator (prefix or postfix) returns an rvalue, not an lvalue. You can't do ++$a = 8 or $a++ = 8 either.

    Update: swapped rvalue and lvalue, as noted by calin.

Re: Pre-increment *and* Post-increment
by Zaxo (Archbishop) on Feb 01, 2005 at 17:50 UTC

    Perl follows the C convention for the increment/decrement operators, or nearly so. ++$foo++ is undefined because only one *-ment op is allowed per variable in a statement. Would you like that to do (++$foo)++, or ++($foo++) ? It would make a difference, but there is nothing in the grammar to distinguish them.

    After Compline,
    Zaxo

      (++$foo)++ and ++($foo++) don't work either, for reasons mentioned by ambrus.
Re: Pre-increment *and* Post-increment
by brian_d_foy (Abbot) on Feb 01, 2005 at 20:20 UTC

    I was thinking about that this week, because I wanted (for no good reason), things like:

    $n+++; # increment, then do that again (add 2) $n++++; # increment, then again, and once more (add 3) $n++++++++++; # increment N-1 times, just because I can --$n++; # use the value that's one less, then restore it ++$n--; # same thing, other way $n++-; # increment, but not really cause this is a JAPH $n++-+; # I changed my mind, really do it this time $n++-+-; # boss says change it back $n+- # can I decide later, or, pick one at random $n** # no exponent, so square $n $n*** # cube it, and so on $n***/ # square it, but cube it first and go back one $n// # 1 $n/// # 1/$n $n///** # $n
    Surely Perl can be even more write-only if we just applied ourselves.
    --
    brian d foy <bdfoy@cpan.org>
      and that would be horribly annoying. Instead of  $n += 5; people would write $n++++++... does that seem pretty? On the other hand, it wouldn't be all that hard to write a source filter to do this...

      If you want +++ operator, rather use them for addition with lower precedence, for example this

      $a + $b ** $c + $d ++ $e + $f ** $g + $h *** $i + $j ** $k + $l ++ $m + $n ** $o + $p
      would become
      (($a + $b) * ($c + $d) + ($e + $f) * ($g + $h)) * (($i + $j) * ($k + $l) + ($m + $n) * ($o + $p))
Re: Pre-increment *and* Post-increment
by holli (Abbot) on Feb 01, 2005 at 17:49 UTC
    Why in the world do you want to do that?

    holli, regexed monk
Re: Pre-increment *and* Post-increment
by tinita (Parson) on Feb 03, 2005 at 14:09 UTC
    how about:   ${ \++$a }++;
    you can even do this as often as you want:   ${ \++${ \++$a } }++;
    =)
Re: Pre-increment *and* Post-increment
by Anonymous Monk on Feb 02, 2005 at 09:18 UTC
    You can't do ++$a++ because ++ returns a value, and not something you can modify. But you can write:
    ($a+=1)++
    which should have the same effect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-26 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found