Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

how can I input different files as an argument while I am running my perl program?

by Anonymous Monk
on Jul 12, 2000 at 00:24 UTC ( #22097=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am a new perl user and would like to seek advices from any perl experts. I would like to run my perl program (.pl file) by inserting different .txt files as my argument. I'm currently running my perl program under UNIX. How can I perform such action above? Many thanks in advance.
  • Comment on how can I input different files as an argument while I am running my perl program?

Replies are listed 'Best First'.
Re: how can I input different files as an argument while I am running my perl program?
by plaid (Chaplain) on Jul 12, 2000 at 00:27 UTC
    When you run your program, just specify the filename as an argument on the command line:
    ./program.pl file1.txt <optionally more files>
    The filenames can then be found inside the script in the global array @ARGV.
      Thanks very much for the reply. However, does that mean that in my perl program, I should include the following line: open (INFILE, @ARGV); in order for me to input different textfiles when I type in the command line? btw is it necessary for me to include -x when I specify the path?
Re: how can I input different files as an argument while I am running my perl program?
by ZZamboni (Curate) on Jul 12, 2000 at 01:16 UTC
    Just specify the files as arguments. The file names will be available in the @ARGV array.

    If you want to input data from those files, you can do several things, depending on how you need to use the data from them:

    • If you need to process their data sequentially, you could simply use the <> "magic" construct, which will read data from all the files given as arguments (or from standard input if no files are given). It will appear as if all the files have been concatenated. If you need to differentiate between the different files, you could use eof as described in its documentation, to test at each iteration if it's the end of the current file.
    • If you need to get data simultaneously from different files, you can explicitly open them by accessing the @ARGV array.

    --ZZamboni

Re: how can I input different files as an argument while I am running my perl program?
by mrmick (Curate) on Jul 12, 2000 at 01:01 UTC
    As plaid said, just add the filename(s) as arguments (space delimited) after the program name.

    Example:

    program.pl file1 file2 file3

    The array @ARGV contains the command line arguments intended for the program. To get the filename or filenames you simply get it from your @ARGV array as follows:

    my $file = shift(@ARGV); # for one file
    or
    my @files = @ARGV; # for more than one file
    You can then do whatever you need to with the file names.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://22097]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2020-07-16 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?