read from file or from <DATA>?

M

Michael Friendly

This is probably simple, but I can't find it...
I frequently write scripts to take input from STDIN or from a named file
by doing

open(STDIN, "<$opt_i") or die "-i $opt_i: can't open.\n" if $opt_i ;
...
while (<>) {
}

but how can I read from <DATA> if no file is given on the command line?

undef $/; # slurp whole file
my $lines = @ARGV ? <> : <DATA>;

will work, but I need to read in a while loop.


--
Michael Friendly Email: (e-mail address removed)
Professor, Psychology Dept.
York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT M3J 1P3 CANADA
 
P

Paul Lalli

Michael said:
This is probably simple, but I can't find it...
I frequently write scripts to take input from STDIN or from a named file
by doing

open(STDIN, "<$opt_i") or die "-i $opt_i: can't open.\n" if $opt_i ;
...
while (<>) {
}

but how can I read from <DATA> if no file is given on the command line?

undef $/; # slurp whole file
my $lines = @ARGV ? <> : <DATA>;

will work, but I need to read in a while loop.

Read from either the *DATA filehandle or the *ARGV filehandle...

$ cat file1.txt
line one from file1
line two from file1

$ cat file2.txt
line one from file2
line two from file2

$ cat file_or_data.pl
#!/usr/bin/perl
use strict;
use warnings;

my $fh = @ARGV ? \*ARGV : \*DATA;
while (<$fh>){
print;
}

__DATA__
Line one from Data
Line two from Data
Line three from Data


$ ./file_or_data.pl
Line one from Data
Line two from Data
Line three from Data

$ ./file_or_data.pl file*.txt
line one from file1
line two from file1
line one from file2
line two from file2


Hope this helps,
Paul Lalli
 
X

xhoster

Michael Friendly said:
This is probably simple, but I can't find it...
I frequently write scripts to take input from STDIN or from a named file
by doing

open(STDIN, "<$opt_i") or die "-i $opt_i: can't open.\n" if $opt_i ;
...
while (<>) {
}

Why not just let <> do it's magic?

unshift @ARGV, $opt_i if $opt_i;
while (<>) {

Or, if $opt_i came from @ARGV in the first place, then stop removing it
from @ARGV and just do:

but how can I read from <DATA> if no file is given on the command line?

undef $/; # slurp whole file
my $lines = @ARGV ? <> : <DATA>;

will work, but I need to read in a while loop.

Paul answered this. But you don't have to use glob-refs, you can just
use strings.

my $fh = @ARGV ? 'ARGV' : 'DATA';
while (<$fh>) {

Xho
 
P

Paul Lalli

Paul answered this. But you don't have to use glob-refs, you can just
use strings.

my $fh = @ARGV ? 'ARGV' : 'DATA';
while (<$fh>) {

Huh! I just assumed that using a string there would give some sort of
"cannot use string 'DATA' as file-handle reference when strict refs is
in effect" error. Guess I was being overly cautious.

Thanks, Xho.

Paul Lalli
 
J

John W. Krahn

Michael said:
This is probably simple, but I can't find it...
I frequently write scripts to take input from STDIN or from a named file
by doing

open(STDIN, "<$opt_i") or die "-i $opt_i: can't open.\n" if $opt_i ;

No need to mess with the STD* filehandles:

@ARGV = $opt_i if defined $opt_i;
...
while (<>) {
}

but how can I read from <DATA> if no file is given on the command line?

You just said that you will take input from STDIN if no file is given on the
command line. Is this a psychology test? :)

You probably want to determine if STDIN is connected to a terminal or
receiving data from redirection or a pipe.

perldoc -f -t



John
 
B

Brian McCauley

you don't have to use glob-refs, you can just use strings.

Just because Perl will sometimes let you use symrefs without
complaining doesn't make it a good idea to do in situations where you
could equally simply use real references.
 
B

brian d foy

John W. Krahn said:
You probably want to determine if STDIN is connected to a terminal or
receiving data from redirection or a pipe.

perldoc -f -t

Heh, I'm surprised that perldoc sequence worked, considering that
perldoc has its own -t switch. :)
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top