Is it kosher to assign to $! in Perl? - perl

Is it OK to assign to $! on an error in Perl?
E.g.,
if( ! (-e $inputfile))
{
$! = "Input file $inputfile appears to be non-existent\n";
return undef;
}
This way I can handle all errors at the top-level.
Thanks.

If you assign to $!, it is placed in the system errno variable, which only takes numbers. So you can in fact do:
use Errno "EEXIST";
$! = EEXIST;
print $!;
and get the string value for a defined system error number, but you can't do what you want - setting it to an arbitrary string. Such a string will get you a Argument "..." isn't numeric in scalar assignment warning and leave errno set to 0.
The other problem is that $! may be changed by any system call. So you can only trust it to have the value you set until you do a print or just about anything else. You probably want your very own error variable.

Well, the documentation says it's an indicator for system errors, basically. So I wouldn't assign to it, you just make your lib's users mad.
Use exceptions instead:
eval { # this ain't the evil eval
# some code
die $something;
}
if (my $err = $#) {
# exception handling
}
Note that you can "throw", or die, with about anything you need..

My Rabbi said "no!"

Setting $! is fine, but:
you should do it at the end of your function
you should return a different value that indicates that an error occurred
you should use your OS's errno values rather than strings for setting purposes
the checking code needs to check the value should do so immediately on failure of the function (and only if a failure is indicated by the function)
Something to keep in mind is that die uses the value in $! for its exit code (so long as it is not zero).

