Perl Misc

$. Line number
$_ Input line
$” This is similar to $,, except it applies to array values interpolated into a double quoted string.
$, Field separator
$\ Output Record Separator
$/ Input Record Separator

-F Input field separator
-n
-e
-E Just like -e, plus enabling Perl 5.10 features
-i Inline edit
-p Print each line right away
-l Add new line
-a Split input line into @F
-00 Turns paragraph slurp mode on.

-0777 Together with –n causes Perl to slurp the whole file into $_.
perl -00pe0 # here code is 0, which does nothing

@F
@{[reverse @F]}

\d

\012, \015

%-5d Alignes to left.

a..z
“a”..”z”

perl -e “print map { (a..z)[rand 26] } 1..8″

-p:
while(<>){
   # your program goes here
} continue {
   print or die “-p failed: $!\n”;
}

somecode unless /^$/
somecode if /reg/

$n = () = <>
$n = ( () = <> )
scalar( () = <> )

perl -e “}{ print $.”
   # this is like:
   while (<>) {
   }{ # eskimo operator here
      print $.;
   }
  
perl –alne ‘map { /regex/ && $t++ } @F; END {print $t || 0 }’

y/a-z/A-Z/

print if 1..10   print first 10 lines

print if eof  prints last line.

scalar(grep { /./ } <> )
~~grep/./,<>

$x+0: this can be useful if $x is not initialized.

perl -ne “/regex/ && $last && print $last; $last = $_”
   print every line preceding a line that matches a RE.
  
perl -ne “$. == 13 && print && exit”

perl -ne “print && $. != 27″

perl -ne “print if $. == 13 || $. == 19″

flip-flop operator/range operator
perl -ne “print if /regex1/../regex2/”
   print all lines between two REs

perl -ne “print if ++$a{$_} == 2″
   print all repeated lines only once

perl -ne “print unless $a{$_}++”
   print unique lines

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>