Hello. I have the following program.
#!/usr/bin/perl sub print_user_input { my $input = <STDIN>; print $input; } sub mock_user_input { local *STDIN; open STDIN, '<', \"this is fake user input\n"; print_user_input(); } mock_user_input(); __END__ $ perl ./scratch.pl this is fake user input
I have successfully used the pattern displayed in the
mock_user_input
subroutine to test some user interfacing code I have. However I cannot wrap my head around how this actually works. What exactly does *STDIN
mean and why does it need to be localized? Why are we passing a string reference as the third argument to open
here? A detailed explanation of this code would be helpful for me. Thanks in advance. submitted by /u/nicholas_hubbard
[link] [comments]