Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This is my first meditation (well online meditation anyway), so please forgive me if it isn't up to the usual Monk standards, or if it turns out to be total non-sense... (I am after all only a simple scribe)

More and more I am building my Perl applications in a nice an clean OO way, with constructors, inheritance, prototypes, you name it. Lately I'm finding myself repeating the same few lines of code in every single instance method;

sub foo { my $self = shift; croak "I am an instance method" unless $self and ref $self and $self->isa(__PACKAGE__); # actual body of code }
And the same with 'ref' negated for each class method. This has become my generic valid invocation checker... I know 'ref' should probably be 'blessed', but my Perl knowlegde may be a bit out of date. For class methods, $self should in fact probably be checked with /\W/, and possibly another check to see if the namespace is defined.

I was wondering if it might make sence to add 'modifiers' to the 'sub' keyword that would indicate to the interpreter to do checks like this... Something like:

method sub {} # $SELF=shift, check $SELF & isa instance method sub {} # =method modifier + ref $SELF class method sub {} # =method modifier + !ref $SELF & /\W/ & de +fined namespace

The 'method' modifier would setup $SELF automagically for instance. This would 'standardize' the syntax used in method code, while still allowing 'old-fashioned' code.

Maybe I've been working in Java too much, but this kind of run-time checking would greatly enhance my coding experience I think...

Naturally you guys will tell me that Perl 6 has all these things, and I will be shamed for not knowing enough about Perl 6... :)

In reply to OO-style modifiers for 'sub' ? by Gilimanjaro

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 a moth chases the moon...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2018-03-19 01:59 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (231 votes). Check out past polls.