opendir & grep:
opendir(DIR,"$SCANDATA");
@files = sort(grep(/[0-9]*\.[0-9]*\-/,readdir(DIR)));
closedir(DIR);
foreach:
foreach $FILE (@FILES) { }
hash iteration:
while (($key,$val) = each (%hash)) { }
referencing a subroutine:
$SUBNAME = "junk";
&$SUBNAME(@PARAMETERS);
capitalizing:
word: $foo = ucfirst lc $foo;
each word: $foo =~ s/(\w+)/\u\L$1/g;
passing array/hash by reference
sub test {
local(*OPTS,*USERSET,*FIELDS) = @_;
..... }
&test (\%options,\%USERLIST,\@FIELDLIST);
select output
$OLDFH = select();
open(TXT,"> report.txt");
select(TXT);....
select($OLDFH);
getopt
use Getopt::Std;
%options=();
getopts("he:f:s:SD:u:F:",\%options);
if ($options{h}) { &usage(); }
Parsing Directory & File Names:
use File::Basename;
($name,$path,$suffix) = fileparse($fullname,@suffixlist)
fileparse_set_fstype($os_string);
$basename = basename($fullname,@suffixlist);
$dirname = dirname($fullname);
($name,$path,$suffix) = fileparse("lib/File/Basename.pm","\.pm");
fileparse_set_fstype("VMS");
$basename = basename("lib/File/Basename.pm",".pm");
$dirname = dirname("lib/File/Basename.pm");
fileparse - split a pathname into pieces
basename - extract just the filename from a path
dirname - extract just the directory from a path
optimal byte units (from here)
sub utils_convert_bytes_to_optimal_unit{
my($bytes) = @_;
return '' if ($bytes eq '');
my($size);
$size = $bytes . ' Bytes' if ($bytes < 1024);
$size = sprintf("%.2f", ($bytes/1024)) . ' KB' if($bytes >= 1024 && $bytes < 1048576);
$size = sprintf("%.2f", ($bytes/1048576)) . ' MB' if($bytes >= 1048576 && $bytes < 1073741824);
$size = sprintf("%.2f", ($bytes/1073741824)) . ' GB' if ($bytes >= 1073741824 && $bytes <1099511627776);
$size = sprintf("%.2f", ($bytes/1099511627776)) .' TB' if ($bytes >= 1099511627776);
return $size;
}
Keywords: cheatsheet perl