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.


In reply to Why does $$self++ works? by rob25

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.