Getopt::long samples

Posted on 2007-10-10
Last Modified: 2008-01-09
I'm trying to get some simple Getopt::Long examples going, but nada  so far.  

For example;
c:\> --name=Jerry
Input name is Jerry

# code
use Getopt::Long;
# read options
$result = GetOptions ("name=s" => $name);  

# print value
print "Input name is $name\n";


c:\> --base=10 --height=20
Base = 10
Height = 20
Area = 100

# code
use Getopt::Long;

# read options into hash
$result = GetOptions (%options, "base=i", "height=i");  

# print hash values
print "Base = " . $options{'base'} . "\n";
print "Height = " . $options{'height'} . "\n";
print "Area = " .  0.5 * $options{'base'} * $options{'height'} . "\n";

Question by:Marketing_Insists
    LVL 48

    Accepted Solution

    $result = GetOptions ("name=s" => $name);  

    should be

    $result = GetOptions ("name=s" => \$name);  
    LVL 48

    Expert Comment

    $result = GetOptions (%options, "base=i", "height=i");  

    should be

    $result = GetOptions (\%options, "base=i", "height=i");  

    Author Comment

    Thanks!, That was it.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    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…
    This video discusses moving either the default database or any database to a new volume.

    745 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

    15 Experts available now in Live!

    Get 1:1 Help Now