Perl: Getting $ARGV's to operate like while(<>)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Getting $ARGV's to operate like while(<>)
# 1  
Old 07-18-2007
Perl: Getting $ARGV's to operate like while(<>)

I have a script that asks a bunch of questions using the following method for input:
Code:
print "Name:"; 
while(<>){ 
    chomp; 
    $name=$_; 
}

So for example, if the questions asked for name, age, & color (in that order)... I want to be able to easily convert $ARGV[0] into the input expected by <>.

So, just for clarification, the user can run the script as is, answer a bunch of questions, and be done... OR they can know the order of the questions, and put answers to as many as they want in the command line arguments

Irony is that Im asking if there is a way to place $ARGV[0] inside the <> instead of letting <> try to read the file specified by $ARGV[0] like it does by default
# 2  
Old 07-18-2007
You can probably test if you receive any command-line arguments. If so, then read using cmdline arguments, otherwise read <>. I can think of a way to make a facade that makes <> read from the @ARGV, but I cannot think of much justification for that complexity for the scenarios I can imagine. Let's try with some simpler methods first, then if they don't satisfy your requirements then I tell you how to do it.
# 3  
Old 07-19-2007
Why not use <STDIN> instead of <>? Something like
Code:
if(@ARGV) {
   ($name,$age, $color) = @ARGV;
} else {
   print "Name:";
   while(<STDIN>){
       chomp;
       $name=$_;
   }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl sys.argv issue

I am running a perl script and reading the arguments passed to the script as below..... resembles more arguments. java weblogic.WLST /web/update.py 34 56 .... I am trying to print the arguments passed to the update.py script as below for arg in sys.argv: print "other args:", arg... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Wildcarding in a Perl @ARGV Context?

Hello folks! While "sedding" about again, I ran into this little conundrum du jour:#!/usr/bin/perl use strict; use warnings; use diagnostics; @ARGV = ('./afile.dat', './*.txt'); $^I = ''; while (<>) { s/Twinkies/Dinner/g; print; }When run, perl complains,...but, of... (2 Replies)
Discussion started by: LinQ
2 Replies

3. Shell Programming and Scripting

Wildcarding in a Perl @ARGV Context?

Hello folks! While "sedding" about again, I ran into this little conundrum du jour:#!/usr/bin/perl use strict; use warnings; use diagnostics; @ARGV = ('./afile.dat', './*.txt'); $^I = ''; while (<>) { s/Twinkies/Dinner/g; print; }When run, perl complains,...but, of... (1 Reply)
Discussion started by: LinQ
1 Replies

4. Shell Programming and Scripting

Match in perl not working with ARGV

Hi , I need to match module data_tx_dig ( I tried matching ~m/module(\s+)ARGV(\s+)\(/ --> (generic code)but is not working.The same is able to match when i provide hardcoded value instead of passing from the command line. ~m/module(\s+)data_tx_dig(\s+)\(/ -->(hardcoded code).Please help me... (8 Replies)
Discussion started by: dll_fpga
8 Replies

5. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

6. Shell Programming and Scripting

how to include slashes "/" in @ARGV (Perl)

Hello I have simple script that will accept as arg string like this : ".../foo/blah/,.../.../foo1/,.../blah" now perl automatically removes the slashes "/" , I can't escape the slashes in the input I have to control on it so how can I force perl to not touch this slashes? Thanks ... (5 Replies)
Discussion started by: umen
5 Replies

7. Shell Programming and Scripting

To operate MENU via script

Hi, I run this utility very frequently. This utility displays some 5 options and again inturn these options have few more option to select. For Eg : It looks like this 1. User Control 2. Locking and Waiting Statistics 3. Block Access 4. Record Locking Table 5. ... (1 Reply)
Discussion started by: ashokm
1 Replies
Login or Register to Ask a Question