> -----Original Message----- > From: Olivier Wirz [mailto:[EMAIL PROTECTED]] > Sent: Thursday, February 20, 2003 6:02 PM > To: [EMAIL PROTECTED] > Subject: Dynamic load of a Perl class > > > Hello, > > Ich would like to load dynamically a Perl class (late binding). > > For example, if I start a Perl script with a command line > parameter like --sport=SWIMMING, I would like to have > something like that (after having saved SWIMMING via > $opt_sport in variable $aSport): > > my $aSport = $opt_sport; > use Classes::$aSport; <----- doesn't work ! >
It's possible (see perldoc -f require) like this: eval "require Classes::$aSport"; if($@){ die $@; } But i'm personally avoid using this solution because of following reasons: * errors inside Classes::$aSport appear at run time not at compile time. If your script takes a longtime before executing the line eval "require Classes::$aSport"; Debugging becomes painful (I know what i'm saying :). * since the syntax is eval "" and not eval {}, this line will be re-compiled. So if you put the eval "" inside a loop, there will be a re-compilation for each iteration => Not elegant ! Alternatively, why not just write *all* possible cases: use Classes::aSport use Classes::Foo use Classes::Bar use Classes::BlaBla On other hand there should be an implementation of your Classes::XYZ, so you should know in advance which class can be use. The list can be huge but you will gain in term of maintenance. HTH, José. **** DISCLAIMER **** "This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer". Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]