Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Perl Script with Command Line Options

I need help creating a Perl script that will accept command line switches using a dash to distinguish the switch.

example:
myscript.pl -n var1 -b var2
0
abgtemp
Asked:
abgtemp
2 Solutions
 
johnsoneSenior Oracle DBACommented:
Try looking for getopt.  There are plenty of pages out there with details.
0
 
sentnerCommented:
You can do this with argv, just as with C code.

http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/

You can also enable perl auto-parsing of command line flags, which will set variables, by using the -s switch.
0
 
sentnerCommented:
As an example, using perl -s, your command line would look like:

myscript.pl -n=var1 -b=var2


And your perl script would use the variables "$n" and "$b", which will be undefined if they weren't set.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
FishMongerCommented:
Use Getopt::Long it's a core module and very flexible.
0
 
kyanwanCommented:
I assume this is what you want:

-aLabel=aData -bLabel=bData -cLabel=cData -dLabel="d data"

foreach $arg (@ARGV) {
	($aLabel,$aData) = split (/\=/,$arg); 
	$pArgs{$aLabel}  = $aData; 
}

Open in new window


This will give you a nice hash with your arguments and options easily accessible.  Enjoy.
0
 
abgtempAuthor Commented:
Having not worked with perl, it would have been nice if an example was provided when referencing getopt
0
 
FishMongerCommented:
The Getopt::Long module I linked to provides several examples.  If you needed more explanation, we would have been happy to provide more examples and explanations.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now