Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Help creating a function

by tospo (Hermit)
on Feb 16, 2011 at 11:01 UTC ( [id://888493]=note: print w/replies, xml ) Need Help??


in reply to Help creating a function

I think you are looking for a factory pattern. Here is a suggestion:
package People; sub new { my ($classname, $type, $data_ref) = @_ ; my $class = $classname . '::' . $type ; use $class; return $class->new( $data_ref ) ; } # new ####################### package People::Employee; sub new{ ... } sub name{...} ... ###################### In a script far, far away: use People; my $employee = People->new( 'Employee', {name => 'Someone',...}); print $employee->name;

Replies are listed 'Best First'.
Re^2: Help creating a function
by varghees (Novice) on Feb 17, 2011 at 05:35 UTC
    Yes. I am looking for factory pattern. But this code
    my $class = $classname . '::' . $type ; use $class;
    is not working in perl. Throws syntax error.
        I tried Module::Load also. but no luck
        use Data::Dumper; print Dumper {A=>'testA',B=>'testB'};
        using 'use' statement as above code, I get the output as
        $VAR1 = { 'A' => 'testA', 'B' => 'testB' };
        but when i try loading the module using Module::Load as below, prints nothing.
        use Module::Load; load 'Data::Dumper'; print Dumper {A=>'testA',B=>'testB'};
      oops sorry, yes that was wrong. Actually what I normally do is this:
      $class=~s/:{2}/\//g; $class.= '.pm'; require $class_path;
      But there is probably a better way of doing it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found