View on
Philip Guenther > Cant > Cant



Annotate this POD


Open  0
View/Report Bugs
Module Version: 1.01   Source  


Cant - Perl extension for easy error messages


  use Cant;

  open(FOO, "foo")              or cant "open 'foo'";
  $pid = fork();
  defined $pid                  or cant "fork";
  ! system("some command")      or wcant "run 'some command'";

  print "Prepping system...";
  !system("foo")                or cant "\nrun foo";


The Cant module provides easy shorthands for warning and dieing of system or library errors. The messages generated by cant and wcant always begin with the program name (from $0), a colon, and then "Unable to" and the first argument to cant or wcant. As a result, you can usually write your cant invocations so as be sensical when read in the script and get sensical errors message out.

The exact message generated depends on the values of the $! and $? variables. The first match is choosen from the following possibilities:

The text from the above will be put after all other arguments to cant or wcant, unless the first argument contains a newline anywhere but at the very beginnning. If there is such a non-leading newline, the text will be inserted immeadiately after that newline.

Finally, the text " at filename line line\n" will be added at the very end containing the actual filename and line number of the call to cant or wcant.

If the first argument starts with a newline, that newline will be suppressed from its apparent place in the message and instead a newline will be put at the start of the message, before anything else. This allows you to force a leading newline when you think the message will be generated while they're already on the current line, as in the last example in the SYNOPSIS.


The formatting rules are too complicated.



Finally gave the wrapping of a module. The earliest version of cant() was simply sub cant { die "$0: Unable to", @_, ": $!" }

It evolved from there first to add handling of program errors ($?), then newline formatting singals, and finally the "trailing newline means no extra message". You've come a long way, baby...


If cant() or wcant() appears to be called from a package besides main they'll now invoke croak/carp instead of die/warn.


Philip Guenther,


Copyright (C) 1998-2000, Philip Guenther. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.



syntax highlighting: