CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Writing multiple lines - if required



Nov 21, 2012, 1:51 AM

Post #1 of 2 (3231 views)
Writing multiple lines - if required Can't Post

Hi All,

The following code I have written takes a user inputted 'service instance' and writes it to a file. Once done, the user is asked whether they wish to delete the 'service instances' and if "Y" is chosen, the next part of the script is executed.

Currently, the user is only able to input one service instance at a time (due to the loop) however, I would like to have the option of either pasting a big list of 'services instances' or being able to type one by one how it is now.

It is technically possible to paste in data now, it just means the loop will run "x" amount of times until it is all added, however this looks messy and inefficient.

Below is the code and how it looks when run, any help is greatly appreciated.

When run:

Please type the next service instance, 
or just press enter to finish...
adding service instance Hi...

Please type the next service instance,
or just press enter to finish...
adding service instance How ...

Please type the next service instance,
or just press enter to finish...
adding service instance Are...

Please type the next service instance,
or just press enter to finish...

No more service instances to add...

Do you want to run the delete? (Y/N)



my $nullcnt = 1;
my $servinst;
my $sdate = `date "+%Y%m%d"`;
chomp $sdate;

my $rad_home_dir = '$RAD_HOME';
my $outdir = "/opt/netcool/rad/scripts/radsh/";
my $outfile = join("", "RAD-Delete-", $sdate, ".radsh");
my $outname = join("", $outdir, $outfile);
my $delpath = $outdir;

#Clear screen and print message on screen#

print "\n";
print "*************** Welcome to RAD Service Instance Auto-Delete ***************\n";

#pause for 1 second#


#run sub writeline2#


while ($nullcnt) {

&writelines if ($nullcnt);
print "\n";
print "Do you want to run the delete? (Y/N)\n";

my $userconfirm = <STDIN>;
chomp ($userconfirm);
my $userconfirm = uc($userconfirm);

if ($userconfirm eq "Y") {

else {
print "\nDelete cancelled, no deletes will be made.\n";


print "\n";

# sub-routines #

sub readline {
print "\n";
print "Please type the next service instance,\n";
print "or just press enter to finish...\n";

$servinst = <STDIN>;
chomp ($servinst);

if ($servinst ne "") {

else {
print "No more service instances to add...\n";

$nullcnt = 0;

sub writelines {

print "adding service instance $servinst...\n";

my $bit1 = 'deleteServiceInstance(new String[] { "';
my $bit2 = '" });';
my $line2 = join("", $bit1, $servinst, $bit2);

open (OUTNAME,">>$outname");
print OUTNAME "$line2\n";

sub writeline2 {

my $line1 = 'rad.setUserForSession("root");';

open (OUTNAME,">$outname");
print OUTNAME "$line1\n";


sub rundelete {

print "\n";
print "Changing to directory to run delete script: ";


print "$delpath\n";

sleep (2);

chdir($delpath) or die "Cant change directory\n";

exec "nohup cat $outfile | $rad_home_dir/bin/rad_radshell > /tmp/deletes.out";


Nov 21, 2012, 5:14 AM

Post #2 of 2 (3228 views)
Re: [sammclean23] Writing multiple lines - if required [In reply to] Can't Post

I personally dislike strongly any user interaction on the command line, especially when it comes to entering a bunch of information (what if the user, after having entered 10 services already, finds out that he made a mistake when he had entered the first service?), but if you REALLY want to do it this way, here we go:

If you want your program to handle either single-line or multiline answers, you need to distinguish between theses cases. For example, you could require the user to end his line with a comma if he wants to continue with the next line. Actually, you can make the comma a valid separator, which means that the user can alternatively enter several services in a single line.

Also, you could allow the user to put the service names into a file and read them from the file. Again, I would handle this in different way - via the command line -, but you could reserve a special notation (say: @FILENAME) for saying, that this is not the name of a service, but that FILENAME is the name of a file containing a list of services.

As always, there are many ways to skin a cat. Maybe it's best if you see this problem from the user's viewpoint: If you had to use this program a multiple of times, how would you like the user interface to be?


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives