in reply to Clever Padding

I am curious. Standard printf() formats can pad with leading zeroes or leading spaces.
#!/usr/bin/perl -w use strict; printf("%03d\n", 5); printf("%03d\n", 12345); __END__ Prints: 005 12345
What kind of fixed field application do you have that would require padding with other than zero or space? Maybe you are asking how do deal with 12345 above? And the print should be '123' or '345'?

I looked at your site reference http://www.comp.leeds.ac.uk/Perl/control.html#exercise and there is some really bogus advice there! Use chomp() to get rid of trailing LF or CR,LF, not chop()! chop() deletes the last char in the string no matter what it is! chomp() is conditional and only gets rid of trailing "new line" characters. In the case of Windows, chomp() will get rid of 2 characters (the CR and LF), on Unix only one (the LF) - in case of a simple string without any "new line" characters, nothing happens.

Anyway, please explain your application with something other than zero or space leading pad characters. That would be unusual and I'd like to understand it.

Replies are listed 'Best First'.
Re^2: Clever Padding (chomp, Win32)
by tye (Sage) on Jan 27, 2010 at 22:05 UTC
    In the case of Windows, chomp() will get rid of 2 characters (the CR and LF)

    Ah, yet another person fooled by the misleading statements in perlport, most likely.

    chomp does not remove "\r" characters, even on Win32.

    Actually, I would never use chomp. s/\s+$// is a much better idea. Not only does it remove "\r" characters when on Win32 but it also removes them when on Unix (or elsewhere). Even better, it removes trailing whitespace. Trailing whitespace should never be "significant", so removing it before processing a line is a good general practice.

    - tye        

      My perlport says

      With default settings that function looks for a trailing "\n" character and thus trims in a portable way.

      Did it use to say something different?

        Yeah, it used to say a whole lot more than that. I hope all of perlport (a link to perldoc which appears to be down) hasn't been whittled down to a single sentence. That would surely be less misleading but also clearly much less useful.

        - tye