Perl

From Rohidekar

Jump to: navigation, search

Deprecated

Contents

How do I pad a string with blanks or pad a number with zeroes?

    # Right padding a string with blanks (no truncation):
    $padded = sprintf("%-${pad_len}s", $text);

http://perl.active-venture.com/pod/perlfaq4-datastrings.html


To sort an array in ASCII (Alphabetical) order

sort(@array);

http://www.htmlite.com/perl016.php


HTML Generator for CGI scripts

Most of main() deals with putting together bits of HTML. For a real-world script, use something like HTML::Template or Template Toolkit instead of putting HTML directly in the script. http://www.perl.com/pub/a/2006/03/02/ajax_and_perl.html?page=2

Find and Execute

  find . -name "*.jar" -exec jar -tvf {} \;

Regular Expressions, String manipulation ec

How do I use POSIX in regular expressions?

To use the POSIX character classes, they must be within another character class:

for(split(//,$line)) {
  if (/[[:print:]]/) { print; }
}

Using a POSIX class on its own:

if (/[:print:]/) { } # WRONG!

won't have the intended effect. The previous bit of code would match :, p, r, i, n, and t.

Appending strings

  • $html .= "

    $err

    ";

Include newline in regex wildcard

undef $/;

Capitalize first letter of word

$string =~ s/(\w+)/\u$1/g;
should work just fine

http://stackoverflow.com/questions/77226/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-in-perl

Personal tools