Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
$class = $in || ref($in);

Yeah its right.. but why? when? where? If you shrug don't use it. How many modules will break for no apparent reason due to that one singular line?

I don't think that would be right anywhere, and certainly not in a consturctor. Perhaps you meant the opposite?

my $class=ref($proto) || $proto;

which AFAICT, wouldnt break any code at all. Why would it? The primary objection to this idiom (assuming you mean when it is used in a new()) is one of semantic meaning. Class->new() has a clear meaning, but $obj->new() doesn't really. Does it mean to copy the object? Does it mean to create a new object of the same type as the old? Does it mean something deeper?

This is primarily an issue that detailed documentation resolves nicely. I would say that you could replace my $class=shift with my $proto=shift; my $class=ref($proto) or $proto; just about everywhere without a failure.

One trap with using this idom (and about the only real trap regarding that I am aware of) is when someone accidentally mixes indirect notation with direct notation, or calls new in a strange way

my $Obj=new Class->new(@args); my $New=Class->new(@argS)->new()->foo(); # probably an error

most likely will create an initialized object, then use that to create an unitialized object. A hard to track down error that is thankfully rather uncommon as well.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: Re: Re: Re: Philosophical Perly Queues by demerphq
in thread Philosophical Perly Queues by mojotoad

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, 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2024-07-25 03:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.