Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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: $@"; }

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 chanting in the Monastery: (10)
As of 2016-10-21 14:14 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (289 votes). Check out past polls.