[lug] perl question

Brad Doctor bdoctor at ps-ax.com
Sun Jun 9 19:32:36 MDT 2002


You may try this:

chomp($foo = <STDIN>);

This will read everything up to the first keystroke, and assign it to $foo.
I would also consider using "strict", via the following:

use strict; # put at the top of your script

This will ensure that all variables are doing what you want them to do.  In
general, it is good practice to do this with perl, and actually helps lessen
coding errors.

One other thing on user input.  If you want to validate what the user passed,
which is a good idea, look into the "redo" blocks, like such:

#!/usr/bin/perl
use strict;
my $val  = undef;
my $null = undef;

GET_INPUT: {

  print "Enter a value: ";
  chomp($val = <STDIN>);

    if ( $val ne "value" ) {
          print "Invalid input, please press enter to continue ";
          chomp($null = <STDIN>);
          redo GET_INPUT;
     } # if
       else {
          print "Correct entry, thanks!\n";
     } # else
 } # redo GET_INPUT block

You can also do the validation quickly like:
 
  redo GET_INPUT if ( $val ne "value" );

Good luck!
-brad


> 
> 
> 
> hello,
> 
>   I have a small perl script that can be passed 2 args. before i made
> the script take args it worked great , now that it takes args i am
> having to rewrite the scrip because everytime i request user input with..
> $foo = <>;
> it gets messed up and doesent ask for user input and just keeps going 
> assinging
> $foo odd stuff. Before user input went smooth. the request for user input is
> in a while loop nested in a else. Dont know if that makes a diff but thought
> i should mention it. If this is not enough info or just plain dumb , i'm 
> sorry.
> i will gladley post the scrip its very small.
> 
> thanks,
> jd
> 
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
> 


-- 
Brad Doctor, CISSP



More information about the LUG mailing list