Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Dynamic Class Loading, Compilation, & Speed

by jdrago999 (Pilgrim)
on Mar 07, 2010 at 02:21 UTC ( #827188=note: print w/ replies, xml ) Need Help??


in reply to Dynamic Class Loading, Compilation, & Speed

Try this:

#!/usr/bin/perl -w use strict; use warnings 'all'; # ...later... load_class("Some::Class"); sub load_class { my ($class) = @_; (my $file = "$class.pm") =~ s/::/\//g; unless( $INC{$file} ) { require $file; $class->import; }# end unless(); }# end load_class()

Just as use <ClassName> operates in compile-time, require operates in runtime without a string eval.

To finish the job, call import on the newly-loaded package and you're done.


Comment on Re: Dynamic Class Loading, Compilation, & Speed
Select or Download Code
Re^2: Dynamic Class Loading, Compilation, & Speed
by shmem (Canon) on Mar 07, 2010 at 19:40 UTC

    Nitpick... to handle the equivalent of use Module qw(foo => 'bar'); -

    # ...later... load_class("Some::Class", @load_args); sub load_class { my $class = shift; (my $file = "$class.pm") =~ s|::|/|g; unless( $INC{$file} ) { require $file; }# end unless(); $class->import(@_); }# end load_class()

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-04-17 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (451 votes), past polls