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

Re: eval "require $class" seems wrong

by jZed (Prior)
on Aug 22, 2007 at 17:37 UTC ( [id://634440]=note: print w/replies, xml ) Need Help??


in reply to eval "require $class" seems wrong

eval { require $class }; :-)

update:

eval { require "$class.pm" }; :-)

Replies are listed 'Best First'.
Re^2: eval "require $class" seems wrong
by rvosa (Curate) on Aug 22, 2007 at 17:43 UTC
    That's not the same thing! Compare:
    perl -Mstrict -e 'my $c="CGI";eval "require $c";die $@ if $@' perl -Mstrict -e 'my $c="CGI";eval {require $c};die $@ if $@'
Re^2: eval "require $class" seems wrong
by rvosa (Curate) on Aug 22, 2007 at 18:03 UTC
    Ah, the update works. I'd ++ that if I had votes left for today :)

      Then you are violating a different "best practice": Modules should always have a "::" in their name.

      - tye        

        Okay, well, so in the general case (including '::' in module names), the non-string-eval way to do it seems to be:
        # turn $class name into $path my $class = 'Some::Class'; my $path = $class; $path =~ s/::/\//g; $path .= '.pm'; # check if $path already loaded if ( not exists $INC{$path} ) { # do block eval on $path eval { require $path }; die "Can't load $class: $@" if $@; } # if we make it here, we can use $class my $obj = $class->new;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found