Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > distinguish $! vaules

Reply
Thread Tools

distinguish $! vaules

 
 
Peter Michael
Guest
Posts: n/a
 
      06-30-2004
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(OSIX);
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

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      07-01-2004
Peter Michael <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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(OSIX);
> 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( OSIX);

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to distinguish which button was clicked? Shapper ASP .Net 1 06-19-2005 10:01 PM
Distinguish vty sessions on routers. AM Cisco 1 05-20-2005 06:16 PM
FileSystemWatcher - distinguish file and folder events? Jon Maz ASP .Net 3 10-20-2004 05:23 PM
Distinguish text URLs from non-text URLs? Kaidi Java 5 01-04-2004 10:15 AM
Distinguish Timer (in Swing) event among the others fix Java 7 11-20-2003 11:27 PM



Advertisments