Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Testing a subroutine with STDIN

by mulander (Monk)
on Oct 28, 2005 at 14:16 UTC ( [id://503649]=perlquestion: print w/replies, xml ) Need Help??

mulander has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monks.

I am wondering how can one test a subroutine that requires input from STDIN somewhere in the middle of the sub.

I searched for some function that could test such a sub in Test::More but did not found any that could let me scheduele what to input when the test sees <STDIN>. I just want to queue the input that normally a user would type in, so the test can run without the tester substituting the user. Maybe any one could guide me where to search for such a sub, or should I test it in another way?

Edited by planetscape - formatting and s/subrutine/subroutine/;

2005-10-28 Retitled by planetscape, as per Monastery guidelines
Original title: 'Re: Testing a subrutine with STDIN'

Replies are listed 'Best First'.
Re: Testing a subroutine with STDIN
by calin (Deacon) on Oct 28, 2005 at 15:30 UTC
    #!/usr/bin/perl sub with_stdin { my $surrogate_input = shift; my $test_sub = shift; local *STDIN; open STDIN, '<', \$surrogate_input; &{$test_sub}; } sub test_sub { print "Arguments: @_\n"; while(<STDIN>) { print "** $_"; } } with_stdin <<'EOS' Once Upon A Time EOS , \&test_sub, qw(arg1 arg2 arg3);
Testing a subroutine with STDIN
by sauoq (Abbot) on Oct 28, 2005 at 14:40 UTC

    Just put the input in a file and reopen STDIN to read from that file before you call your sub.

    -sauoq
    "My two cents aren't worth a dime.";
    

    2005-10-28 Retitled by planetscape, as per Monastery guidelines
    Original title: 'Re: Testing a subrutine with STDIN'

Re: Testing a subroutine with STDIN
by liz (Monsignor) on Oct 28, 2005 at 19:22 UTC
Re: Testing a subroutine with STDIN
by xdg (Monsignor) on Oct 28, 2005 at 17:54 UTC

    Similar to the external file approach, for simple tests, I've gotten some mileage out of just pointing STDIN to DATA.

    use warnings; *STDIN = *DATA; print while <STDIN>; __DATA__ Hello world Once upon a time... How now brown cow

    Another approach that doesn't need an external file is to use IO::String to create an in-memory file and alias STDIN to that. By directly changing the contents of the underlying scalar, you can control what will next be read from the in-memory file.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://503649]
Approved by fizbin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found