Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

A question about the diamond...

by fokat (Deacon)
on Oct 31, 2003 at 17:07 UTC ( [id://303611]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monks:

While writing some pieces of a test suite, I stumbled upon this strange behavior, which the following snippet exemplifies:

*STDIN = \*DATA; print for <>; __DATA__ This should be printed...

The diamond is supposed to read from the files given in the command line (well, actually the files named in @ARGV) and if none is given, use STDIN.

With the above definition, the snippet should print the message This should be printed..., although this does not happen in any of my perls... (5.6.0, 5.6.1, 5.8.0, 5.8.1). close()ing STDIN before assigning to it does not help much.

I know I can rewrite my tests to read from a file, pipe(), etc. But I really would like to know why this happens, and if using __DATA__ to replace STDIN is possible.

Best regards

-lem, but some call me fokat

Replies are listed 'Best First'.
Re: A question about the diamond...
by Abigail-II (Bishop) on Oct 31, 2003 at 17:12 UTC
    <> is short for <ARGV>, not <STDIN>.

    Abigail

      Duh!... Thanks a million abigail-II!

      Best regards

      -lem, but some call me fokat

Re: A question about the diamond... (open)
by tye (Sage) on Oct 31, 2003 at 18:46 UTC

    Just another example of why you shouldn't copy globs containing open file handles. It is tempting because the syntax is so terse, but there are just too many ways that it fails. Use this instead:

    open STDIN, "<&DATA" or die "Can't dup DATA to STDIN: $!\n"; print for <>; __END__ This *is* printed.

                    - tye

      I was using open in my code. The syntax I pasted in the snippet was my attempt at maximal simplification so as to produce the smallest test case possible. Thanks a lot for the advice tye!

      Best regards

      -lem, but some call me fokat

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found