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

In reply to Re: Length and Chomp ?? by Marshall
in thread Length and Chomp ?? by snape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.