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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

initial values in perl

Status
Not open for further replies.

tsp120

Programmer
May 21, 2003
52
US
I want to be able to run my program in unix in this way:

perl filename -p hhc -v 2

Then i want to designate hhc into a variable called '$prefix' and 2 into a variable '$version'. I am under the impression that this is possible. How would I store these values into the appropriate variables?

Note, it is possible that it will not always be necessary to insert an initial prefix and version.

Hope this made sense. Thanks.
 
look up getopts

--Paul
arcnon is right @ARGV is where they go, what, and how you want to deal with them is your choice

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Check out Getopt::Std

or you could roll your own, e.g.
Code:
#!perl
use strict;
use warnings;

my ($prefix, $version);

while (@ARGV && $ARGV[0] =~ /^-/) {
    $_ = shift;
    last if /^--$/;
    if (/^-p$/) {
        $prefix = shift;
    } elsif (/^-v$/) {
        $version = shift;
    } else {
        die qq(Unrecognized option "$_"!\n);
    }
}

print qq(\$prefix = $prefix, \$version = $version\n);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top