Yes you can assign stuff(#'s) to $!, just be wary of where you do it so you don't mess up some other functions message.

If you only have one variable to store errors, you'll have problems if you have more than one error occurring in your program before checking the status of your error variable. That's worth avoiding if you can help it.
Thankfully in Perl you can help it. A really nice solution is to use object-oriented exception handling from Error.pm. This module will allow you to write try/catch blocks, like this:
try {
some code;
code that might thrown an exception;
more code;
return;
}
catch Error with {
my $ex = shift; # Get hold of the exception object
# handle the exception;
};
The CPAN documentation for the module is quite good, and there is a Perl.com article on the subject too.

$! has so many caveats, being a global variable which lots of functions assign to (some of them C functions which Perl calls), that I would simply throw an exception (which in Perl means dying) and let the user trap it if they care. So instead of writing:
$obj->foo or die $!;
$obj->bar or die $!;
$obj->baz or die $!;
or even
$obj->foo or die $obj->error;
$obj->bar or die $obj->error;
$obj->baz or die $obj->error;
you can just write
$obj->foo;
$obj->bar;
$obj->baz;
and know that if there is an error you'll be informed of it. Also anyone above you will be informed and can trap it. Since that's the most common case make it happen without the user needing to remember it and type it over and over and over again.
Should you want to ignore or recover from the error, simply use eval BLOCK.
eval { $obj->foo }; # don't care if it works or not
eval { $obj->bar } or do { ...something when it doesn't work... };
Since this is the exceptional case it should be where the user has to remember to add more code and does more work.
Examples of this approach include DBI's RaiseError flag, which is only off by default for backwards compatibility, and the wonderful autodie module.

Related

How to set $! in perl

I would like to write some functions in perl that set $! in a similar way to the built in perl functions. When I try to do this it complains that 'Argument "Cannot create admin user" isn't numeric in scalar assignment'. I have tried googling this but unfortunately google won't watch on $! so results are difficult to come by.
if(!createUser('admin')) { print $!; }
sub createUser
{
my ($user) = #_;
if($user eq "admin")
{
$! = "Cannot create admin user";
return 0;
}
#create user here
return 1;
}
PS. I know about Try::Tiny etc but I want to maintain consistancy with the rest of the script, if I switch to try/catch I want to move the entire script across which I am not ready to do.
$! is a magic variable that corresponds to the numerical error codes of your operating system (errno in the Unix-y libc libraries, something else like GetLastError() from kernel32.dll in Windows).
You can only set it to a number. When Perl wants to use it in a string context, it converts it to an appropriate text error message.
Example:
$! = 4;
print $!, "\n";
print 0+$!, "\n";
Produces (on my system, anyway):
Interrupted system call
4
It is perfectly appropriate (if non-portable, since different systems are free to use different integer values to represent standard error codes) to set $! to correspond to some standard error on your system:
sub my_do_something_with_file {
my $file = shift;
if (! -f $file) {
$! = 2; # "No such file or directory" on my system
return;
}
...
}
But there is no facility to create your own custom error messages to be returned with $!.
As documented in perlvar, $! contains a C errno, which is a number. Textual Perl-level error messages appear in $#, which is usually set by die. I would not recommend setting it manually, not least because it's a transpackage global.
Just use Try::Tiny if that's what you need. If the rest of the script is written poorly, you don't gain much from being consistent with it.
You can set $! to a numeric value that happens to match a C errno value and that sets the expected string value. Unfortunately this is not very portable.
If $! was not a magic variable you could assign a dual numeric/string value using dualvar(), which is available in Scalar::Util, which is a core package.
use Scalar::Util qw(dualvar) ;
my $x = dualvar( 44, "Hello, world" );
print "string=$x, number=" . (0+$x), "\n"
But with $! only the number survived my prior attempts.

Why do I need to localize $# before using eval?

I'm aware of the fact that $# is a global variable, still I can't figure out why I need to localize it before using eval:
For instance:
eval { SOME_FUNC_THAT_MAY_DIE(); };
if ($#) {
print "An error occured!\n";
}
The only possible thing I can think of is, if some signal handler will call die at the same time I try to read $#, what am I missing here?
The reason to say local $# before calling eval is to avoid stepping on your caller's $#. It's rude for a subroutine to alter any global variables (unless that's one of the stated purposes of the subroutine). This isn't really an issue with top-level code (not inside any subroutine).
Also, on older Perl's, any eval called during object destruction would clobber the global $# (if the object was being destroyed because an exception was being thrown from an eval block) unless $# was localized first. This was fixed in 5.14.0, but many people are still running older Perls.
The Try::Tiny module documentation gives the rationale (as well as providing an alternative):
When you run an eval block and it succeeds, $# will be cleared, potentially clobbering an error that is currently being caught.
This causes action at a distance, clearing previous errors your caller may have not yet handled.
$# must be properly localized before invoking eval in order to avoid this issue.
More specifically, $# is clobbered at the beginning of the eval, which also makes it impossible to capture the previous error before you die (for instance when making exception objects with error stacks).
You don't need to, but if you wrote code like this, localizing $# would keep the first error as it was. and if you didn't write code like this, the local $# would have no effect. better would be to handle errors before running any extra code.
eval {
die "error 1\n";
};
foo();
print "processing $#\n";
sub foo {
#local $#;
eval {
die "error 2\n";
};
}

How can I prevent my perl script from terminating if an exception is thrown in a module it uses?

I have a perl script, using standard-as-dirt Net::HTTP code, and perl 5.8.8.
I have come across an error condition in which the server returns 0 bytes of data when I call:
$_http_connection->read_response_headers;
Unfortunately, my perl script dies, because the Net::HTTP::Methods module has a "die" on line 306:
Server closed connection without sending any data back at
/usr/lib/perl5/vendor_perl/5.8.8/Net/HTTP/Methods.pm line 306
And lines 305-307 are, of course:
unless (defined $status) {
die "Server closed connection without sending any data back";
}
How can I have my script "recover gracefully" from this situation, detecting the die and subsequently going into my own error-handling code, instead of dieing itself?
I'm sure this is a common case, and probably something simple, but I have not come across it before.
Using eval to catch exceptions can occasionally be problematic, especially pre 5.14. You can use Try::Tiny.
You can use eval { } to catch die() exceptions. Use $# to inspect the thrown value:
eval {
die "foo";
};
print "the block died with $#" if $#;
See http://perldoc.perl.org/functions/eval.html for details.
Customizing the die to mean something else is simple:
sub custom_exception_handler { ... } # Define custom logic
local $SIG{__DIE__} = \&custom_exception_handler; # Won't die now
# Calls custom_exception_handler instead
The big advantage of this approach over eval is that it doesn't require calling another perl interpreter to execute the problematic code.
Of course, the custom exception handler should be adequate for the task at hand.

Returning error messages from perl subroutines

Is this a good way of returning an error messages from a subroutine in perl?
sub some_subroutine{
# do something
$something = 14;
if(1 == 2){
$_ = "This should not be happening!";
return undef;
}
return $something;
}
my $ret=some_subroutine();
print "ERROR: $_" unless(defined $ret);
The code runs OK (in a parallel world, where 1 == 2), but using $_ to return the error message is a good way? I didn't found any documentation regarding the usage of $_ for this kind of purposes.
Thank you!
$_ is not a good mechanism as so many other things use and set it.
You could set some other global variable, like $Error, but the best way is to throw an exception. Then the user doesn't have to always check, and forget, it just happens. In Perl, exceptions are generated with "die". die will take a string or an object. There's a number of modules to make throwing exceptions easier including Exception::Class, Exception::Simple, Ouch and autodie.
You can catch the exception with eval {}, but there are a number of problems with that so you should use something like Try::Tiny.
It's not unknown to use a global variable to hold the error message. For example, built-in function use $!, and DBI uses $DBI::errstr. But then you inherit all the problems of using global variables. Signal handlers need to localise them, destructors can clobber them, multi-threading issues, etc.
It seems to me that throwing an exception (e.g. using die) is a more common choice.
Whatever you do, don't use $_. It is often aliased to something, so using it could have unintended consequences.
One thing you can do to make this slightly better is to simply return; on error/undefined result. There are contexts where return undef; can be evaluated as true. You might check out Perl Best Practices's chapter on error handling as it covers this and has other good pointers.
For example, if:
my $ret=some_subroutine();
print "ERROR: $_" unless(defined $ret);
becomes
my #ret=some_subroutine(); # oops!
print "ERROR: $_" unless(defined $ret);
You have a weird bug (not catching the error) that may be hard to track down, whereas if you return; from some_subroutine you get:
my #ret=some_subroutine(); # oops!
print "ERROR: $_" unless($ret); # but error is still caught
Furthermore, in the case of a function that should return a list:
my #ret=some_other_subroutine(); # OK
print "ERROR: $_" unless($ret); # error will be caught if you "return;"
# ... but later ...
my $ret=some_other_subroutine(); # oops!
print "ERROR: $_" unless($ret); # if you "return;" $ret will equal 0
# (the length of the returned list)
# and the error will be caught
So, while there are other patterns for returning error/undefined status from a function, always using return; allows the use of one pattern for nearly all functions, both in returning the appropriate value and when it comes time to check for error status.
It totally depends on what behavior you are expecting to see from your error handler. It is very common in perl to use die or warn in case a runtime error/warning happens.
Take a look at this tutorial on error handling in perl: http://www.tutorialspoint.com/perl/perl_error_handeling.htm
For a web service, you could write a subroutine for printing custom error responses (40x errors, etc.).

What are the best practices for error handling in Perl?

I'm learning Perl, and in a lot of the examples I see errors are handled like this
open FILE, "file.txt" or die $!;
Is die in the middle of a script really the best way to deal with an error?
Whether die is appropriate in the middle of the script really depends on what you're doing. If it's only tens of lines, then it's fine. A small tool with a couple hundred lines, then consider confess (see below). If it's a large object-oriented system with lots of classes and interconnected code, then maybe an exception object would be better.
confess in the Carp package:
Often the bug that led to the die isn't on the line that die reports.
Replacing die with confess (see Carp package) will give the stack trace (how we got to this line) which greatly aids in debugging.
For handling exceptions from Perl builtins, I like to use autodie. It catches failures from open and other system calls and will throw exceptions for you, without having to do the or die bit. These exceptions can be caught with a eval { }, or better yet, by using Try::Tiny.
Since I use Log::Log4perl almost everywhere, I use $logger->logdie instead of die. And if you want to have more control over your exceptions, consider Exception::Class.
It is better to catch your exceptions with Try::Tiny (see its documentation why).
Unless you've got a more specific idea, then yes you want to die when unexpected things happen.
Dying at the failure to open a file and giving the file name is better than the system telling you it can't read from or write to an anonymous undefined.
If you're talking about a "script", in general you're talking about a pretty simple piece of code. Not layers that need coordination (not usually). In a Perl Module, there is an attendant idea is that you don't own the execution environment, so either the main software cares and it catches things in an eval, OR it doesn't really care and dying would be fine. However, one you should try at a little more robustness as a module and just pass back undefs or something.
You can catch whatever dies (or croaks) in an eval block. And you can do your more specific handling there.
But if you want to inspect $! then write that code, and you'll have a more specific resolution.
Take a look at the near-universal standard of using strict. That's code that dies on questionable syntax, rather than letting you continue along.
So I think the general idea is: yes, DIE unless you have a better idea of how things should be handled. If you put enough foresight into it, you can be forgiven for the one or two times you don't die, because you know you don't need to.
The more modern approach is to use the Carp standard library.
use Carp;
my $fh;
open $fh, '<', "file.txt" or confess($!);
The main advantage is it gives a stack trace on death.
I use die but I wrap it in eval blocks for control over the error handling:
my $status = eval
{
# Some code...
};
If the 'eval' fails:
$status will be undefined.
$# will be set to whatever error message was produced (or the contents of
a die)
If the 'eval' succeeds:
$status will be the last returned value of the block.
$# will be set to ''.
As #friedo wrote, if this is a standalone script of a few lines, die is fine, but in my opinion using die in modules or require'd piece of code is not a good idea because it interrupts the flow of the program. I think the control of the flow should be a prerogative of the main part of the program.
Thus, I think, in module it is better to return undef such as return which would return undef in scalar context and an empty list in list context and set an Exception object to be retrieved for more details. This concept is implemented in the module Module::Generic like this:
# in some module
sub myfunc
{
my $self = shift( #_ );
# some code...
do{ # something } or return( $self->error( "Oops, something went wrong." ) );
}
Then, in the caller, you would write:
$obj->myfunc || die( "An error occurred at line ", $obj->error->line, " with stack trace: ", $obj->error->trace );
Here error will set an exception object and return undef.
However, because many module die or croak, you also need to catch those interruption using eval or try-catch blocks such as with Nice::Try
Full disclosure: I am the developer of both Module::Generic and Nice::Try.