Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

$class = ref $class || $class

by rir (Vicar)
on Feb 26, 2003 at 21:58 UTC ( [id://238931]=note: print w/replies, xml ) Need Help??


in reply to Re4: Constructor/Factory Orthodoxy
in thread Constructor/Factory Orthodoxy

I've never considered $foo->new() to indicate cloning, but rather a better looking way to say (ref $foo)->new().

I accept the $class = ref $class || $class as a Perl-specific idiom and so don't find it confusing. It is a bad idiom though. In itself it reads as nonsense: class is the ref of class or class. Worse is the habit that you have acquired: to think that $foo->new; is shorthand for (ref $foo)->new;. This is bad because they don't mean the same thing unless you know the internals of new. Also if you don't know the type of $foo you should explicate this fact with ref $foo not leave the issue in doubt.

As this use is idiomatic Perl it is not too confusing once you realize a codebase uses the idiom. However it is sloppy, it spreads in a codebase and is some work to undo. I write something inside my new (for no reason) that allows you to imply different meanings (for no reason) through the different usages that I allowed (for no reason). Sloppy.

It buys nothing. It will give pause to the better OO programmers who are new to Perl.

Replies are listed 'Best First'.
Re: $class = ref $class || $class
by herveus (Prior) on Feb 27, 2003 at 20:16 UTC
    Howdy!

    I usually take 'ref $class || $class' to allow me to use an extant object to call the constructor instead of uaing a class name.

    Of course, we always check the documentation before assuming we know how the module works, right? And we module authors make clear how one is expected to call the constructor (and any other methods) in our docs, right?

    yours,
    Michael

      I usually take 'ref $class || $class' to allow me to use an extant object to call the constructor instead of uaing a class name.

      Sure. I've used and written such myself. I wrote such code only because I was following the examples given me in the docs. No other reason.

      The point is Why? Why do you let users of your package use objects to call constructors? Why do you want to be able to write $obj->new vs Obj->new? What is the benefit of this more complicated and slower code?

        Howdy!

        Why? Re: Re5: Constructor/Factory Orthodoxy by steves spoke to this.

        For myself, if I have a bunch of classes that inherit from a base class that provides the constructor (or at least a default constructor), and I want "another one of these", where I don't necessarily know exactly what package "this" is blessed in to, I either have to do ref($this) everywhere I do this, or just expect that $this can(new) and let new figure it out.

        Sure, if I call $obj->new, Perl may have to dredge through @INC@ISA to find the method, where Class->new is explicit, but that may force me (the lazy programmer) to have to discern Class from $obj every time. If I bury that discovery in new, I only have to do it in one place.

        I'm not clear on what you mean by "more complicated and slower". Well, I can see the "slower" part, but "more complicated"?

        yours,
        Michael

        Update: corrected erroneous reference to @INC (where I should have said @ISA) (slaps forehead)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://238931]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found