The easiest way to do this is to use IO::Handle:
#!/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;
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.

What's nice about this approach is that IO::Handle handles reverting STDIN to its previous state, after  $fh->close for you. Moreover, it's also a rather simple approach. However, in addition, you could also always localize STDIN, like so:
{ local *STDIN; open(STDIN,'myfile'); #local *STDIN = *MYFILE; print <>; close(STDIN); }
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.

P.S: FYI, this is technique is generally called 'overloading STDIN'. Your searches would have been more successful, even on perlmonks, if you had used the proper vocabulary. :)

Update: With regards to your specific question, here's a possible (untested--closer to pseudocode) solution, which mimics your own (in functionality.)
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';
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.

Gyan Kapur
The Worst Nodes Upvoter!

In reply to Re: Pre-empting STDIN during Testing by Revelation
in thread Pre-empting STDIN during Testing by jkeenan1

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.