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

Hi, I have a little problem, when running my perl script under win XP. When I use the file test -e with a path from a CD-ROM drive and no disk is inserted, I get a windows error dialog. How can I prevent this to happen ?
  • Comment on Prevent windows dialog when using file test -e

Replies are listed 'Best First'.
Re: Prevent windows dialog when using file test -e
by BrowserUk (Patriarch) on Jun 22, 2006 at 10:46 UTC

    You can control the display of error dialogs through the API SetErrorMode(). Using Win32::API::Prototype it looks like this. See the link for possible values of the argument and note that they are bit flags that can be or'd together.

    #! perl -slw use strict; use Win32::API::Prototype; ApiLink( 'Kernel32', q[ UINT SetErrorMode( UINT uMode ) ] ); SetErrorMode( 0x8000 ); ## Updated per ikegami's correction below. if( -e $ARGV[ 0 ] ) { print 'Found it?' } else { print 'Not found'; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks that was a good hint,

      I use Win32::API instead and the uMode 8 doesnt fix my problem. Until now I have to use :
      if ($^O eq 'MSWin32') { require Win32::API; Win32::API->Import('Kernel32','SetErrorMode','I','I'); print "Disable :".SetErrorMode(0x0001)."\n"; }
      Seems that 8 is something else. With 1 I can surpress the "Drive not ready dialog"

        He might have meant 0x8000. If 0x8000 works, I'd use that. Using 0x0001 could disable more than you intend to disable.

        Reference: SetErrorMode

Re: Prevent windows dialog when using file test -e
by syphilis (Archbishop) on Jun 23, 2006 at 01:45 UTC
    Is this some XP-specific behaviour ? On 2000 I get no error dialog with such a test:
    D:\>perl -e "print -e 'G:\some.file'" D:\>
    'G' is my (empty) CD-ROM drive. The '-e' test simply returns undef as it does for any non-existent file.

    Cheers,
    Rob
      I don't get the dialog on WinXP and perl v5.8.0. I just assumed different drives behaved differently.
        Have you worked with the drives before and then eject the media ? On my machine it happens only in this case (on XP, never seen it under 2000). I use mostly perl 5.8.8, also it is from inside a wxPerl app (dont know if it makes any difference)
A reply falls below the community's threshold of quality. You may see it by logging in.