Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P219 - Perl - Libraries and Resources
  [3377] What do I mean when I add things in Perl? - (2011-08-02)
  [3101] The week before Christmas - (2010-12-23)
  [3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
  [2234] Loading external code into Perl from a nonstandard directory - (2009-06-12)
  [2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
  [1865] Debugging and Data::Dumper in Perl - (2008-11-02)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
  [1219] Judging the quality of contributed Perl code - (2007-06-06)
  [760] Self help in Perl - (2006-06-14)
  [737] Coloured text in a terminal from Perl - (2006-05-29)
  [712] Why reinvent the wheel - (2006-05-06)
  [358] Use standard Perl modules - (2005-06-25)
  [357] Where do Perl modules load from - (2005-06-24)
  [112] Avoid the wheel being re-invented by using Perl modules - (2004-11-08)
  [86] Talk review - Idiomatic Perl, David Cross - (2004-10-12)

P711 - An Introduction to Standards in Perl
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [2875] A long day in Melksham ... - (2010-07-17)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [2375] Designing your data structures for a robust Perl application - (2009-08-25)
  [1853] Well structured coding in Perl - (2008-10-24)
  [1728] A short Perl example - (2008-07-30)
  [1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
  [1395] Dont just convert to Perl - re-engineer! - (2007-10-18)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [965] KISS - one action per statement please - Perl - (2006-12-05)
  [945] Code quality counts - (2006-11-26)
  [743] How to debug a Perl program - (2006-06-04)
  [668] Python - block insets help with documentation - (2006-04-04)
  [242] Satisfaction of training - (2005-03-11)


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
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
4759 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, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1863_Abo ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat