Logo
  • Home
  • About
  • Services
  • Blogs
  • Career
  • Contact
Logo

The UK's #1 Software Development Company. PodTech IO builds custom software solutions for businesses. Call today for a free consultation

  • Address

    4th Floor, 4 Tabernacle Street London EC2A 4LU
  • Email

    info@podtech.com
  • Contact

    +44 (0) 20 8720 6583

Pass Parameters to Perl Script

  • Home
  • Pass Parameters to Perl Script
  • By podtech
  • In Perl

Pass Parameters to Perl Script

Perl command line arguments stored in the special array called @ARGV.

ARGV example

Use $ARGV[n] to display argument.

Use $#ARGV to get total number of passed argument to a perl script.

For example, if your scriptname is foo.pl and you called script as follows:

./foo.pl one two three

You can print one, two, three command line arguments with print command:
[perl]
print “$ARGV[$0]\n”;
print “$ARGV[$1]\n”;
print “$ARGV[$2]\n”;
[/perl]
 

Or just use a loop to display all command line args:

[perl]
#!/usr/bin/perl -w
if ($#ARGV != 2 ) {
print “usage: mycal number1 op number2\neg: mycal 5 + 3 OR mycal 5 – 2\n”;
exit;
}
$n1=$ARGV[0];
$op=$ARGV[1];
$n2=$ARGV[2];
$ans=0;
if ( $op eq “+” ) {
$ans = $n1 + $n2;
}
elsif ( $op eq “-“){
$ans = $n1 – $n2;
}
elsif ( $op eq “/”){
$ans = $n1 / $n2;
}
elsif ( $op eq “*”){
$ans = $n1 * $n2;
}
else {
print “Error: op must be +, -, *, / only\n”;
exit;
}
print “$ans\n”;
[/perl]

Save and run script as follows:

[bash]
$ chmod +x mycal.pl
$ ./mycal.pl
$ ./mycal.pl 5 + 3
$ ./mycal.pl 5 \* 3
[/bash]

Note: * need to be escaped under UNIX shell.

ARGV examplePass ParametersPass Parameters to Perl ScriptPerlPerl Script
change passwd – permission denied (NIS)
Shell, KSH Read Variables

Recent Posts

  • DevOps as a Software Development Methodology
  • How Does Spiral Software Development Methodology Work?
  • Waterfall Software Development Methodology And When To Use It
  • What Exactly Is Lean Software Development Methodology?
  • What is Kanban and How Does It Work?

PodTech IO

The UK's #1 Software Development Company. PodTech IO builds custom software solutions for businesses. Call today for a free consultation

Company

  • About
  • Services
  • Contact
  • Blogs
  • Career

Terms

  • Terms & Conditions
  • Privacy Policy

Contact Us

  • ADDRESS

    Headquarter: Tabernacle Street London India: Vi john Tower, Gurgaon, Haryana
  • EMAIL

    info@podtech.com
  • CONTACT

    +44 (0) 20 8720 6583

Copyright ©2023 PodTech IO. All Rights Reserved

Logo