Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

exit or die in error handling?

by Anonymous Monk
on Apr 14, 2005 at 10:09 UTC ( #447683=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I have a doubt, I am posting my code here.
#!/usr/bin/perl $type = $ARGV[0]; $argument = $ARGV[1]; if( $type eq 'job' || $type eq 'member' || $type eq 'file' ) { print "yes..."; } else { print "The parameters for this program are as follows: [job/member/fil +e] [jobnumber/membernumber/filename]"; die; }

or in else condition shall I use
else { print "The parameters for this program are as follows: [job/member/fil +e] [jobnumber/membernumber/filename]"; exit; }

Which one is the best way? (Memory management point of view).
Regards,
John

Edited by Arunbear: Changed title from 'Provide your valuable suggestion', as per Monastery guidelines

Replies are listed 'Best First'.
Re: exit or die in error handling?
by monkey_boy (Priest) on Apr 14, 2005 at 10:16 UTC
    I dont think "Memory management" has anything to do with it.
    They both end the program, however exit() without an argument returns 0 (which is true in the *nix world).

    also, errors should be printed to STDERR not SDTOUT.
    So.. third option..
    else { die("The parameters for this program are as follows: [job/member/fil +e] [jobnumber/membernumber/filename]"); }



    This is not a Signature...
      They both end the program,

      I think the most important difference is that die can be caught by an eval (it is Perl's way of doing exceptions) in other parts of the program, so it does not necessarily end the program. Using die thus makes code more easily reusable, and is a good habit to get into.

Re: exit or die in error handling?
by myuserid7 (Scribe) on Apr 14, 2005 at 10:15 UTC
    Use "exit" if it's not an error. Use "die" if it is. You don't want to worry about memory management for simple things like this - think programmer efficiency!
Re: exit or die in error handling?
by QM (Parson) on Apr 14, 2005 at 15:30 UTC
    I would suggest also looking at one of the Getopt::* modules on how to manage command line parameters (Getopt::Long, or my current favorite, Getopt::Declare). There is also the -s command line switch for parsing command line options.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: exit or die in error handling?
by Taulmarill (Deacon) on Apr 14, 2005 at 10:17 UTC
    depends on what you want to do. die gives an error message to STDOUT and is used if there is an error. exit on the other hand just ends the script.
    see perldoc -f die and perldoc -f exit
      "die gives an error message to STDOUT"

      No it doesnt! STDERR


      This is not a Signature...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://447683]
Approved by ghenry
help
Chatterbox?
[erix]: ah, pcregrep is a stanalone program?
[Lady_Aleena]: erix, yes.
[Lady_Aleena]: pcre stands for 'Perl Compatible Regular Expressions'.
[erix]: ok, ok, I thought you meant the engine
[perldigious]: Tanktalus: Ha, understandable, that does make it harder when you don't want to move. I assume you are looking at remote work from home jobs too?
[Tanktalus]: yeah, I'm looking at a lot of remote work - I've been working from home since 2002, so I know how to do the work from remote, I just need a new employer ;)
[Lady_Aleena]: Discipulus, I wouldn't know how to get a script to do what the greps do in one line.
[perldigious]: Even working for a large software engineering contractor that's willing to do remote could be an option. The fortune 500 company at $job-- I worked for had quite a lot of that work to hire out to large contractors.
[perldigious]: The pay and benifits leave a lot to be desired though, or so I've heard...
[Lady_Aleena]: Discipulus, grep doesn't have an option to go recursively through a directory as far as I know.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2017-05-23 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?