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

Hi all, I am very new to perl. I wish to know how @ARGV function? I knew that you can run perl script by typing "file_3.pl -file_1 -file_2" but I do not know how to use it effectively.

Replies are listed 'Best First'.
Re: How to use @ARGV
by kcott (Archbishop) on Aug 29, 2017 at 05:21 UTC

    G'day chaney123,

    Welcome to the Monastery.

    @ARGV is a special variable (see perlvar). You'll find documentation about @ARGV, along with other variables containing 'ARGV', in the "Variables related to filehandles" section.

    You can use it directly in your code:

    $ perl -E 'say for @ARGV' file_1 file_2 file_1 file_2

    You can assign it to another variable and use that:

    $ perl -E 'my @x = @ARGV; say for @x' file_1 file_2 file_1 file_2

    Be careful if your arguments (whether filenames or something else) start with a hyphen, e.g. the -file_1 and -file_2 you show:

    $ perl -E 'say for @ARGV' -file_1 -file_2 -i used with no filenames on the command line, reading from STDIN.

    They look like options (to both Perl and humans). Separate options and other (non-option) arguments with '--':

    $ perl -E 'say for @ARGV' -- -file_1 -file_2 -file_1 -file_2

    If your command line arguments are options, instead of trying to process them yourself, use one of the core modules. Getopt::Long is what I use. There's also Getopt::Std: I don't use that one so I can't really comment on it.

    — Ken

Re: How to use @ARGV
by NetWallah (Canon) on Aug 29, 2017 at 05:06 UTC
    The perlvar documentation describes @ARGV as:

    @ARGV

    The array @ARGV contains the command-line arguments intended for the script.
    $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself.
    See $0 for the command name.

    Here are a few references to get you started:

    Perl Maven : argv in perl

    Roberts tutorial

                    All power corrupts, but we need electricity.

Re: How to use @ARGV
by Marshall (Canon) on Aug 29, 2017 at 05:00 UTC
    The array, @ARGV contains the space separated stuff that follows the command.

    #!/usr/bin/perl; use strict; use warnings; #this file is: file_3.pl #command used: >perl file_3.pl -file_1 -file_2 print "$_\n" for @ARGV; __END__ C:\Projects_Perl\testing>perl file_3.pl -file_1 -file_2 -file_1 -file_2
    There are modules that can parse out options like "-x" and so forth. Depending upon your shell, wild cards can be used in the command line, like: file_*

    I am not sure what you want to do. Please explain further.

Re: How to use @ARGV
by karlgoethebier (Abbot) on Aug 29, 2017 at 08:06 UTC

    For command line processing see also Re: Using number "0" as an input argument and the links ibidem. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: How to use @ARGV
by thanos1983 (Parson) on Aug 29, 2017 at 08:32 UTC

    Hello chaney123,

    It seems the fellow Monks have provided you with plenty of examples and tutorials. I would like to add something minor here.

    My personal (favorite) way of handling @ARGV when you need to read files (but not only for that) I prefer to use it like this:

    #!usr/bin/perl use strict; use warnings; while (<>) { print "$.\t$_"; # updating $. $.\t$_ } continue { close ARGV if eof; }

    Depending upon the case of course but I tend to prefer to use it like this. It does not mean that it always the best solution, but it depends upon your task. You can read more about that here eof and also similar question with code samples on how to open a file and parse the data open and read text file.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Also in the "I/O Operators" section of perlop. "The null filehandle <> is special..."
      Bill