Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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
Re^4: BEGIN and compile-time ($@)
by tye (Cardinal) 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        

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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2015-07-04 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls