There's more than one way to do things PerlMonks

### Re^5: \$1 not "freezing" in an addition

by grondilu (Friar)
 on Dec 14, 2012 at 15:36 UTC ( #1008859=note: print w/replies, xml ) Need Help??

Interesting. This gave me the idea of trying this code:

\$_ = 1; print \$_ + \$_++;

Amazingly enough, this prints 3, not 2

But this does print 2:

\$_ = 1; print 0+\$_ + \$_++;

Replies are listed 'Best First'.
Re^6: \$1 not "freezing" in an addition
by tobyink (Abbot) on Dec 14, 2012 at 16:03 UTC

I can count as high as 7...

```sub r  { \$_[0] } # no-op?
sub rr :lvalue { \$_[0] } # no-op?

\$_ = 1;
print (\$_) + \$_++;

\$_ = 1;
print r(\$_) + \$_++;

\$_ = 1;
print \$_ + \$_++;

\$_ = 1;
print \$_ + ++\$_;

\$_ = 1;
print r(++\$_) + r(++\$_);

\$_ = 1;
print ++\$_ + ++\$_;

\$_ = 1;
print rr(++\$_) + rr(++\$_)++;

(Requires Perl 5.16, though you can get up to 6 in Perl 5.10 and above.)

Update: 0..10

```use v5.16;
no v5.17;
no strict 'subs';

sub r          { \$_[0] } # no-op?
sub rr :lvalue { \$_[0] } # no-op?

\$_ = 1;
say ( \$ ++ _ ) + \$_++;

\$_ = 1;
say (\$_) + \$_++;

\$_ = 1;
say r(\$_) + \$_++;

\$_ = 1;
say \$_ + \$_++;

\$_ = 1;
say \$_ + ++\$_;

\$_ = 1;
say r(++\$_) + r(++\$_);

\$_ = 1;
say ++\$_ + ++\$_;

\$_ = 1;
say rr(++\$_) + rr(++\$_)++;

\$_ = 1;
say ++rr(++\$_) + rr(++\$_);

\$_ = 1;
say ++rr(++\$_) + rr(++\$_)++;

\$_ = 1;
say ++rr(++\$_) + ++rr(r(rr(++\$_)++));
perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'

Create A New User
Node Status?
node history
Node Type: note [id://1008859]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-06-18 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (110 votes). Check out past polls.

Notices?