<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=Perl_Command-Line_Arguments</id>
	<title>Perl Command-Line Arguments - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=Perl_Command-Line_Arguments"/>
	<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=Perl_Command-Line_Arguments&amp;action=history"/>
	<updated>2026-04-13T07:31:58Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://wiki.scott5.org/index.php?title=Perl_Command-Line_Arguments&amp;diff=201&amp;oldid=prev</id>
		<title>Scott: Created page with &#039; == The @ARGV array == With Perl, command-line arguments are stored in the array named @ARGV. $ARGV[0] contains the first argument, $ARGV[1] contains the second, etc.  == The Get…&#039;</title>
		<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=Perl_Command-Line_Arguments&amp;diff=201&amp;oldid=prev"/>
		<updated>2011-02-01T18:15:40Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039; == The @ARGV array == With Perl, command-line arguments are stored in the array named @ARGV. $ARGV[0] contains the first argument, $ARGV[1] contains the second, etc.  == The Get…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== The @ARGV array ==&lt;br /&gt;
With Perl, command-line arguments are stored in the array named @ARGV. $ARGV[0] contains the first argument, $ARGV[1] contains the second, etc.&lt;br /&gt;
&lt;br /&gt;
== The Getopt::Std and Getopt::Long Modules ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use Getopt::Long;&lt;br /&gt;
GetOptions(&amp;quot;o&amp;quot;=&amp;gt;\$oflag,&lt;br /&gt;
    &amp;quot;verbose!&amp;quot;=&amp;gt;\$verboseornoverbose,&lt;br /&gt;
    &amp;quot;string=s&amp;quot;=&amp;gt;\$stringmandatory,&lt;br /&gt;
    &amp;quot;optional:s&amp;quot;,\$optionalstring,&lt;br /&gt;
    &amp;quot;int=i&amp;quot;=&amp;gt; \$mandatoryinteger,&lt;br /&gt;
    &amp;quot;optint:i&amp;quot;=&amp;gt; \$optionalinteger,&lt;br /&gt;
    &amp;quot;float=f&amp;quot;=&amp;gt; \$mandatoryfloat,&lt;br /&gt;
    &amp;quot;optfloat:f&amp;quot;=&amp;gt; \$optionalfloat);&lt;br /&gt;
print &amp;quot;oflag $oflag\n&amp;quot; if $oflag;&lt;br /&gt;
print &amp;quot;verboseornoverbose $verboseornoverbose\n&amp;quot; if $verboseornoverbose;&lt;br /&gt;
...&lt;br /&gt;
print &amp;quot;Unprocessed by Getopt::Long\n&amp;quot; if $ARGV[0];&lt;br /&gt;
foreach (@ARGV) {&lt;br /&gt;
    print &amp;quot;$_\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Diamond Operator ==&lt;br /&gt;
If the command line arguments are a list of files that you want to read through, use the diamond operator to read through each one sequentially: &lt;br /&gt;
&amp;lt;source lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
while (&amp;lt;&amp;gt;){&lt;br /&gt;
    chomp;  # operates on $_&lt;br /&gt;
    # do something with the current line in $_&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Call it like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./my_program file1 file2 file3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./my_program file*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It reads each file line by line, giving an error if a file can&amp;#039;t be opened. &lt;br /&gt;
&lt;br /&gt;
== User Input from the Command Line ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
print &amp;quot;Enter your name: &amp;quot;;&lt;br /&gt;
chomp( my $name = &amp;lt;STDIN&amp;gt;);&lt;br /&gt;
print &amp;quot;Hello, $name!\n&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Scott</name></author>
	</entry>
</feed>