By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

arguments problem

P: 11
Hello!

I'm working on a short perl script that receives 2 arguments in the command line (or at least, that's what I'd like it to do), and puts them in variables. Afterwards, the script should pop a question and wait for user input. My problem is, that it looks like one of the arguments is being taken as input, so the script won't wait for input, and later fail.

Can anyone please tell me how can I make @ARGV invisible to <> after I've assigned the variables?


Thanks a lot.
Mar 11 '08 #1
Share this Question
Share on Google+
8 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hello!

I'm working on a short perl script that receives 2 arguments in the command line (or at least, that's what I'd like it to do), and puts them in variables. Afterwards, the script should pop a question and wait for user input. My problem is, that it looks like one of the arguments is being taken as input, so the script won't wait for input, and later fail.

Can anyone please tell me how can I make @ARGV invisible to <> after I've assigned the variables?


Thanks a lot.
First, we need to see your code to evaluate what you have done. If we can't see it, we can't tell you what is going wrong.

Regards,

Jeff
Mar 11 '08 #2

P: 3
there is a good promptUser script that I found on the internet. I am copying it here. You'll figure out how to use it.

Good luck,
Mark

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #----------------------------------------------------------------------#
  3. #  Copyright 1998 by DevDaily Interactive, Inc.  All Rights Reserved.  #
  4. #----------------------------------------------------------------------#
  5.  
  6. $username = &promptUser("Enter the username");
  7. $password = &promptUser("Enter the password");
  8. $homeDir  = &promptUser("Enter the home directory", "/home/$username");
  9.  
  10. print "$username, $password, $homeDir\n";
  11.  
  12. exit;
  13.  
  14. #----------------------------(  promptUser  )-----------------------------#
  15. #                                                                         #
  16. #  FUNCTION:    promptUser                                                #
  17. #                                                                         #
  18. #  PURPOSE:    Prompt the user for some type of input, and return the    #
  19. #        input back to the calling program.                        #
  20. #                                                                         #
  21. #  ARGS:    $promptString - what you want to prompt the user with     #
  22. #        $defaultValue - (optional) a default value for the prompt #
  23. #                                                                         #
  24. #-------------------------------------------------------------------------#
  25.  
  26. sub promptUser {
  27.  
  28.    #-------------------------------------------------------------------#
  29.    #  two possible input arguments - $promptString, and $defaultValue  #
  30.    #  make the input arguments local variables.                        #
  31.    #-------------------------------------------------------------------#
  32.  
  33.    local($promptString,$defaultValue) = @_;
  34.  
  35.    #-------------------------------------------------------------------#
  36.    #  if there is a default value, use the first print statement; if   #
  37.    #  no default is provided, print the second string.                 #
  38.    #-------------------------------------------------------------------#
  39.  
  40.    if ($defaultValue) {
  41.       print $promptString, " [", $defaultValue, "]: ";
  42.    } else {
  43.       print $promptString, ": ";
  44.    }
  45.  
  46.    $| = 1;               # force a flush after our print
  47.    $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)
  48.  
  49.  
  50.    #------------------------------------------------------------------#
  51.    # remove the newline character from the end of the input the user  #
  52.    # gave us.                                                         #
  53.    #------------------------------------------------------------------#
  54.  
  55.    chomp;
  56.  
  57.    #-----------------------------------------------------------------#
  58.    #  if we had a $default value, and the user gave us input, then   #
  59.    #  return the input; if we had a default, and they gave us no     #
  60.    #  no input, return the $defaultValue.                            #
  61.    #                                                                 # 
  62.    #  if we did not have a default value, then just return whatever  #
  63.    #  the user gave us.  if they just hit the <enter> key,           #
  64.    #  the calling routine will have to deal with that.               #
  65.    #-----------------------------------------------------------------#
  66.  
  67.    if ("$defaultValue") {
  68.       return $_ ? $_ : $defaultValue;    # return $_ if it has a value
  69.    } else {
  70.       return $_;
  71.    }
  72. }
  73.  
  74. __END__
  75.  
Mar 12 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
SeniorSE,

Please know that my asking for the OPs code was not an invitation for you to just throw code at him/her from the internet. The OP is trying to learn and in that process, I wanted him to provide the code they are working on so we can help them get it right.

Hopefully, the OP will provide their code and still be determined to get it working and figure out what was going awry.

Regards,

Jeff
Mar 12 '08 #4

savanm
P: 85
Hello!

I'm working on a short perl script that receives 2 arguments in the command line (or at least, that's what I'd like it to do), and puts them in variables. Afterwards, the script should pop a question and wait for user input. My problem is, that it looks like one of the arguments is being taken as input, so the script won't wait for input, and later fail.

Can anyone please tell me how can I make @ARGV invisible to <> after I've assigned the variables?


Thanks a lot.

Hello peruron

If u have any consequence in ur code, U Should show ur code in this forum.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Win32;
  3.  
  4. my $arg1=$ARGV[0];
  5. my $arg2=$ARGV[1];
  6. my $cnt=@ARGV;
  7. if($cnt<2)
  8. {
  9. Win32::MsgBox("Argument is Missing");
  10. exit;
  11. }
  12. print $cnt;
  13. my $input=<STDIN>;
  14.  
  15. print $arg1;
  16. print $arg2;
  17. print "user inp $input";
  18.  
u should validate the arguments.

This idea may help u...
Mar 12 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
savanm,

First, after 82 posts, we expect you to know how to use code tags when posting in the forums. Second, please read my last post to this thread, directed at SeniorSE. They posted code as well, after I REQUESTED code from the user. As I told them, that was not an invitation to just give the OP the answer. They are trying to learn and we are trying to help them.

Regards,

Jeff
Mar 12 '08 #6

P: 11
hello again, thanks for trying to help! My code (well, relevant parts):

Expand|Select|Wrap|Line Numbers
  1. my $serial = $ARGV[0];
  2. my $newVal = $ARGV[1];
  3. ...
  4. print "you're about to edit ".$serial.". If it's ok, press 1, otherwise press 0\n";
At this point, I think, the scripts takes the ARGV as input, or at least, won't wait for me to give input, but moves on and fails later. I've tried to empty ARGV, but wasn't very successful.

thanks again.
Mar 13 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
hello again, thanks for trying to help! My code (well, relevant parts):

Expand|Select|Wrap|Line Numbers
  1. my $serial = $ARGV[0];
  2. my $newVal = $ARGV[1];
  3. ...
  4. print "you're about to edit ".$serial.". If it's ok, press 1, otherwise press 0\n";
At this point, I think, the scripts takes the ARGV as input, or at least, won't wait for me to give input, but moves on and fails later. I've tried to empty ARGV, but wasn't very successful.

thanks again.
You have to explain what you are doing if you expect to get help. In your above code $serial and $newVal are assigned the first two values you pass to the script:

perl yourscript.pl arg1 arg2

if you need to get more input you have to ask for it:

Expand|Select|Wrap|Line Numbers
  1. my $serial = $ARGV[0];
  2. my $newVal = $ARGV[1];
  3. ...
  4. print "you're about to edit ".$serial.". If it's ok, press 1, otherwise press 0\n";
  5. print "Enter some more input:";
  6. chomp (my $newinput =  <STDIN>);
  7. .....
  8.  
Mar 13 '08 #8

P: 11
Hurray! It was the "\n" at the end of the input... I've chomped it and now it's ok. Thanks a lot.
Mar 13 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.