473,413 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

arguments problem

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
8 1750
numberwhun
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
peruron
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
4,059 Expert 2GB
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
peruron
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

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

Similar topics

8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
2
by: Steven D'Aprano | last post by:
I'm trying to keep an open mind, but I am perplexed about something in Python that strikes me as a poor design. py> def func(a,b): py> print a,b py> func(1) Traceback (most recent call...
12
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of...
7
by: A. Saksena | last post by:
Hi all, Is it possible to write a function or a macro in C++, which is capable of accepting any number of arguments. To give an example, the following should be possible: - ...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
0
by: Max TenEyck Woodbury | last post by:
I have a static array that requires quite complicated initialization. With C89 I constructed a set of macros that greatly simplified that process but required the user to count the number of...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
3
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say template<typename Atype, typename Btype, typename Ctype> class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.