[comp.lang.perl] Perl STDIN and ARGV

kandall@sgitokyo.nsg.sgi.com (Michael Kandall) (01/28/91)

I am new to Perl and this newsgroup.  It looks like a useful language.

A common UNIX(tm) System utility usage is to process command line args,
and if they are not present to process stdin.

What's the standard paradigm for switching between these two in Perl?

Can a FILEHANDLE be associated with an arbitrary array?  A natural
equivalence would be one array element per line.  Something like:

	if( $nargs == 1 ) {
		open( INPUT, '-' );
	} else {
		open( INPUT, ARGV );	# ????
	}
		

	while( <INPUT> ) {
		print $_ ;
	}
-- 
----
Michael Kandall
Independent Consultant
Nihon Silicon Graphics

tchrist@convex.COM (Tom Christiansen) (01/29/91)

From the keyboard of kandall@sgitokyo.nsg.sgi.com (Michael Kandall):
:A common UNIX(tm) System utility usage is to process command line args,
:and if they are not present to process stdin.
:
:What's the standard paradigm for switching between these two in Perl?

Normally you do something like this:

    while (<>) {  # <> is magic
	# blah
    } 

This will read from the files on the command line, or stdin if none are
given, and will correctly interpret "-" as stdin in the midst of the
stream.  If you want to process leading -foo options, you can use one of
the getopts libraries:

    require 'getopts.pl';
    &Getopts('wxyza:b:') || &usage;

You mIhgt also use even the -s flag for simple things.  For really
sophisticated argument parsing, you might wish to roll your own.

--tom
--
"Hey, did you hear Stallman has replaced /vmunix with /vmunix.el?  Now
 he can finally have the whole O/S built-in to his editor like he
 always wanted!" --me (Tom Christiansen <tchrist@convex.com>)

gpvos@cs.vu.nl (Gerben 'P' Vos) (01/30/91)

kandall@sgitokyo.nsg.sgi.com (Michael Kandall) writes:

>A common UNIX(tm) System utility usage is to process command line args,
>and if they are not present to process stdin.

>What's the standard paradigm for switching between these two in Perl?

Perl handles this automagically with the <> construct.

while (<>)
	{
	&do_what_you_want($_);
	}

>Can a FILEHANDLE be associated with an arbitrary array?  A natural
>equivalence would be one array element per line.

If you mean what i think you mean, you can't. You can, however, slurp in
a whole file into an array (one element per line), work on it, and then
print the whole thing, along the lines of

open(FILE, "foo");
@array = <FILE>; # slurp
close(FILE);

&dwim(@array);

open(FILE, ">foo");
print @array; # not sure about the line separators, check out yourself.
close(FILE);

jmm@eci386.uucp (John Macdonald) (01/30/91)

In reading this discussion about STDIN, ARGV, and the <> magic
scanner, I had a thought cross my mind, and tried it out.

The command:

    perl -pe '' '|ps' '|who'

printed the output of a ps and a who command!  In processing the
command line arg list as "files" using the <> magic cookie, you
can have all of the magic filename control handling that any
other open provides - pipes, redirection of file descriptors, etc.

Now, to beat Randall to it (if he hasn't already done it a year ago)...

    perl -pe '' '|echo Just' '|echo another' '|echo Perl' '|echo hacker'

-- 
Cure the common code...                      | John Macdonald
...Ban Basic      - Christine Linge          |   jmm@eci386

merlyn@iwarp.intel.com (Randal L. Schwartz) (01/31/91)

In article <1991Jan30.153743.10868@eci386.uucp>, jmm@eci386 (John Macdonald) writes:
| Now, to beat Randall to it (if he hasn't already done it a year ago)...
| 
|     perl -pe '' '|echo Just' '|echo another' '|echo Perl' '|echo hacker'

Nope, haven't done that.  But to get it "right", you need:

perl -pe 's/\n/ /;' 'echo Just|' 'echo another|' 'echo Perl|' 'echo -n hacker,|'
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/