Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable into a perl script

Status
Not open for further replies.

Dudek

Technical User
Jan 14, 2002
27
MY
Hi there..

One simple question here. How do i pass a variable into a perl script?

Eg.. at the prompt, i want to type

$scriptname var1 var2 var3

and then in the script..

#!/usr/bin/perl

print var1;
print var2;
print var3;

Thanks
JD
 
Use the '-s' option either from the command line or like

=================================================
#!/usr/bin/perl -s

print "$var1\n";
print "$var2\n";



=================================================

Invoking this script with

'<scriptname> var1 var2=100'

from your command prompt would print

1
100

as the output. The default value for an option passed to a script invoked with the -s option is 1.


HTH

C &quot;Brahmaiva satyam&quot;
-Adi Shankara (788-820 AD)
 
Another way is;

print $ARGV[0];
print $ARGV[1];
print $ARGV[2];

if you invoke your script with paramters than @ARGV is automatically filled up.
 
yet another way.....

$var1 = shift or die &quot;oops(1) $!\n;
$var2 = shift or die &quot;oops(2) $!\n;
$var3 = shift or die &quot;oops(3) $!\n;

print $var1;
print $var2;
print $var3;
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
You could try something like this, hope it helps

if (@ARGV < 3) {
die &quot;Usage: program.pl ARGV[0] ARGV[1] ARGV[2]\n&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top