#!/usr/bin/perl -w use strict; while ( (print "Enter the string(or Quit): "), (my $line = ) !~ /^\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 = (); 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