Hey monks,
I have a subroutine that builds a command. It allows for users to re-enter input if they happen to mess up. I have no problem building everything from input, however, I am not sure how to build the flag if someone passes the variables to the subroutine.
Basically, I am trying to allow the flags to be passed to the subroutine (e.g., buildCommand("bobjones"); ), but if they send that to the subroutine at this point, nothing gets returned...
UPDATE
Sorry, I forgot about the title... Woops! Well, the reason I said "Simple Hash Question" is because I was thinking that maybe it'd be easier to build the flags as a hash and then return them. As you can see, if $user gets passed at this point, it will never be added to $flag as "-user $user". In order for this to occur the individual must select option 1 and then enter the user, even after it is passed. This is obviously because anything passed to the subroutine is not evaluated. I was thinking I could do it this way (see CODE below ORIGINAL CODE) with scalars, but there must be an easier way to do it, possibly with a hash...
ORIGINAL CODE
sub buildCommand {
my ($user,$uid,$group,$home,$create,$shell)=@_;
my $flag;
while (1) {
print qq {
1. User: $user
2. UID: $uid
3. Group: $group
4. Home Dir: $home
5. Comment: $comment
6. Shell: $shell
D. Done
};
print "Please Choose An Option From Above: "
chomp(my $ans=<STDIN>);
if ($ans == 1) {
$user = getInput("Enter Username: ");
$flag = "-user $user";
}
elsif ($ans == 2) {
$uid = getInput("Enter UID: ");
$flag .= " -uid $uid";
}
elsif ($ans == 3) {
$group = getInput("Enter Group: ");
$flag .= " -group $group";
}
elsif ($ans == 4) {
$home = getInput("Enter Home: ");
$flag .= " -home $home";
}
elsif ($ans == 5) {
$comment = getInput("Enter Comment: ");
$flag .= " -comment $comment";
}
elsif ($ans == 6) {
$shell = getInput("Enter Shell: ");
$flag .= " -shell=$shell";
}
elsif ($ans =~ /d/i && defined($user)) {
return($flag);
}
else {
print "INVALID OPTION!\n";
}
}
}
WORKING (BUT NOT VERY GOOD) WAY OF DOING IT
sub buildCommand {
my ($user,$uid,$group,$home,$create,$shell)=@_;
my $flag;
while (1) {
print qq {
1. User: $user
2. UID: $uid
3. Group: $group
4. Home Dir: $home
5. Comment: $comment
6. Shell: $shell
D. Done
};
print "Please Choose An Option From Above: "
chomp(my $ans=<STDIN>);
if ($ans == 1) {
$user = getInput("Enter Username: ");
}
elsif ($ans == 2) {
$uid = getInput("Enter UID: ");
}
elsif ($ans == 3) {
$group = getInput("Enter Group: ");
}
elsif ($ans == 4) {
$home = getInput("Enter Home: ");
}
elsif ($ans == 5) {
$comment = getInput("Enter Comment: ");
}
elsif ($ans == 6) {
$shell = getInput("Enter Shell: ");
}
elsif ($ans =~ /d/i && defined($user)) {
last;
}
else {
print "INVALID OPTION!\n";
}
}
if (defined($user)) {
$flag = "-user $user";
}
if (defined($uid)) {
$flag = " -uid $uid";
}
if (defined($group)) {
$flag = " -group $group";
}
if (defined($home)) {
$flag = " -home $home";
}
if (defined($comment)) {
$flag .= " -comment $comment";
}
if (defined($shell)) {
$flag .= " -shell $shell";
}
return($flag);
}