rob25 has asked for the wisdom of the Perl Monks concerning the following question:
Greetings,
at first the short form of my question(s) - direct from my heart to the monestary public. After that, I provide some more background.
In short:I recently implemented some code inspired by a CPAN code example. Here is a snippet:
# add one handle and count it sub add_handle($$) { my $self = shift; my $easy = shift; $$self++; $self->SUPER::add_handle( $easy ); }
Why is it possible to use an object as a counter? Why does this not destroy the object? When I coded a similar thing for testing, the program breaks with the message "Not a scalar reference".
Background of the question:The example comes form the Net-Curl-package (see https://metacpan.org/pod/distribution/Net-Curl/lib/Net/Curl/examples.pod#Extracted-from-examples/02-multi-simple.pl). I made a successful API implementation along this example. But while adopting the example this special bit of code (the $$self++) looks so wired to me, that I choose to replace it by somewhat more readable ($self->{MULTI_HANDLER_COUNT}++).
But now I did some profiling on the API, and I ask myself, whether there is the essential difference between the official example code and the way I implemented the counting.
I hope someone can give me some deeper insights about the difference.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why does $$self++ work?
by hippo (Archbishop) on Sep 28, 2018 at 09:56 UTC | |
by ikegami (Patriarch) on Sep 29, 2018 at 07:14 UTC | |
by rob25 (Initiate) on Sep 28, 2018 at 10:10 UTC | |
|
Re: Why does $$self++ works?
by LanX (Saint) on Sep 28, 2018 at 10:09 UTC | |
|
Re: Why does $$self++ works?
by davido (Cardinal) on Sep 28, 2018 at 13:48 UTC | |
|
Re: Why does $$self++ works?
by AnomalousMonk (Archbishop) on Sep 28, 2018 at 14:54 UTC |