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:

use strict; use warnings; *STDIN = *DATA; while (my $test = <STDIN>) { print "$test\n"; } __DATA__ one two three four
But I don't understand how or why it works...

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?


In reply to STDIN typeglob by Bod

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.