(1) How does one "pass two parameters" to any perl script,
by enterning the parameters on the "command line", where one (either one) is a filename, and the other is some variable-value to be passed to a variable-name in the perl script)?
I had supposed (from my reading in perl books)that the array, "@ARGV" held all the "parameters" on the "command line". However, when I try this, I get confusing (to me) results, and certainly not the results I expected!
(2) Example #1:
script:
==========================================================
# program perl_argv_1.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
==========================================================
command-line: "perl perl_argv_1.pl sample sample_2 <enter>"
===========================================================
Output:
@ARGV[0] = sample
@ARGV[1] = sample_2
============================================================My comment: This is what I understood would happen, and what I expected to happen - no problem. But the next program, "perl_argv_2.pl" does NOT produce the results I expected;
-----------------------------------------------------------
-----------------------------------------------------------
Example #2
script:
# program perl_argv_2.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
while ( <sample> )
{ print $_; }
===========================================================
command-line: "perl perl_argv_2.pl sample sample_2 <enter>"
===========================================================
output:
@ARGV[0] = sample
@ARGV[1] = sample_2
============================================================
My comment: This is certainly NOT what I expected. I thought the program would print the first two lines (above), and then in addition, all the lines of the ascii-text-file, "sample", following the above two lines!
============================================================ Example #3:
Script:
# program perl_argv_3.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
print "-------------------------\n";
while ( <@ARGV[0] > )
{ print $_; }
============================================================
command-line: "perl perl_argv_3.pl sample sample_2 <enter>"
===========================================================
Output:
@ARGV[0] = sample
@ARGV[1] = sample_2
-------------------------
sample
==========================================================
My comment: I'm not sure what I expected. What I WANTED was the program to print the first two lines and then, following that, the CONTENT of the file: "sample". I understand (I think), why I got the result I did, even though it wasn't the results I wanted.
===========================================================
Example #4
Script:
# program perl_argv_4.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
print "-------------------------\n";
while ( <> )
{ print $_; }
============================================================
command-line: "perl perl_argv_4.pl sample sample_2 <enter>"
===========================================================
Output:
@ARGV[0] = sample
@ARGV[1] = sample_2
-------------------------
sample
123 mary has fair skin
has red hair
1286 bob has no
hair
sample
XXX123 mary has fair skin
has red hair
XXX1286 bob has no
hair
=========================================================
My comment: This is exactly what I expected from my commmand line - but NOT what I wanted. What I WANTED was to print the CONTENTS of the file: "sample", and to use the 2nd. command-line parameter ("sample_2"
for "SOMETHING ELSE" (whatever else doesn't particularly matter.)
The question is: HOW do I "tell" perl that one (either one) of these two parameters is a file name, and the other is merely one of the values of "@ARGV"?
I had expected that Example#3 would do this, since I had "spelled-out" (I thought) the "name" of the file I wanted to read and print (as the VALUE "contained in" @ARGV[0]), and I had NOT mentioned reading and printing the "other" file, "sample_2. Instead,, of course, as one can see, I did NOT print out the contents of either file, in Example#3, as I had wanted to do with ONE of them.
===========================================================================================
The question is: HOW do I "tell" perl that one (either one) of these two parameters is a FILE NAME, to be READ and "processsed", and the other is merely one of the values of "@ARGV" that I want to use some (any) other way?
=========================================================================================
Thanx in advance for any help you can give me with this. ernieah
by enterning the parameters on the "command line", where one (either one) is a filename, and the other is some variable-value to be passed to a variable-name in the perl script)?
I had supposed (from my reading in perl books)that the array, "@ARGV" held all the "parameters" on the "command line". However, when I try this, I get confusing (to me) results, and certainly not the results I expected!
(2) Example #1:
script:
==========================================================
# program perl_argv_1.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
==========================================================
command-line: "perl perl_argv_1.pl sample sample_2 <enter>"
===========================================================
Output:
@ARGV[0] = sample
@ARGV[1] = sample_2
============================================================My comment: This is what I understood would happen, and what I expected to happen - no problem. But the next program, "perl_argv_2.pl" does NOT produce the results I expected;
-----------------------------------------------------------
-----------------------------------------------------------
Example #2
script:
# program perl_argv_2.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
while ( <sample> )
{ print $_; }
===========================================================
command-line: "perl perl_argv_2.pl sample sample_2 <enter>"
===========================================================
output:
@ARGV[0] = sample
@ARGV[1] = sample_2
============================================================
My comment: This is certainly NOT what I expected. I thought the program would print the first two lines (above), and then in addition, all the lines of the ascii-text-file, "sample", following the above two lines!
============================================================ Example #3:
Script:
# program perl_argv_3.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
print "-------------------------\n";
while ( <@ARGV[0] > )
{ print $_; }
============================================================
command-line: "perl perl_argv_3.pl sample sample_2 <enter>"
===========================================================
Output:
@ARGV[0] = sample
@ARGV[1] = sample_2
-------------------------
sample
==========================================================
My comment: I'm not sure what I expected. What I WANTED was the program to print the first two lines and then, following that, the CONTENT of the file: "sample". I understand (I think), why I got the result I did, even though it wasn't the results I wanted.
===========================================================
Example #4
Script:
# program perl_argv_4.pl
print '@ARGV[0] = ', @ARGV[0], "\n";
print '@ARGV[1] = ', @ARGV[1], "\n";
print "-------------------------\n";
while ( <> )
{ print $_; }
============================================================
command-line: "perl perl_argv_4.pl sample sample_2 <enter>"
===========================================================
Output:
@ARGV[0] = sample
@ARGV[1] = sample_2
-------------------------
sample
123 mary has fair skin
has red hair
1286 bob has no
hair
sample
XXX123 mary has fair skin
has red hair
XXX1286 bob has no
hair
=========================================================
My comment: This is exactly what I expected from my commmand line - but NOT what I wanted. What I WANTED was to print the CONTENTS of the file: "sample", and to use the 2nd. command-line parameter ("sample_2"
The question is: HOW do I "tell" perl that one (either one) of these two parameters is a file name, and the other is merely one of the values of "@ARGV"?
I had expected that Example#3 would do this, since I had "spelled-out" (I thought) the "name" of the file I wanted to read and print (as the VALUE "contained in" @ARGV[0]), and I had NOT mentioned reading and printing the "other" file, "sample_2. Instead,, of course, as one can see, I did NOT print out the contents of either file, in Example#3, as I had wanted to do with ONE of them.
===========================================================================================
The question is: HOW do I "tell" perl that one (either one) of these two parameters is a FILE NAME, to be READ and "processsed", and the other is merely one of the values of "@ARGV" that I want to use some (any) other way?
=========================================================================================
Thanx in advance for any help you can give me with this. ernieah