in reply to $class = ref $class || $class
in thread Constructor/Factory Orthodoxy

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

Replies are listed 'Best First'.
Re: Re: $class = ref $class || $class
by rir (Vicar) on Feb 27, 2003 at 21:56 UTC
    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)

        @INC is only consulted when doing a use or require. It's not consulted for object or method calls.

        Don't expect there to be much difference in speed between $obj -> new and Class -> new. The overhead of calling the method dwarves any finding of the class.

        Abigail

        Most of the code I've seen with this construct in new does not have the usage you describe as a justification. And I don't find your justification compelling where it does apply.

        I would prefer to write (ref $obj)->new instead of $obj->new because that tells the reader that the type of $obj is variable or questionable. If I write Obj->new there is no doubt of the type. There is less chance the reader will research the wrong package if he wants to do something with the created object.
        If I just write $obj->new as a convenient shorthand I hide this info. Thus I make the code more complicated to understand.