Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Why does $$self++ works?

by davido (Cardinal)
on Sep 28, 2018 at 09:48 UTC ( #1223230=note: print w/replies, xml ) Need Help??


in reply to Why does $$self++ works?

This is a blessed scalar. Something like this:

sub new { my ($class, $initial_int) = @_;; my $scalar = 0+($initial_int // 0); return bless \$scalar, $class; }

A more typical constructor would bless a hashref:

sub new { my ($class, $args) = @_; die "Constructor args must be passed as a hash reference.\n" if defined($args) && ref($args) ne 'HASH'; $args //= {}; return bless $args, $class; }

(Neither of these have a sufficient level of error checking on their input paramaters, and are intended as a demonstration only.)

But we'll look at the blessed scalar reference, as it's what is probably at play here. The last piece is this:

my $o = Foo->new(0); ${$o}++; # dereference $o, then increment the value of the scalar that + the scalar reference in $o points to. # Because the dereferencing sigil binds more closely than the postincr +ement operator, you can also do this: $$o++; # dereference $o, then increment the value of the scalar that t +he scalar reference in $o points to.

Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1223230]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2019-05-20 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (123 votes). Check out past polls.

    Notices?