Perl Script with Command Line Options

Posted on 2011-10-24
Last Modified: 2012-05-12
I need help creating a Perl script that will accept command line switches using a dash to distinguish the switch.

example: -n var1 -b var2
Question by:abgtemp
    LVL 34

    Accepted Solution

    Try looking for getopt.  There are plenty of pages out there with details.
    LVL 14

    Expert Comment

    You can do this with argv, just as with C code.

    You can also enable perl auto-parsing of command line flags, which will set variables, by using the -s switch.
    LVL 14

    Assisted Solution

    As an example, using perl -s, your command line would look like: -n=var1 -b=var2

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

    LVL 28

    Expert Comment

    Use Getopt::Long it's a core module and very flexible.
    LVL 4

    Expert Comment

    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.

    Author Closing Comment

    Having not worked with perl, it would have been nice if an example was provided when referencing getopt
    LVL 28

    Expert Comment

    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.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now