in reply to Making open fail
Ikegami, I tried subs again. It's pretty close to what I had tried before, but I was trying explicitly to be as close as possible to what you suggest. The script hangs on the print/read statement without the custom "open" function running.
#!perl -w use strict; use warnings; use subs qw(open); use Errno qw(EPIPE); sub open { $! = EPIPE; warn "open faked"; return 0; } Test::subs(); exit(0); package Test; sub subs { my $string = "hello world"; local *STDIN; open(STDIN, '<', \$string) or die "open failed: $!"; binmode STDIN; while(<>) { print; } } 1
bluescreen I arrived at this situation by trying to follow best practices. 1.) opening a file should be error checked. 2.) I should use Devel::Cover to make sure that my tests provide 100% test coverage. It does seem likely that this particular call can never fail, but I still thinking the problem would be hard in some other circumstances. Also may be there could be a security hole in the perl core, which would allow hackers to generate such a situation and I want to defend my code against such known unknowns.
Actually I have had two more ideas on how to approach this. One is experimenting with
If ikegami is right that't won't work but I mean it only as indication of what I might try. I assume that this is how subs works anyway. The second idea is to use autodie. I have tried this and it works in as far as it allows me to get my test coverage up. Arguably however it is just sidestepping the problem.local *IO::File::open = sub {.....}
Edit: Looking at the code for subs I can see why it won't work for me. It only deals with the calling package not where as I want to change how "open" works inside a package from outside.
Edit 2: My experimentation with typeglobs is failing. It does no better than the subs code above.
and Test.pm#!perl -w use warnings; use Errno qw(EPIPE); use lib qw(.); eval "use Test;"; BEGIN { *{main::open} = sub { $! = EPIPE; warn "open faked"; return 0; }; } Test::subs(); exit(0);
package Test; sub subs { my $string = "hello world"; local *STDIN; open(STDIN, '<', \$string) or die "open failed: $!"; binmode STDIN; while(<>) { print; } } 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Making open fail
by bluescreen (Friar) on Jul 04, 2010 at 16:03 UTC | |
by DrHyde (Prior) on Jul 05, 2010 at 10:26 UTC | |
by bluescreen (Friar) on Jul 05, 2010 at 13:15 UTC | |
by SilasTheMonk (Chaplain) on Jul 05, 2010 at 14:50 UTC | |
by bluescreen (Friar) on Jul 05, 2010 at 18:30 UTC | |
by SilasTheMonk (Chaplain) on Jul 04, 2010 at 21:50 UTC | |
|
Re^2: Making open fail
by ikegami (Patriarch) on Jul 05, 2010 at 18:23 UTC |