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.
8 1750
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
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 -
#!/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__
-
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
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. -
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";
-
u should validate the arguments.
This idea may help u...
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
hello again, thanks for trying to help! My code (well, relevant parts): - 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.
hello again, thanks for trying to help! My code (well, relevant parts): - 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: - 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>);
-
.....
-
Hurray! It was the "\n" at the end of the input... I've chomped it and now it's ok. Thanks a lot.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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: -
...
|
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...
|
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 +=...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |