Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
We don't bite newbies here... much
 
PerlMonks  

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.


Comment on Re^2: Perl "new" command
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-04-17 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (444 votes), past polls