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); }

In reply to Simple Hash Question by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.