Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: A Class inside a Perl script?

by arc_of_descent (Hermit)
on Jun 02, 2009 at 09:24 UTC ( #767518=note: print w/ replies, xml ) Need Help??


in reply to A Class inside a Perl script?

You're package is named test, but you are blessing into Person.

You should use the following constructor in your packages, which takes care of inheritance too.

sub new { my $proto = shift; my $class = ref $proto || $proto; my $self = {}; # ... # ... bless $self, $class; return $self; }

--
Rohan


Comment on Re: A Class inside a Perl script?
Download Code
Re^2: A Class inside a Perl script?
by GrandFather (Sage) on Jun 02, 2009 at 11:37 UTC
    my $class = ref $proto || $proto; ... bless $self, $class;

    Don't do that. It generates a function/method that implies both constructor and clone semantics. new should be a class function that generates a new initialised object. An explicit copy or clone method (not class function) should be used to generate a cloned instance of an existing object. new() function: incorrect? and its replies discuss the topic at some length.


    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2015-07-05 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls