in reply to Length and Chomp ??

Some wise Monks have shown what length($x) does.
I will show how to use this in a command line application.

First, what starts it and ends it goes right at the front!
We prompt for some input (the string) and give a graceful way to stop (like Q, q, quit QUIT).

After that part, we re-prompt without error message if input line is blank.
Then we clean up the input line as users often put spaces around things. This part along the way cleans up all this "\n" stuff.

Now we enter the 2nd level prompt for the "size".
Same sort of things happen... but we detect and report and re-prompt if something isn't quite right.

I could have written the unless() as an if(). That is a matter of choice and style. However, I do think that the way that I indent these clauses is very good (my opinion). I believe that the "lining up" and spacing of this makes a difference. You can do it this way with either "unless" or "if".

Then we come to the "print". That print won't fail mainly because we know that $size is "good".

This code is pretty typical of UI code...lots of code that finally gets around to a few statements that actually "do something".

Oh, I think it is worthy of notice that there aren't any tricky <CR><LF> vs just <LF> things in the code below.

#!/usr/bin/perl -w use strict; while ( (print "Enter the string(or Quit): "), (my $line = <STDIN>) !~ /^\s*q(uit)?\s*$/i ) { next if ($line =~ m/^\s*$/); #re-prompt if blank line $line =~ s/^\s*//; #delete leading spaces $line =~ s/\s$//; #delete trailing spaces print "Enter the Sub-string size: "; my $size = (<STDIN>); unless( ($size =~ m/^\s*\d\s*$/) #only single digit && (length($line) >= $size) #only valid context ) { print "Invalid size, must be a single positive digit\n"; print " and less than or equal to: ",length($line),"\n"; next; } print "output: ",substr($line,0,$size),"\n"; } __END__ Example Output: C:\TEMP>perlcommand.pl Enter the string(or Quit): asdf Enter the Sub-string size: 8 Invalid size, must be a single positive digit and less than or equal to: 4 Enter the string(or Quit): asdf Enter the Sub-string size: -23 Invalid size, must be a single positive digit and less than or equal to: 4 Enter the string(or Quit): asdf Enter the Sub-string size: 2 output: as Enter the string(or Quit): assf Enter the Sub-string size: 4 output: assf Enter the string(or Quit): q