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

Re: How do I dynamically declare a package name for

by gildir (Pilgrim)
on Apr 27, 2001 at 13:36 UTC ( [id://76029]=note: print w/replies, xml ) Need Help??


in reply to How do I dynamically declare a package name for

I use this for long-running mod_perl modules:
my $class = fetch_from_somewhere; my $object; eval { $object = $class->new; }; if ($@) { eval ("use $class"); die ($@) if $@; $object = $class->new; }
This will include $class.pm only once, the first time it is refferenced. Ideal for mod_perl modules. So performance penalty of compiling "use $class .." code is suffered only once.

Replies are listed 'Best First'.
Re: Re: How do I dynamically declare a package name for
by hdp (Beadle) on Apr 27, 2001 at 16:09 UTC
    This is actually unnecessary. Both use and require will look in %INC to make sure the file has not yet been included before loading it. (use will still call import again unless you explicitly pass it an empty list of symbols to import.)

    hdp.

      Yes, but there is big difference when calling:
      eval { $object = $class->new; };
      and eval " $object = $class->new; "; In the former case, code is compiled as soon as it is first encountered by perl compiler, that is at compile time and it is compiled only once. While in the later case the code is compiled at run-time and is compiled every time the eval() is run.
      There is no way to do eval { use $class; }; so I must use eval " use $class; "; version, and I try to execute it as seldom as possible.

Log In?
Username:
Password:

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

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

    No recent polls found