Bod has asked for the wisdom of the Perl Monks concerning the following question:
One of the modules I have recently released to CPAN reads JSON data from STDIN on a webserver. To write the tests I have had to simulate this.
Using code I found in an answer on SO, I have written code similar to this test:
But I don't understand how or why it works...use strict; use warnings; *STDIN = *DATA; while (my $test = <STDIN>) { print "$test\n"; } __DATA__ one two three four
I've read the documentation and still don't understand it.
My understanding of a typeglob is that it is a legacy left over from the days before Perl had references. It manipulates the scalar, array and hash of the same name simultaneously. Still, it shouldn't be used in modern Perl because it is generally bad practice to call arrays, hashes and scalars by the same name and references have replaced its necessity. References, thanks to my time in The Monastery, I understand reasonably well.
Is there a better way to do what the above code does?
Can you help me understand this use of a typeglob and typeglobs more generally?
Am I able to 'reset' the __DATA__ input so that I can use the same input for testing again or should I put multiple tests in different test files?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: STDIN typeglob
by hv (Prior) on Jun 11, 2023 at 19:40 UTC | |
by afoken (Chancellor) on Jun 12, 2023 at 09:35 UTC | |
by eyepopslikeamosquito (Archbishop) on Jun 12, 2023 at 13:17 UTC | |
by afoken (Chancellor) on Jun 12, 2023 at 19:53 UTC | |
by Bod (Parson) on Jun 13, 2023 at 20:42 UTC | |
by Bod (Parson) on Jun 13, 2023 at 20:58 UTC | |
by eyepopslikeamosquito (Archbishop) on Jun 14, 2023 at 00:24 UTC | |
| |
by Bod (Parson) on Jun 11, 2023 at 21:48 UTC | |
by hippo (Archbishop) on Jun 11, 2023 at 22:07 UTC | |
Re: STDIN typeglob
by pryrt (Abbot) on Jun 11, 2023 at 17:32 UTC | |
Re: STDIN typeglob
by kcott (Archbishop) on Jun 12, 2023 at 02:50 UTC | |
by Bod (Parson) on Jun 13, 2023 at 21:06 UTC | |
by Bod (Parson) on Jun 18, 2023 at 17:10 UTC | |
by afoken (Chancellor) on Jun 18, 2023 at 18:35 UTC | |
by Bod (Parson) on Jun 18, 2023 at 18:49 UTC | |
by afoken (Chancellor) on Jun 18, 2023 at 19:57 UTC | |
| |
Re: STDIN typeglob
by ikegami (Patriarch) on Jun 11, 2023 at 22:35 UTC | |
Re: STDIN typeglob
by Marshall (Canon) on Jun 11, 2023 at 19:09 UTC | |
Re: STDIN typeglob
by jwkrahn (Abbot) on Jun 11, 2023 at 22:56 UTC |