Code:
Variables need to be explicitly defined and initialized. For example, optionx.
If no variables are defined and explicitly initialized, and no options are specified on the command line, the variables corresponding to the options are undefined.
# ! /Bin/perl-5.8.3/bin/perl $
Use Warnings; $
Use Strict; $
UseData: dumper; $
UseGetopt: long; $
UsePod: usage; $
Our $ G_opts ;
Our $ Optionx = '' ; # If not defined in command line, it will be empty string
Sub Parse_opts {
My $ Result = Getoptions (
" Optiona = s " => \ $ G_opts -> { ' Optiona ' }, # String
" Optionb = s " => \$ G_opts -> { ' Optionb ' }, # String
" Optionc = I " => \ $ G_opts -> { ' Optionc ' }, # Integer
" Optiond = f " => \ $ G_opts -> { ' Optiond ' }, # Float
" Optionx = f " => \ $ Optionx ,
" Optiony = f " => \ $ Optiony ,
" Verbose " => \ $ G_opts -> { ' Verbose ' }, # Flag
" Quiet " => Sub { $ G_opts -> { ' Verbose ' } = 0 },
" Help |? " => \$ G_opts -> { ' Help ' }
);
If (! ( $ G_opts -> { ' Optiona ' })){
& Pod2usage (-verbose => 1 ); # Exit status will be 1
}
If ( $ G_opts -> { ' Help ' }){
& Pod2usage (-verbose => 1 ); # Exit status will be 1
}
}
& Parse_opts ();
Print("\ N $ optionx \ n");
Print($ Optiony);#If not defined in command line, it will be undefined
Print($ G_opts-> {"Optionb"});
Foreach My $ Key ( Keys % { $ G_opts }) {$
If (! $ G_opts -> { $ Key }){ Next ;} $
Print ( $ Key . " = " . $ G_opts -> { $ Key }. " \ N " ); $
} $
$
Exit ( 0 ); $
$
$
_ End __ $
$
= Head1 name $
$
Sample-using getopt: Long and POD: usage $
$
= Head1 synopsis $
$
Sample [Options] [ARGs...] $
$
Options: $
$
-Optiona $
-Optionb $
-Optionc $
-Optiond $
-Verbose $
-Quiet noverbose $
-Help brief help message $
$
= Head1 options $
$
= Over 8 $
$
= Item B <-help> $
$
Print A brief help message and exits. $
$
= Back $
$
= Head1 description $
$
B <this program> will Read The given input file (s) and Do Something $
Useful with the contents thereof. $
$
= Cut $
Complete!