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
P711 - An Introduction to Standards in Perl [242] Satisfaction of training - (2005-03-11)
[668] Python - block insets help with documentation - (2006-04-04)
[743] How to debug a Perl program - (2006-06-04)
[945] Code quality counts - (2006-11-26)
[965] KISS - one action per statement please - Perl - (2006-12-05)
[1047] Maintainable code - some positive advice - (2007-01-21)
[1221] Bathtubs and pecking birds - (2007-06-07)
[1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
[1395] Dont just convert to Perl - re-engineer! - (2007-10-18)
[1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
[1728] A short Perl example - (2008-07-30)
[1853] Well structured coding in Perl - (2008-10-24)
[2375] Designing your data structures for a robust Perl application - (2009-08-25)
[2688] Security considerations in programming - what do we teach? - (2010-03-22)
[2875] A long day in Melksham ... - (2010-07-17)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
P219 - Perl - Libraries and Resources [86] Talk review - Idiomatic Perl, David Cross - (2004-10-12)
[112] Avoid the wheel being re-invented by using Perl modules - (2004-11-08)
[357] Where do Perl modules load from - (2005-06-24)
[358] Use standard Perl modules - (2005-06-25)
[712] Why reinvent the wheel - (2006-05-06)
[737] Coloured text in a terminal from Perl - (2006-05-29)
[760] Self help in Perl - (2006-06-14)
[1219] Judging the quality of contributed Perl code - (2007-06-06)
[1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1444] Using English can slow you right down! - (2007-11-25)
[1865] Debugging and Data::Dumper in Perl - (2008-11-02)
[2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
[2234] Loading external code into Perl from a nonstandard directory - (2009-06-12)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
[3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
[3101] The week before Christmas - (2010-12-23)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
Some other Articles
Domain Renewal GroupWhat a difference a day madeObject Oriented Perl - First StepsAbout dieing and exiting in PerlRemember your unitsReactive (dynamic) formatting in PerlSeven new intermediate Perl examplesWiltshire at dawn - the tourist trailCamera with night vision, youth with no vision