Problems? Is your data what you think it is? PerlMonks

Re: Quantum Weirdness and the Increment Operator

by ysth (Canon)
 on Jun 24, 2004 at 18:33 UTC ( #369448=note: print w/replies, xml ) Need Help??

in reply to Quantum Weirdness and the Increment Operator

This may prove useful to study:
my \$x; # unless overloaded, numeric values of two refs to the same thing are +equal {package X; use Carp; sub is_x { carp \\$x == \\$_[0] ? "ok" : "not ok" +}} X::is_x(\$x); X::is_x(++\$x); X::is_x(\$x++); X::is_x(\$x+0);

Replies are listed 'Best First'.
Re^2: Quantum Weirdness and the Increment Operator
by barrachois (Pilgrim) on Jun 25, 2004 at 02:54 UTC
Yes.

Part of what surprised me was that "return" gave me back a copy, so my "noop" subroutine should actually be called "copy".

sub noop { X::is_x( \$_[0] ); # ok return \$_[0]; } X::is_x( noop(\$x) ); # not_ok
Update

The plot thickens.

I see over on Aliasing and return, how does return work? a discussion of just this issue, with a suggestion by japhy to use lvalue subs.

A quick test shows that one can indeed return \$x itself this way - but the increment expression is still altered by a call to this kind of subroutine, too.

sub noop_lval :lvalue { \$_[0] } X:is_x( noop_lval(\$x) ); # ok ! \$m=20; print noop_lval(++\$m) + \$m++; # 42 (still) \$m=20; (++\$m) + \$m++; # 43
So I guess the copy that "return" makes isn't the whole issue.

Create A New User
Node Status?
node history
Node Type: note [id://369448]
help
Chatterbox?
 vrk adds oatmeal cookies to the platter on the sideboard. [LanX]: evrybody can participate in a YAPC choroba is still undecided what to present at YAPC [LanX]: imagine the stock market if the choice for the second round would have been those two ... [robby_dobby]: LanX: Not me. No YAPC in my country and the only one in Asia is so far off in Japan [LanX]: Oh I thought "Misery" meant Missoury [LanX]: where do you live? [LanX]: YAPC Asia was renamed and you'd need to speak Japanese to be able to follow [robby_dobby]: LanX: Misery :P [robby_dobby]: Or India

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2017-04-24 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (442 votes). Check out past polls.