Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Calling a class constructor as a function on input arguments

by tye (Cardinal)
on Jul 21, 2000 at 18:41 UTC ( #23586=note: print w/ replies, xml ) Need Help??


in reply to Calling a class constructor as a function on input arguments

This sounds like a job for anonymous subs:

my %constructor= { MSWin32 => sub { FTP::Session::Win32->new( @_ ) }, HPUX => sub { FTP::Session::HPUX->new( @_ ) }, } my $ctor= $constructor{$^O}; die "No constructor for $^O" if ! $ctor; my $session= $ctor->( ... ); # Old Perl requires: $session= &$ctor( ... );

The next best thing is UNIVERSAL::can(), which returns a reference to a named method. But then you need to cache each class name so that you can pass that in to the sub that you got a reference to:

my %class= { MSWin32 => "FTP::Session::Win32", HPUX => "FTP::Session::HPUX", } my %constructor= { MSWin32 => FTP::Session::Win32->can("new"), HPUX => FTP::Session::HPUX->can( "new" ), } my $ctor= $constructor{$^O}; die "No constructor for $^O" if ! $ctor; my $session= $ctor->( $class{$^O}, ... ); # Old Perl requires: $session= &$ctor( $class{$^O}, ... );


Comment on Re: Calling a class constructor as a function on input arguments
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2015-07-06 02:26 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 (69 votes), past polls