Re: Why does this work?
by Jenda Krynicky other posts by this author
May 20 2008 4:09PM messages near this date
Re: Why does this work?
|
Re: GD and libgd, please help
From: "Barry Brevik" <BBrevik@[...].com>
> OK, I've stumped myself. I wanted to assign 0 to several variables,
> except for a single variable that should be set to 1.
>
> Before I knew what I was doing I whipped this code into my editor:
>
> ($frow = $ax = $bx = $cx = 0)++;
>
> ...and it works as I expected. That is, all of the variables are set to
> 0 except $frow which is 1.
>
> Now I'm afraid that it might not always work because I don't understand
> why it works in the first case. Anyone want to suggest if this is stable
> code or not?
It will always work, but you should not do it like this.
$frow = 1;
$ax = $bx = $cx = 0;
would be much cleaner.
It works because the assignment "returns" not the value, but the
variable. That is
($x = 7)++;
is not equivalent to
$x = 7;
7++;
but to
$x = 7;
$x++;
There are few cases when this is actually fine to use, one of them is
($newvar = $oldvar) =~ s/some/transformation/;
in this case again, you assign the value of $oldvar to $newvar and
then apply the s/// to the $newvar.
Jenda
===== Jenda@[...].cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
_______________________________________________
ActivePerl mailing list
ActivePerl@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Barry Brevik
Casteele/ShadowLord
Jenda Krynicky
|