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

(jeffa) Re: Advice for moving forward with modules

by jeffa (Chancellor)
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 Carp.pm. Try this little exercise, save the following as Foo.pm:

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 Foo.pm 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.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)


Comment on (jeffa) Re: Advice for moving forward with modules
Download Code
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

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-11-25 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (148 votes), past polls