Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Simple Hash Question

by walkingthecow (Friar)
on May 22, 2009 at 05:31 UTC ( [id://765607]=perlquestion: print w/replies, xml ) Need Help??

walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Simple Hash Question
by targetsmart (Curate) on May 22, 2009 at 06:02 UTC
    IMO your subroutine seems to be fine
    however, you may have problems in the place where you are calling this subroutine
    at all these tough times, perl -d is your helper; use it; see perldebug
    BTW your post title, 'simple hash question' seems to be irrelevant to the post!.

    UPDATE
    good that you know you can use hashes, why you have not tried it?.
    see here about hashes
    for your case you can use some thing like below

    my %flag; .. $flag{'user'} = $user; .. $flag{'uid'} = $uid; ... ... return %flag; at the calling place use my %userflags = buildCommand(..) then access the values like $userflags{'user'} or use references return \%flag; at the calling place use my $userflags = buildCommand(..) then access the values like $userflags->{'user'}
    see perlref for more details on references.
    learning perl is a good place to start.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      Hm, so I am still at a roadblock here. I guess I do not understand how I would do it... Here is what I have, but as you can see I still have to evaluate what comes in... The problem is, this: "my ($user,$uid,$group,$home,$create,$shell)=@_;" is not doing anything at all. It is never evaluated, and I am unsure a nice clean way of doing that.
      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) { $flag{'user'} = getInput("Enter Username: "); } elsif ($ans == 2) { $flag{'uid'} = getInput("Enter UID: "); } elsif ($ans == 3) { $flag{'group'} = getInput("Enter Group: "); } elsif ($ans == 4) { $flag{'home'} = getInput("Enter Home: "); } elsif ($ans == 5) { $flag{'comment'} = getInput("Enter Comment: "); } elsif ($ans == 6) { $flag{'shell'} = getInput("Enter Shell: "); } elsif ($ans =~ /d/i && defined($user)) { last; } else { print "INVALID OPTION!\n"; } } while (($key, $value) = each(%flag)){ $flag .= "-$key $value "; } return(%flag); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://765607]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-18 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found