in reply to Pre-empting STDIN during Testing
You'll notice that this (at the least) solves your problem with the diamond operator. With regards to a while (<>) loop, for which I assume, you have a last() somewhere, the solution is not as clear. Obviously, any file has a limited length. Perhaps, there's some way to use IO::Handle as a base and generate some sort of callback routine that could generate more data, but that's a little too dirty for me. Realistically, are you going to need more than, say, 2,000 lines? If not, then just create a 2,000 line file.#!/usr/bin/perl use strict; use warnings; use IO::Handle; open (INPUT, "myfile") || die $!; if (my $fh = STDIN->fdopen(\*INPUT, "r")) { print <>; $fh->close; } close INPUT;
Both of these aproaches will work whether you're calling STDIN in a used module or in the test script, itself. However, I assume each approach has different limitations as to backwards compatibility which I have not explored as of yet.{ local *STDIN; open(STDIN,'myfile'); #local *STDIN = *MYFILE; print <>; close(STDIN); }
Moreover, and the true beauty of IO::Handle, is that you can inherit from it. If you want to have a retrieve values from a list, rather than using my silly workaround, then feel free to write a module inheriting from IO::Handle that does so (my advice would be to overload perhaps fdopen and definitely getline and getlines)! Otherwise, IO::Handle _at least_ gives you a template for extending Perl's IO system in a more flexible manner than tying a hash. Perhaps, another monk will have an in-the-box workaround on IO::Handle to allow you to use a callback to a subroutine that shifts from an array (look at IO::String to see how this is handled for strings--you could modify that), but until then, you now have a pretty flexible solution that only involves a temporary file (which can be created using IO::File); I'm going to sleep now, but perhaps tomorrow, I'll think of a better solution, or somebody smarter than myself, like merlyn will do so.my @lines = ( 'a string with numbers and letters 4678', '5678', '46789', ' 4678', '4678', ); open (INPUT, '+<', "tmpfile") || die $!; print INPUT $_."\n" for @lines; seek(INPUT,0,0); if (my $fh = STDIN->fdopen(\*INPUT, "r")) { my $room = enter_number(); $fh->close; print "Room: $room\n"; } close INPUT; unlink 'tmpfile';
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pre-empting STDIN during Testing
by jkeenan1 (Deacon) on Feb 15, 2005 at 02:34 UTC | |
by Anonymous Monk on Feb 15, 2005 at 02:44 UTC | |
|
Re^2: Pre-empting STDIN during Testing
by jkeenan1 (Deacon) on Feb 15, 2005 at 03:30 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |