distinguish $! vaules

P

Peter Michael

Hi,

what is currently the preferred way to distinguish between different
values of $! ? I suppose that %! was created to this end (sample code?)
but should I use Switch(3) instead today?

use Errno qw:)POSIX);
use Switch;

open my $fh, "file" or do
{ switch($!)
{ case ENOENT { warn "you should first create the file\n"; }
case EACCES { warn "you are not allowed to see this\n"; }
else { warn "some other error...\n"; }
}
};

Any hints welcome (Anno? ;-).

Best regards,

Peter
 
A

Anno Siegel

Peter Michael said:
Hi,

what is currently the preferred way to distinguish between different
values of $! ? I suppose that %! was created to this end (sample code?)
but should I use Switch(3) instead today?

use Errno qw:)POSIX);
use Switch;

open my $fh, "file" or do
{ switch($!)
{ case ENOENT { warn "you should first create the file\n"; }
case EACCES { warn "you are not allowed to see this\n"; }
else { warn "some other error...\n"; }
}
};

These are really two questions. One is how to get at the official
name of an error. Here the only answer is "use Errno". The error
numbers (the numeric side of $!) and the error texts (the string side
of $!) are can vary from system to system.

The other question is how to branch on conditions (whether these are
error conditions ore something else). The Switch module can be used
for that, but is not really necessary. There are many other branching
methods in Perl. The program snippet above could also be written:

use Errno qw( :pOSIX);

my %msg = (
ENOENT() => "you should first create the file\n",
EACCES() => "you are not allowed to see this\n",
default => "some other error...\n",
);
my $file = 'gibsnich/wirdnix';
open my $fh, $file or warn $msg{ $! + 0} || $msg{ default};

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top