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

passing a scalar/hash back to main

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
US
if the command eq '?' I want to print a help menu,
else I want to handle a scalar command. How do I deal with $parm in the following example:

#Main:
my ($cmd,$parm) CmdListParser($cmd);
if ( $parm is a SCALAR ) {
handle the scalar;
exit;
}
if ( $parm is a HASH ) {
print the hash;
exit;
}
#End-------------------------------------
sub CmdListParser {
my $input = shift;
my %commandList = (
'cmd1' => 'cmd1 description' ,
'cmd2' => 'cmd1 description' ,
'?' => 'Print This Hash' ,
);

if ( $input eq '?' ) {
return($cmd,%commandList);
}else{
my $output = cmdfunction($input);
return($input,$output);
}

}
 
If the scalar is a reference to a hash or other type of data, you can use the ref() function to determine what the reference points to:


If the scalar is not a reference there is no way to do what you want that I know of.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It looks like you're using that sub like a dispatch table. This might make things easier.

Code:
my %dispatch = (
	'a' => ['Description for option a',
			\&option_a],
	'b' => ['Description for option b',
			\&option_b],
	'?' => ['Prints help / list of options',
			\&print_help]
);

my $choice;
# Get input
$dispatch{$choice}->[1]->();

### SUBS
sub option_a { print "option a sub\n"; }
sub option_b { print "option b sub\n"; }
sub print_help {
	print "\nAvailable options:\n";
	foreach (sort keys %dispatch) {
		print "\t$_\t$dispatch{$_}->[0]\n";
	}
}
Normally you wouldn't put descriptions in the dispatch table, but it might make your help option a little easier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top