473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Passing arguments to perl script

I have written a perl script in which i have mentioned 4 $ARGV[0], $ARGV[1],...etc .
While passing an argument to the script i want to pass arguments as strings.
Like one argument value should contain n number of characters and strings, eg $ARGV[0] = "This is a script" like wise i want all the arguments to be passed on. Can anyone let me know hows that possible?
Or is there any other function to be used for the same?
Jul 6 '07 #1
2 17292
numberwhun
3,509 Expert Mod 2GB
There is definitely more than one way to handle arguments that are supplied to your script. The way you describe above, including the text in double quotes, does count as one argument because of the quotes. So, if you take this code:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use Getopt::Std;
  4.  
  5. my $no_of_args = $#ARGV + 1;
  6.  
  7. print("There were $no_of_args arguments supplied to this script. \n");
  8.  
and run it using Perl, supplying however many arguments you want, you will find that it will tell you how many you supplied. You would access those supplied elements using the array @ARGV. (If you are on a Linux/Unix machine, be sure to add the #!/usr/bin/perl line to the beginning of the script before running it)

There are other ways of handling arguments though, and one of those is using the Getopt module(s). There are different Getopt modules, such as Getopt::Long and Getopt::Std (which is one of my favorites).

You would use one of those modules if you wanted to define options, such as "-a", "-r", etc, for your users to define for whatever they want to supply to the script.

Regards,

Jeff
Jul 6 '07 #2
miller
1,089 Expert 1GB
cpan Getopt::Long

Observe

Expand|Select|Wrap|Line Numbers
  1. use Getopt::Long;
  2.  
  3. use strict;
  4.  
  5. GetOptions(
  6.     "opt1:s"    => \my $opt1,
  7.     "opt2:s"    => \my $opt2,
  8.     "opt3:s"    => \my $opt3,
  9.     "opt4:s"    => \my $opt4,
  10. );
  11.  
  12. print "Option 1 = '$opt1'\n";
  13. print "Option 2 = '$opt2'\n";
  14. print "Option 3 = '$opt3'\n";
  15. print "Option 4 = '$opt4'\n";
  16.  
  17. 1;
  18.  
  19. __END__
  20.  
And the output

Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl --opt1="a b c" --opt2=test --opt4=bar --opt3=foo
  2. Option 1 = 'a b c'
  3. Option 2 = 'test'
  4. Option 3 = 'foo'
  5. Option 4 = 'bar'
  6.  
- Miller
Jul 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: google_nospam | last post by:
Thanks in advance for any help. I'm looking for a way to pass data from php to perl. Basically, I want to take some dynamic data from a database, mixed with user input, then reformat it to make a...
1
by: Romuald Favre | last post by:
Hi there, I just installed Perl (v. 5.6.1. built for MSWin32 from ActiveState) on a new server Windows 2000. Amazingly the passing of arguments doesn't work ! I saved the following code in a...
1
by: Joe | last post by:
I am trying to write a Perlscript to be used with some HTML pages. Here is how it works: 1.. The first HTML page has a form which requests for user input. Then it passes the QUERY_STRING...
7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: thumbiko | last post by:
I read some discussion on this topic however I am faced with the same problem of passing a php variable into a perl script. I want to pass a string from a php script to a perl script which is called...
0
by: mag | last post by:
I have a make file that executes a perl script that takes PERL as an eval. The script executes the eval() function via an arguement to the script like this: target: script $(ARGUMENT) ...
3
by: vijayarl | last post by:
Hi all, i have perl script, which is used to send mail. its a command line utility. if we run this perl script in command line by passing all it's required arguments, it works very well.there no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.