Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Detecting read errors whilst reading from a network drive

by Scarborough (Hermit)
on Apr 14, 2004 at 11:37 UTC ( #344997=perlquestion: print w/ replies, xml ) Need Help??
Scarborough has asked for the wisdom of the Perl Monks concerning the following question:

A simple question, on a windows based network would it be possible to open a file and then not be able to read it at a later stage due to a network failure for example. If so is there a read error. The code I am using is as follows,
open RESTART, "x:\\myfiles\file.txt" or die; while(<RESTART>){ #do something with $_ }
Thanks for the help. UPDATE
Maybe my wording is wrong or a little misleading
open RESTART, "x:\\myfiles\file.txt" or die; #Assume the file opens correctly while(<RESTART>){ #do something with $_ #My Worry is if the network collapses in this #loop. Will I know about it or will it hang? }

20040414 Edit by BazB: Changed title from 'Open and reading files'

Comment on Detecting read errors whilst reading from a network drive
Select or Download Code
Re: Detecting read errors whilst reading from a network drive
by hmerrill (Friar) on Apr 14, 2004 at 12:00 UTC
    This sounds like a loaded question - why are you asking? Did you run into a problem with the code above? Are you wondering if there is more error trapping you can do to detect a read problem?

    The code you gave is exactly the code I would use to open and read/process all the records in a file. Except I would put a message in my die - something like this:

    my $abs_filename = "x:\\myfiles\file.txt"; open(RESTART, "<$abs_filename") or die "Can't open $abs_filename: $!"; while (<RESTART>) { # do something with $_ }
Re: Detecting read errors whilst reading from a network drive
by PodMaster (Abbot) on Apr 14, 2004 at 12:05 UTC
    Sure, why not? You probably won't notice using readline, so you might try read if you care.

    update: Do a test. Start reading a file from a networked drive (actually through the network) and be sure to sleep in between each read (so you don't need a big file). Then, while your program is running, disconnect your computer from the network (pull cable, whatever) and see what happens (the next readline should fail). Be sure to check $! and $^E. If that doesn't work for you, try Win32API::File for more control.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Detecting read errors whilst reading from a network drive
by crabbdean (Pilgrim) on Apr 14, 2004 at 12:07 UTC
    In general yes you'll get an error. The error will be pretty much similar to the one if you had a read error on a local drive. You should try using  or die $!; if you want the reasons for the error to be output. At least then your 'die' is a little informative.

    If you want the bells and whistles version you could drop the below into your code and call it with dying($!) instead. If you don't want the bells remove the 'sound' related lines I appended with ##>> and if you don't want the error message brutally forced to the front of all your other applications remove the 'win32Util' lines that I appended with ##%%

    Happy travels! :-)
    ## put into the top of your main code ## Show fatal error dialog sub dying { my ($error) = @_; use Tk; use Win32::Sound; ##>> use Win32Util; ##%% if (!$error) { $error = "(none given)"; } my $text; $text .= "\n\nError:\n\n$error"; my $box = new MainWindow(-title => "Error", -bg => 'yellow', -bd => 4, -relief => 'ridge'); $box->overrideredirect(1); my $label = $box->Label( -textvariable=> \$text, -bg=> 'yellow', )->pack(-fill=>'both', -padx => 40, ); Win32::Sound::Play('SystemExclamation'); ##>> my $ok = $box->Button(-text => "OK", -command => [ sub{print STDOUT "User selected 'OK' + on error message\n"; $box->destroy; exit; } ] )->pack(-padx => 20, -pady => 20 ); $box->withdraw; $box->Popup; Win32Util::keep_on_top($box, 1); ##%% MainLoop; }

    Dean
    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
      The file is already open. He's reading the file. There is a network failure and he can't read the file anymore. There should be an error in $! and $^E.
        Well yes. But if the only indicator that your network just failed is the $! and $^E then I'd suggest hiring better network administrator and getting better network monitors haha! Sorry couldn't resist. :-) I just get see the chaos that goes on around my work when the network dies. haha. I was running a script once when my network went down. Besides the thought of "Oh hell, did I just do that?" it was pretty obvious why my script failed.

        Dean
        The Funkster of Mirth
        Programming these days takes more than a lone avenger with a compiler. - sam
        RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
Re: Detecting read errors whilst reading from a network drive
by iburrell (Chaplain) on Apr 14, 2004 at 19:52 UTC
    You can check for errors at the bottom of the loop. The <> operator will return undef on errors which ends the loop and sets $!. The other way to find errors is to check the return status of close.
    open RESTART, "x:\\myfiles\file.txt" or die; while (<RESTART>) { } die "Read error: $!" if $!; close(RESTART) or die "Close error: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://344997]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-09-02 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls