I finally figured out why my script testing on $! after provoked errors produced strange results.

Seems like $! is only correct if it's reset to undef before the error occurs otherwise the last state is used.

I couldn't find any documentation for this behaviour:

These are my results:

------ Error: off --- Reset: off $! 0: --- Reset: on $! 0: --- Reset: off $! 17: File exists --- Reset: on $! 0: --- Reset: off $! 17: File exists --- Reset: on $! 0: ------ Error: on --- Reset: off $! 17: File exists --- Reset: on $! 9: Bad file descriptor --- Reset: off $! 9: Bad file descriptor --- Reset: on $! 9: Bad file descriptor --- Reset: off $! 9: Bad file descriptor --- Reset: on $! 9: Bad file descriptor

use strict; use warnings; $|=1; my $dh; sub tst { my ($mk_error, $reset_errno)=@_; mkdir "/tmp/bla"; opendir $dh, "/tmp/bla" or warn "$!"; rmdir "/tmp/bla" if $mk_error; $!=undef if $reset_errno; my $a= scalar readdir($dh); print "\n\$! ",$!+0,": $!\n" ; } for my $mk_error (0,1) { print "\n\n------ Error: ", (qw(off on))[$mk_error] ; for my $reset_errno (0,1,0,1,0,1) { print "\n--- Reset: ", (qw(off on))[$reset_errno] ; tst($mk_error,$reset_errno) ; } }

would be nice to know if versions > 5.10 show the same problems

perl -version This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

UPDATE
$! 17: File exists stems from former mkdir "/tmp/bla"; where the directory already existed.

Cheers Rolf

( addicted to the Perl Programming Language)


In reply to Re: Testing for readdir failure (Perl-bug) by LanX
in thread Testing for readdir failure by Bob Cook

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.