Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

(jeffa) Re: Advice for moving forward with modules

by jeffa (Bishop)
on Feb 27, 2003 at 23:15 UTC ( #239288=note: print w/replies, xml ) Need Help??

in reply to Advice for moving forward with modules

When you call die from inside a module, you should probably be using croak instead (and carp instead of warn). These two methods are available from Try this little exercise, save the following as
package Foo; use Carp qw(croak); use base qw(Exporter); our @EXPORT_OK = qw(gonna_croak gonna_die); sub gonna_die { die "ya got me!" } sub gonna_croak { croak "ya got me!" } 1;
Now use that module with the following one-liners:

$ perl -MFoo=gonna_die -le gonna_die
ya got me! at line 7.

$ perl -MFoo=gonna_croak -le gonna_croak
ya got me! at -e line 1
die reports the error from the actual line it was executed on, which might be many packages deep. croak, on the other hand, reports the error from the perpestive of the caller, which is usually more useful to the end user. The end user usually cares more about which line of their code caused the problem, not which line in the module it affected.


(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Advice for moving forward with modules
by tachyon (Chancellor) on Feb 28, 2003 at 00:16 UTC

    I like the full confession myself rather than croak. Sure you get some cake you may not want to eat but you get the whole cake!

    use Carp; hello(); sub hello { again() } sub again { my_friend() } sub my_friend { I() } sub I { called() } sub called { to_say()} sub to_say { confess('Goodbye, I died') } __DATA__ Goodbye, I died at script line 10 main::to_say() called at script line 9 main::called() called at script line 8 main::I() called at script line 7 main::my_friend() called at script line 6 main::again() called at script line 5 main::hello() called at script line 3




Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://239288]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-12-11 15:21 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (305 votes). Check out past polls.