Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
About dieing and exiting in Perl

If you want to end a program in Perl, you can get out quickly enough with an exit function call. But that's probably just a part of what you'll be wanting to do - you'll be wanting to generate an error message on the error channel (STDERR) too, and perhaps to tell your user rather more about why the program died.

The die function provides a message on STDERR and the exits your program. If you add a \n on as the last character, it will NOT tell you the line number in your code where it died, but with the \n it does tell you.

Note that if you have your die function tell you the line number, it will also tell you how many lines have been read from the latest file. At first, this will seem odd but it's very useful indeed in helping to identify which line of a long data file caused your program to halt!

Some useful variables to use within die strings:
$0 - program name
$. - number of lines of data read in
$! - error message from open function

If you're writing a Perl module, you'll also want to have a look at croak within the Carp module, as this reports information about where you were in the calling module rather than the actual line number of failure ... which when you think about it is exactly what the programmer who's making use of you module wants to know.

Some sample code:

@ARGV < 2 and die ("Usage: $0 perlregex filename\n");
($lookfor, $file) = @ARGV;
open (FH,$file) or die ("$0: Input file $file problem\n$!");
while (<FH>) {
  if (/$lookfor/)
    {die("Data trap");}
  }
print "Completed Correctly\n";


And some sample output from it:

Dorothy:p82 grahamellis$ perl chink
Usage: chink perlregex filename
 
Dorothy:p82 grahamellis$ perl chink Java ../requst.xyz
chink: Input file ../requst.xyz problem
No such file or directory at chink line 33.
 
Dorothy:p82 grahamellis$ perl chink Java ../requests.xyz
Data trap at chink line 35, <FH> line 2.
 
Dorothy:p82 grahamellis$ perl chink Javascript ../requests.xyz
Completed Correctly


I have indexed a copy of this code for your use under our Perl Standards module. In Perl, there are lots of different ways to do almost anything - and it's vital to come up with a concensus approach that's best for your organisation rather than using a bit of everything ... so I'll talk about Perl coding standards - how to write maintainable Perl - on every course I give from learning to program in Perl right through to Perl for larger projects

You can see the first few lines of each data file that we use in our examples in our Data directory. For copyright reasons, a few of the data files we use on our courses cannot be publicly downloaded; if you are a past delegate who would like a complete copy of any of the data files rather than just a sample of a few lines, please let me know.
(written 2008-11-01 06:36:04)

 
Associated topics are indexed under
P219 - Perl - Libraries and Resources
P711 - An Introduction to Standards in Perl

Back to
Remember your units
Previous and next
or
Horse's mouth home
Forward to
Object Oriented Perl - First Steps
Some other Articles
Domain Renewal Group
What a difference a day made
Debugging and Data::Dumper in Perl
Object Oriented Perl - First Steps
About dieing and exiting in Perl
Remember your units
Reactive (dynamic) formatting in Perl
Seven new intermediate Perl examples
Wiltshire at dawn - the tourist trail
Camera with night vision, youth with no vision
1975 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho