SPUG: Dup'ing STDIN from DATA?

Brian Ingerson briani at activestate.com
Fri Mar 16 14:27:48 CST 2001


Matt Tucker wrote:
> 
> -- Tim Gim Yee <tgy at chocobo.org> spake thusly:
> 
> > On Thu, 15 Mar 2001, Tim Maher/CONSULTIX wrote:
> >
> >> I'm writing a little program that will eventually read input from
> >> SDTIN, but initially I want to test it using the DATA filehandle.
> >> I thought the following would work, but it doesn't; it just reads
> >> from STDIN.  Anybody know the way to accomplish this?
> >>
> >> #! /usr/bin/perl -wn
> >>
> >> BEGIN {
> >>      open STDIN, "<&DATA";   # comment-out after testing
> >> }
> >
> > Reading from <>, maybe you want to dup ARGV instead of STDIN.
> >
> >     BEGIN { @ARGV = '<&DATA' }
> 
> You're talking nonsense here. This would place '<&DATA' into the array
> @ARGV, which would presumably cause Perl to try to open an actual file
> named '<&DATA' when it got to 'while (<>)'. I haven't even tried this,
> and I'd be shocked if it did anything like what you expect.

Well it makes sense to me. And it does work. I know this because I
actually *did* try it before I went and flamed you for flaming someone
as bright as TGY. I'm actually shocked that you would post this
*without* trying it. </FLAME>

So this works: (No BEGIN block needed)

----8<----
#! /usr/bin/perl -w

@ARGV = '<&DATA';

while (<>) { print }

__DATA__
one little
two little
three little indians
----8<----

and this also works: (BEGIN Required. Note -p flag which is similar (but
prints $_ automatically) to the -n that Tim Maher originally wanted to
use)

----8<----
#! /usr/bin/perl -pw

BEGIN { @ARGV = '<&DATA' }

s/little/big/;
s/indian/idiot/;

__DATA__
one little
two little
three little indians
----8<----

BTW, This only works with 5.6.0+ (The *real* Perl ;)

Cheers, Brian

-- 
perl -le 'use Inline C=>q{SV*JAxH(char*x){return newSVpvf
("Just Another %s Hacker",x);}};print JAxH+Perl'

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list