Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

die silently in an eval block while using alarm

by gg48gg (Sexton)
on Feb 21, 2013 at 20:36 UTC ( #1020002=perlquestion: print w/ replies, xml ) Need Help??
gg48gg has asked for the wisdom of the Perl Monks concerning the following question:

In reference to Die silently?, an awesome thread, especially the 'fart' code and the explanations of it...

I recently ran into the need to "die silently", while using the below code. I was getting unwanted "\n"'s inserted that I could not explain. I ended up removing the "die" statement and it works as I want, but I am not sure if I am doing something incorrect (not killing my system command?). This is really my first attempt in using a timed out system command via eval/alarm and could use some review and constructive feedback. Here is my code if you care to help...

sub get_size { my $disk=$_[0]; my $size; eval { local $SIG{ALRM} = sub { print_debug("get_size timed out for device $disk"); die; }; alarm(4); $size=qx(bootinfo -s $disk 2>/dev/null); alarm(0); }; $size=($size)? $size : "unknown"; chomp $size; return $size; }

Comment on die silently in an eval block while using alarm
Download Code
Re: die silently in an eval block while using alarm
by 7stud (Deacon) on Feb 22, 2013 at 07:28 UTC
    Never mind, your lack of any discernible indenting means you won the obfuscation contest. Congratulations, you fooled me!
Re: die silently in an eval block while using alarm
by andal (Friar) on Feb 22, 2013 at 07:49 UTC

    I can't really see, how "die" in your code could produce any output. It is called by ALRM signal handler inside of eval, so the output of "die" is saved in $@ and not printed anywhere. If you get some output, then it comes from somewhere else. Check for example this code

    my $ch; eval{ $SIG{ALRM} = sub{ die "Waiting finished\n"; }; alarm(10); $ch = <STDIN>; alarm(0); }; if($@) { print "Timeout happened\n"; } else { print "Got $ch"; }
    You'll never see "Waiting finished" because it is stored in $@ variable and the code does not print it.

      Thanks for the reply. I agree, I shouldn't have any output from die. Is it possible that I get output from the result of die? I can't explain the behavior, I can just observe it. Thanks for the example code, I will study it for a bit.

        What do you mean by "result of die"? "die" never returns, so there is no "result" returned.

        Search for the problem somewhere else. It has nothing to do with "die" in provided piece of code. Inside of eval "die" does not produce any output. Check your other "warn", "print", "write", or "die" outside of eval.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020002]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-23 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls