Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: BEGIN and compile-time

by imp (Priest)
on Nov 10, 2006 at 19:17 UTC ( #583386=note: print w/ replies, xml ) Need Help??

in reply to Re^2: BEGIN and compile-time
in thread BEGIN and compile-time

It is more appropriate to test $@.

eval "use Foo::Bar"; if ($@) { warn "Eval failed: $@"; }

Comment on Re^3: BEGIN and compile-time
Download Code
Replies are listed 'Best First'.
Re^4: BEGIN and compile-time
by ikegami (Pope) on Nov 10, 2006 at 19:40 UTC

    I like

    eval { require Foo::Bar } or warn "Eval failed: $@";

    Since require only returns true or an exception. The import can be done later, although I usually don't want to import from a dynamically loaded module.

    Update: Oops, silly me! Replaced eval "" with eval {}. Thanks tye.

Re^4: BEGIN and compile-time ($@)
by tye (Sage) on Nov 10, 2006 at 19:39 UTC
    It is more appropriate to test $@.

    Oh, I disagree. There are too many cases where $@ doesn't survive. It is more fool-proof (and one of my "best practices") to check the return value of eval "....; 1" to determine whether or not eval failed (and then to use $@ to report what the error was, though $@ might no long contain this information).

    - tye        

      That's a good point.
      Could you provide an example of where a failed eval "use Foo" does not result in $@ being populated?

        It usually involves a "broken" object DESTROY method, usually one that uses eval but forgets to local( $@ ).

        - tye        

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-11-29 23:11 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (754 votes), past polls