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

local *IO::File::open = sub {.....}
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.

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.

exp.pl
#!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);
and Test.pm
package Test; sub subs { my $string = "hello world"; local *STDIN; open(STDIN, '<', \$string) or die "open failed: $!"; binmode STDIN; while(<>) { print; } } 1

In reply to Re: Making open fail by SilasTheMonk
in thread Making open fail by SilasTheMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.