Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

++ Very nice start to a tutorial -- and on a topic that continues to need it, as recent criticism suggests.

I have a few corrections and comments.

sub new { my $self = shift; register( $self );

It should be:

sub new { my $class = shift; my $self = register( $class );

That's the longhand approach for clarity of what the arguments mean. my $self = register( shift ) would be shorthand.

When provided with a single, non-reference argument, register treats it as a class name and will return a reference to an anonymous scalar blessed into the given class. Thus, considering this question:

$manager->{phone} = '555-1313'; # {{what is the behavior of this??}}

If you use the new() example I give above, this will die with an error as $manager is a reference to a scalar, not a hash.

Note that if you use an anonymous hash reference (using an alternate style of register):

sub new { my $class = shift; my $self = register( {}, $class ); # register does the bless, too

Then $manager->{phone} = '555-1313' will work as usual, but will have nothing to do with the value retrieved by $manager->phone().

Aren't inside-out objects fun?

public birthday => my %birthday, { set_hook =>; \&_set_birthday };

Extra semicolon. set_hook => \&_set_birthday

Return values from get_hook are ignored; the accessor expects that get_hook will simply modify '$_'.

This is incorrect. The behavior isn't symmetric with set_hook. For get_hook, $_ is a copy of the attribute -- you can modify it without affecting what is stored. The return value of the get_hook is returned from the accessor. Your example just happens to work because the last thing you do is $_ = strftime and that is what gets returned. You could also have done this:

sub _get_birthday { strftime '%m/%d/%Y', localtime( $_ ); # but NOT just localtime() }

But again, your tutorial has a nice build-up from simple to more advanced concepts, around a well-defined example (date storage). With some editing and clarifications from feedback here and elsewhere in the thread, it will probably be something I'm happy to either link to in the Class::InsideOut documentation or possibly even add as Class::InsideOut::Tutorial.

One thing you might want to clarify a bit more is the id() function and its importance in generating the unique ID for a object.

I also wouldn't describe Class::Std as "deprecated" -- I'm not a big fan, but it's certainly in use. It is very good for dealing with complex class hierarchies (as one would expect of a module by TheDamian) but I think it errs by not being more fundamentally robust.

My YAPC::NA 2006 presentation is another potential reference you could add.


Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

In reply to Re: RFR: Inside-out classes - a Class::InsideOut primer by xdg
in thread RFR: Inside-out classes - a Class::InsideOut primer by radiantmatrix

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 musing on the Monastery: (7)
    As of 2018-06-24 22:45 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.