InfiniteSilence has asked for the wisdom of the Perl Monks concerning the following question:

Let's say you want to do the following on a Windows machine:
open(PIPE,"dir /od a: |")||die 'cannot pipe!'; my @dirs = <PIPE>; close(PIPE);
And there is nothing in the floppy drive? I get a horrible error that starts asking the incessant, "Abort, Retry, Fail" question on the command line and locks up the machine entirely. Anybody have a way to detect whether the floppy drive is in a state of readiness for read/write?

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
(tye)Re: Detecting drive unavailability on ActivePerl
by tye (Sage) on Apr 30, 2001 at 19:15 UTC

    You can use Win32API::File (which should be included with most versions of Perl) to control this. See the module's documentation on SetErrorMode. Since the API is rather broken (Microsoft's fault), here is a sample usage:

    #!/usr/bin/perl -w use strict; use Win32API::File qw(:ALL); { my $mode= SetErrorMode( 0 ); $mode |= SEM_FAILCRITICALERRORS; SetErrorMode( $mode ); } opendir( DIR, "a:/" ) or die "Can't read directory from a:/; $!\n"; my @files= readdir(DIR); closedir(DIR);
    Note that this example uses opendir rather than reading the output of "dir /od a:" which can often be a better idea.

            - tye (but my friends call me "Tye")
      This works like a charm. Thanks.

      Celebrate Intellectual Diversity

Re: Detecting drive unavailability on ActivePerl
by the_slycer (Chaplain) on Apr 30, 2001 at 20:14 UTC
    Just an observation.
    I tried a couple of different things using 2k with regards to this.

    With opendir (DIR, "a:") || die "could not access floppy: $!"; It prompts me twice to insert a disk and then dies gracefully - I would assume on 9x or NT it would give the abort retry fail error 2 times.

    With open (PIPE, "dir /od a: |") || die "Could not access floppy: $!"; It does not die, and gives me an error message after the script exits.

    Useless info, as you already have an answer, but interesting. I'd be curious to see whether the opendir function behaves better for you.
Re: Detecting drive unavailability on ActivePerl
by THRAK (Monk) on Apr 30, 2001 at 19:17 UTC
    I don't have a Windows specific way of helping you out, but I do have another idea. I'm working on the assuption that this is an interactive things being you are accessing a floppy otherwise you can stop reading now! Maybe instead of just dying if the pipe can't be opened, trap the error and send it to a sub-routine that prompts the user to insert/check the floppy etc. If it fails a second (or some number of) times then exit with a nice "Can't access your floppy" message.

    -THRAK
    www.polarlava.com
      No, no. You cannot recover from the pipe by changing how the open routine dies. You don't have that luxury. The lockup is immediate. You get "Abort, Retry, Fail" prompts at the command line. Regardless of what you type, your machine is going to either lock up completely or stay in that mode forever.

      Celebrate Intellectual Diversity