Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Perl "new" command

by JavaFan (Canon)
on Mar 01, 2012 at 10:00 UTC ( #957143=note: print w/replies, xml ) Need Help??

in reply to Re: Perl "new" command
in thread Perl "new" command

Actually, constructors cannot be named new. The name of the constructor in Perl is called bless.

Unless you make an XS function called new which reimplements bless.

Replies are listed 'Best First'.
Re^3: Perl "new" command
by nemesdani (Friar) on Mar 01, 2012 at 10:09 UTC
    I thought a constructor is any routine that calls the bless operator.
      That would be a silly and confusing definition. If you had:
      sub foo { my $x = bless [], "X"; my $y = bless {}, "Y"; ... return 1; }
      Is that a double constructor? Half a constructor?
        bless attaches a method dispatch table to some Perl data structure. That's not what most people would consider a constructor.

        In most programming languages the constructor is some function that gets called once the object has been allocated in memory in order to have its attributes initialized. Setting its method dispatching table is something done by the language runtime under the hood, not inside the constructor.

        Perl leaves to the programmer the task of allocating the object storage, setting the dispatch table and initializing the object for greater flexibility.

        In my opinion, a constructor in Perl can be any function or code fragment returning a new object in an usable state. For some simple cases, just a bless {}, $class can qualify as such, but the usual thing is to use a method for that and to call it "new".

        Well, if I had to, I'd name it a multiple constructor. I would find it a "silly and confusing" way to do things ;). I read that definition in the Perl documentation by the way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://957143]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-04-23 00:29 GMT
Find Nodes?
    Voting Booth?