Pathologically Eclectic Rubbish Lister 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?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-05-27 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (195 votes). Check out past polls.

Notices?