I thought the magic <> was worth mentioning specifically because of the error message issue, not because I thought people might not be able to figure it out.
When using the locale module and POSIX::setlocale, the error message does not seem to change. I get the same when setting LANG, LC_CTYPE, and LC_MESSAGES in my environment then using the locale module.
perllocale says this:
The remaining locale category, "LC_MESSAGES" (possibly supplemented by others in particular implementations) is not currently used by Perl--except
possibly to affect the behavior of library functions called by extensions outside the standard Perl distribution and by the operating system and
its utilities. Note especially that the string value of $! and the error messages given by external utilities may be changed by "LC_MESSAGES".
If you want to have portable error codes, use "%!". See Errno.
There are options, though. You can use the Errno module to provide an error message in addition to the one Perl's going to emit, you can trap the warning and change the text that's emitted, or you can wrap the hash assignment/map/read in parentheses and use a logical or to emit an additional message.
use Errno;
...
my %skater_avg = map
{
chomp;
my( $skater, @scores ) = split /,/;
{ $skater => ( sum(@scores)-min(@scores)-max(@scores) ) / (@scores
+-2) }
}
<>;
warn "custom message!\n" if $!{ENOENT};
use Errno;
$SIG{__WARN__} = sub {
if ( $!{ENOENT} ) {
warn "custom message!\n";
}
};
( my %skater_avg = map
{
chomp;
my( $skater, @scores ) = split /,/;
{ $skater => ( sum(@scores)-min(@scores)-max(@scores) ) / (@scores
+-2) }
}
<> ) || warn "custom message!\n";
Update: made a doc link to Errno. Update 2:s/though/thought/ in that first sentence. |