develooper Front page | perl.perl5.porters | Postings from May 2003

Re: [perl #22229] [no subject]

Thread Previous
From:
Michael G Schwern
Date:
May 18, 2003 10:57
Subject:
Re: [perl #22229] [no subject]
Message ID:
20030518175720.GO26852@windhund.schwern.org
On Sun, May 18, 2003 at 03:18:04AM +0200, Slaven Rezic wrote:
> > print "increasing a to 1\n" if ($a++);
> > print "increasing a to 2\n" if ($a++);
> > ----------------------------------------------------------------------
> > i.e. if trying to increase zero-valued (or undefined) scalar the
> > result is always false if using '++' operator.
> > below is perl -V from my system. i also tried it on 5.005_03 and it
> > worked the same manner.
> 
> This works as expected, because $var++ is the post increment operator.
> If you want pre increment, then use ++$var instead.

To clarify, post-increment means the value is checked and *then* its
incremented.  Like:

    if( $a ) {
        print "increasing a to 1\n";
    }
    $a += 1;

useful for things like:

    do_something_once unless $Done_Something++;

instead of:

    unless( $Done_Something ) {
        do_something_once;
        $Done_Something += 1;
    } 

To make your example work the way you expected, use ++$a (pre-increment).


-- 
Keep your stick on the ice.
    -- Red Green


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About