distinguish $! vaules

Discussion in 'Perl Misc' started by Peter Michael, Jun 30, 2004.

  1. 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
     
    Peter Michael, Jun 30, 2004
    #1
    1. Advertising

  2. Peter Michael

    Anno Siegel Guest

    Peter Michael <> 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:)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
     
    Anno Siegel, Jul 1, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    3
    Views:
    7,630
    Jon Maz
    Oct 20, 2004
  2. Shapper
    Replies:
    1
    Views:
    367
    Craig Deelsnyder
    Jun 19, 2005
  3. fix
    Replies:
    7
    Views:
    381
  4. Kaidi
    Replies:
    5
    Views:
    485
    Andrew Thompson
    Jan 4, 2004
  5. ruds
    Replies:
    3
    Views:
    340
    Arne Vajhøj
    Jun 1, 2013
Loading...

Share This Page