arguments problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peruron
    New Member
    • Aug 2007
    • 11

    arguments problem

    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.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by peruron
    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

    Comment

    • SeniorSE
      New Member
      • Mar 2008
      • 3

      #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

      Code:
      #!/usr/bin/perl
      #----------------------------------------------------------------------#
      #  Copyright 1998 by DevDaily Interactive, Inc.  All Rights Reserved.  #
      #----------------------------------------------------------------------#
      
      $username = &promptUser("Enter the username");
      $password = &promptUser("Enter the password");
      $homeDir  = &promptUser("Enter the home directory", "/home/$username");
      
      print "$username, $password, $homeDir\n";
      
      exit;
      
      #----------------------------(  promptUser  )-----------------------------#
      #                                                                         #
      #  FUNCTION:	promptUser                                                #
      #                                                                         #
      #  PURPOSE:	Prompt the user for some type of input, and return the    #
      #		input back to the calling program.                        #
      #                                                                         #
      #  ARGS:	$promptString - what you want to prompt the user with     #
      #		$defaultValue - (optional) a default value for the prompt #
      #                                                                         #
      #-------------------------------------------------------------------------#
      
      sub promptUser {
      
         #-------------------------------------------------------------------#
         #  two possible input arguments - $promptString, and $defaultValue  #
         #  make the input arguments local variables.                        #
         #-------------------------------------------------------------------#
      
         local($promptString,$defaultValue) = @_;
      
         #-------------------------------------------------------------------#
         #  if there is a default value, use the first print statement; if   #
         #  no default is provided, print the second string.                 #
         #-------------------------------------------------------------------#
      
         if ($defaultValue) {
            print $promptString, " [", $defaultValue, "]: ";
         } else {
            print $promptString, ": ";
         }
      
         $| = 1;               # force a flush after our print
         $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)
      
      
         #------------------------------------------------------------------#
         # remove the newline character from the end of the input the user  #
         # gave us.                                                         #
         #------------------------------------------------------------------#
      
         chomp;
      
         #-----------------------------------------------------------------#
         #  if we had a $default value, and the user gave us input, then   #
         #  return the input; if we had a default, and they gave us no     #
         #  no input, return the $defaultValue.                            #
         #                                                                 # 
         #  if we did not have a default value, then just return whatever  #
         #  the user gave us.  if they just hit the <enter> key,           #
         #  the calling routine will have to deal with that.               #
         #-----------------------------------------------------------------#
      
         if ("$defaultValue") {
            return $_ ? $_ : $defaultValue;    # return $_ if it has a value
         } else {
            return $_;
         }
      }
      
      __END__

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • savanm
          New Member
          • Oct 2006
          • 85

          #5
          Originally posted by peruron
          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.

          [code=perl]
          use strict;
          use Win32;

          my $arg1=$ARGV[0];
          my $arg2=$ARGV[1];
          my $cnt=@ARGV;
          if($cnt<2)
          {
          Win32::MsgBox(" Argument is Missing");
          exit;
          }
          print $cnt;
          my $input=<STDIN>;

          print $arg1;
          print $arg2;
          print "user inp $input";
          [/code]

          u should validate the arguments.

          This idea may help u...
          Last edited by numberwhun; Mar 12 '08, 11:56 AM. Reason: add code tags

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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

            Comment

            • peruron
              New Member
              • Aug 2007
              • 11

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

              Code:
              my $serial = $ARGV[0];
              my $newVal = $ARGV[1];
              ...
              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.

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by peruron
                hello again, thanks for trying to help! My code (well, relevant parts):

                Code:
                my $serial = $ARGV[0];
                my $newVal = $ARGV[1];
                ...
                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:

                Code:
                my $serial = $ARGV[0];
                my $newVal = $ARGV[1];
                ...
                print "you're about to edit ".$serial.". If it's ok, press 1, otherwise press 0\n";
                print "Enter some more input:";
                chomp (my $newinput =  <STDIN>);
                .....

                Comment

                • peruron
                  New Member
                  • Aug 2007
                  • 11

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

                  Comment

                  Working...