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

Meaning of this Filehandle Error?

by mmartin (Monk)
on Oct 22, 2014 at 21:51 UTC ( [id://1104712]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

In my most recent script I'm working on I've began deploying Signal handlers and BEGIN and END blocks for the 1st time.

I added SIGNAL handlers to my script, along with BEGIN and END blocks, so I could create and remove a PID file for my
daemon/script I'm writing, as well as dealing with a few other loose ends... Everything seems to be working great with
this. I tried everyway I can think of to end/kill my running script, and everytime it properly creates and subsequently
removes the file containing the PID ($$) of my running script.

The error I'm wondering about is the error message below. I get the first part, but I'm confused about why there is an error
at all...?
Died at ./myScriptname line 58, <$fh> line 119.

Line 58 in my script is this below, which is what makes my END{} block execute when the script is killed/Terminated/etc...
$SIG{TERM} = $SIG{INT} = $SIG{QUIT} = $SIG{HUP} = sub { die; };

And this is my END code block.
END { ### This will get the Duration from the starting Execution time # till the time the process was stopped... $DURATION = duration(time() - $^T); my $duration_str = "*The myScriptname Daemon ran for $DURATION\n"; push @data, $duration_str; ### Close the LOG_FILE by untie'ing the array @data from the file: untie @data; ### PID File: Name and Location... my $PID_FILE = "/var/run/myScriptname.pid"; ### If the PID file exists, then we will remove it: if (-e $PID_FILE) { unlink $PID_FILE; #-->Deletes files from the given list... } }

Could that error have anything to do with a file being left open, or something like that, when the END block begins
getting executed? Or, is it that I'm using the untie function in that block?

I believe at the time that error is displayed, the file tied to the array "@data", had 119 lines in it...
So what can I do to get rid of that error/warning?

Any thoughts or suggestions would be much appreciated!

UPDATE:
After I just posted I did a few more tests. Lets say the file 'tied' to @data has exactly 82 lines in it before running
my script. I then start my script and immediately I print/push about 10-12 lines onto the end of the file. So at this
point my file has 94 lines. I then click Ctrl+C and kill my script. I then get the error:
Died at ./myScriptname line 58, <$fh> line 82.
I'm confused why it would give an error at line 82, when at the point the script "dies" there are 94 lines in the file...?

Thanks in Advance,
Matt

Replies are listed 'Best First'.
Re: Meaning of this Filehandle Error?
by Anonymous Monk on Oct 22, 2014 at 22:11 UTC
    Its like English :) left to right, left to right, commas seperate :)

    So  Died at ./myScriptname line 58, <$fh> line 82.

    Its like

    Died at ./myScriptname line 58, <$fh> line 82.

    Its like

    Died at program ./myScriptname while execu line 58 of program, the filehandle <$fh> was open to line 82 when program died.
      Hey AM, thanks for the quick reply!

      Ok, so is it more of just a warning then, that the file was open and this was the last line number in the file?

      I'm guessing I get that error/warning because I am issuing the untie command to close the log file after the
      die command is being run..? Does that sound right?

      Thanks again for the reply, much appreciated!

      Thanks,
      Matt
        The 'die' command, when presented with an argument not terminated with a newline, appends the location where 'die' was called, along the currently selected filehandle, and the current line number of that file, where available. The tieing is irrelevant; '<$fh> line 119' means that $fh is the currently selected filehandle, and 119 is the last line that was read from that file (it should match $.). 119 does not indicate the number of lines in the file.

        Dave.

Re: Meaning of this Filehandle Error?
by mmartin (Monk) on Oct 23, 2014 at 15:51 UTC
    Did a quick test using kill instead of die and this time there was no error or warning displayed about the Filehandle.

    Thanks,
    Matt
      Why don't you use simply exit instead of die? Consider the following small tests:
      $ perl -e ' print "foo\n"; die;' foo Died at -e line 1. $ perl -e ' print "foo\n"; exit;' foo $
        Hey Laurent, thanks for the reply.

        Sorry, that is exactly what I am using now... In my last comment I said kill and what I
        actually meant was exit. Don't know how I did that, must have been a brain fart lol.

        When I started using "exit" instead of "die", that Filehandle warning goes away as well...
        Thanks, if you hadn't replied I probably would never have noticed I said "kill" by
        accident, so thanks!


        Thanks again for the reply, much appreciated.

        Thanks,
        Matt

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found