Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Very nice, you just killed two birds with one stone^Wsub.

The first is the ability to cache an AUTOLOAD'ed function. By assigning an anonymous subroutine to the typeglob *{$AUTOLOAD}, you actually create the subroutine 'on the fly' in the package's symbol table - this means that only the first call to this method will suffer the 'find the right sub' performance hit. But I jumped the gun, as tilly pointed out, your method indeed does not add the method name to the symbol table. (thanks for providing the snippet tilly)

The second is being able to 'overload' the method so you don't have to specify if you wish to 'get' or 'set' an attribute. I tried to do the same thing, and failed miserably. :) Now i see how to do it (you just create one sub that determines which of two actions to take).

Spoilers Ahead:

The whole line starts out with a ternary operator. The question is - do the contents of $AUTOLOAD match the regex, and if so, is the number of arguments passed to this 'method' greater than 1?

The match catches everything except DESTROY, and as a side effect stores the attemped method name in $1. For example $object->foo() would be looked up as PKG::foo, and $1 would contain 'foo'.

This match takes place on the left hand side of the &&, and as a result, executes to see what the result is: if true, $1 indeed contains the matched 'method' and the right hand side of the && is evaluated.

The size of the argument list is checked via $#_, which returns the index of the last element in the list. Knowing that the first argument is always the name of the object, it is clear that if $#_ is greater than zero then the client is issuing a 'set' - if not, the client is issuing a 'get'.

The true part of the ternary operator describes the behavior for the 'set' function:

for example: $_[0]{$1}=$_[1] would roughly translate to: $self->{foo} = 'bar' if the method call was: $object->foo('bar');
And likewise, the false part defines a 'get' method:
for example: $_[0]{$1} would roughly translate to: return $self->{foo} if the method call was: $object->foo()
The only recommendation I would make would be to remove the negative look-a-head for DESTROY out of the match and just write an empty DESTROY method 'shell':
sub DESTROY {}
But that is just a personal preference.

(thanks to bikeNomad for class/object clear-up)



In reply to (jeffa) Re: Short version of autoload by jeffa
in thread Short version of autoload by simon.proctor

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2018-05-27 19:52 GMT
    Find Nodes?
      Voting Booth?