# file: PerlCheatSheet.pl v.1.1.0 docs at the end
# INSTALLING MODULES ON PERL
perl -e -MCPAN "install GD"
ppm search module:name
#READ ALL ROWS OF A FILE TO ALIST
open (INPUT, "<$inputfile") || print "could not open the file!\n"; @myfile = <INPUT>;close INPUT;
#FOREACH LINE OF THE FILE DO SOMETHING
foreach my $line ( @lineArray )
{
print "$line" ;
} #eof foreach
# END FOREACH LINE
#GET A NICE TIME
sub timestamp {
#
# Purpose: returns the time in yyyymmdd-format
#
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#---- change 'month'- and 'year'-values to correct format ----
$min = "0$min" if ($min < 10);
$hour = "0$hour" if ($hour < 10);
$mon = $mon + 1;
$mon = "0$mon" if ($mon < 10);
$year = $year + 1900;
$mday = "0$mday" if ($mday < 10);
return "$year$mon$mday" . "_" . "$hour$min" . "_" . $sec;
} #eof timestamp
# READ ALL ROWS TO LIST
open (FILE,$file);
@rows = <FILE>;
close (FILE);
# GREP ALL ROWS CONTAINING 'MACHINE=' (TO A LIST)
@list = grep(/machine=/,@rows);
# A NICE WAY TO CONCATENATE A STRING TO THE SAME STRING
#--- Collect agents from yesterday ------------------------------
$sql = "select distinct(AgentName),LocationName from TransactionDetails td ";
$sql .= "inner join Applications ap on td.ApplicationID = ap.ApplicationID ";
$sql .= "inner join Agents ag on td.AgentID = ag.AgentID ";
$sql .= "inner join Locations lo on td.LocationID = lo.LocationID ";
$sql .= "where ApplicationName like \'" . $APPLICATION . "\' and ";
$sql .= "StartTime between \'" . $DAY2 . "\' and \'" . $DAY1 . "\' ";
$sql .= "order by 1";
# THE EQUIVALENT OF THE SET COMMAND TROUGH THE COMMAND PROMT
foreach $i (keys %ENV)
{
print FH "$i : $ENV{$i} \n";
}
#accept a command line parameter
our $CycleScriptRun_pid = $ARGV[0] ;
#==========================================================
#start a Windows process
use Win32 ;
use Win32::Process ;
my $DayCycle_path= $varHolder->{'BASEDIR'} . " DayCycle.exe" ;
if( $varHolder->{VISIBLE_MODE} == 1)
{ Win32::Process::Create( $DayCycle_o, "$DayCycle_path", "" , 0,
CREATE_NEW_CONSOLE ,$varHolder->{'BASEDIR'}); }
else
{ Win32::Process::Create( $DayCycle_o, "$DayCycle_path", "" , 0,
CREATE_NO_WINDOW ,$varHolder->{'BASEDIR'}); }
$DayCycle_pid = $DayCycle_o->GetProcessID() ;
#==========================================================
#==========================================================
#Purpose : to tie a file
#============================= =============================
use Tie::File ;
my @file_array = ();
my $file = 'C:/Temp/folder/settings.txt' ;
tie @file_array , 'Tie::File', $file , memory => 100_000_000 or die "I cannot find the $file" ;
foreach (@file_array) #foreach line of the file
{ s/$setting=(\d+)/$setting=$value/;
}
untie @file_array ;
#==========================================================
#==========================================================
# create a nameless hash
#==========================================================
my $record = #one record for each cycle
{
APP=> "$csAppName" ,
SCRIPT=> "$psCurrentScript" ,
STEP=> "$psCurrentStep"
} ;
#==========================================================
#==========================================================
#simple perl regex cheatsheet
#==========================================================
Operators
m// 'Match' operator
s/// 'Substitute' operator
tr/// 'Translate' operator
# Special Characters
. Any single character escept newline n
b Between word and non-word chars, ie: /bcat/b/ matches "cat" but not "scat"
B NOT between word and non-word chars
w Word character
W Non-word character
d Digit
D Non-digit
s White-space
S Non-white-space
# Assertions (Position Definers)
^ Start of the string
$ End of the string
# Quantifiers (Numbers Of Characters)
n* Zero or more of 'n'
n+ One or more of 'n'
n? A possible 'n'
n{2} Exactly 2 of 'n'
n{2, } At least 2 (or more) of 'n'
n{2,4} From 2 to 4 of 'n'
# Groupings
() Parenthesis to group expressions
(n/sitebuilder/regex/images/1/a) Either 'n' or 'a'
Character Classes
[1-6] A number between 1 and 6
[c-h] A lower case character between c and h
[D-M] An upper case character between D and M
[^a-z] Absence of lower case character between a and z
[_a-zA-Z] An underscore or any letter of the alphabet
Useful Perl Snippets
$mystring =~ s/^s*(.*?)s*$/$1/; Trim leading and trailing whitespace from $mystring
$mystring =~ tr/A-Z/a-z/; Convert $mystring to all lower case
$mystring =~ tr/a-z/A-Z/; Convert $mystring to all upper case
tr/a-zA-Z//s; Compress character runs, eg: bookkeeper -> bokeper
tr/a-zA-Z/ /cs; Convert non-alpha characters to a single space
#=============================================================
#Install a perl module from behind a firewalll using ppm
#=============================================================
Example with Compress-Bzip2
1. Download tar package from the nearest cpan mirror to C:\Temp\TEMP folder (could be other also ; )
2. Create Compress-Bzip2.ppd type of file , containging the following:
<?xml version="1.0" encoding="UTF-8"?>
<SOFTPKG NAME="Compress-Bzip2" VERSION="2,2,09">
<TITLE>Compress-Bzip2</TITLE>
<ABSTRACT>Blah</ABSTRACT>
<AUTHOR>Somebody</AUTHOR>
<IMPLEMENTATION>
<CODEBASE HREF="file:///C|/Temp/TEMP/Compress-Bzip2-2.09.tar.gz "></CODEBASE>
<INSTALL></INSTALL>
<UNINSTALL></UNINSTALL>
</IMPLEMENTATION>
</SOFTPKG>
Attention: . Name and codebase NOT C:\ BUT file:///C|
3. Open Dos prompt into the C:\Temp\TEMP (or whichever you prevered in the beginning)
4. Run the command :
ppm rep add downloadedTars C:\Temp\A_DOWNLOADS\downloaded _perlmodules
In order to add the folder to the list of your current ppm repositories
5. ppm install Compress-Bzip2
In order to install the module from the local folder
6. ppm rep delete myNewRepository
In order to remove the repository from the ppm
#Add company's servers to thte local ppm's repository
ppm rep add linox1 http://linox.company.com/mirror/cpan/
ppm rep add linox2 ftp://linux.company.com/mirror/cpan/
#sleep for less than second in Windows
use Win32 ;
Win32::sleep ($time_in_milliseconds);
#===================================================================
#throw proper errro messages while checking the file size
use File::stat qw(:FIELDS);
#create the obj wrapper for getting the file attributes
my $st =stat($file) or croak ( "No $stdout : $!"); #die will shut down the program !!!!
# get the file size
my $size = $st->size();
$stat_obj =stat($stdout) or die "No $stdout: $!";
#throw proper errro messages ^^^^
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($filename);
#===================================================================
#================================================================
#get the computername of a computer
$computername = $ENV{COMPUTERNAME} ;
#get the username of the a Windows computer
$userName = $ENV{USERNAME} ;
#get the name of the currrent executing script and ints pid
print $0 , $$ ;
#=========================================================== ========
#The textpad settings for jump to error for perl scripts File 1 Register 2
^.+at (.+) line ([0-9]+)[.,]?
exec *.pl *.pm *.cgi /s /b do perl -wc
#=========================================================== =====
#===================================================================
#======== Exctract only the name of a file witout the long path
#===================================================================
my ($keijo) = "c:\\homo\\urpo\\neste\\kekvo.exe";
@homo = split(/\\/,$keijo);
$jee = pop(@homo);
#The same thing
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
my $name = $2;
#===================================================================
#===================================================================
#PRINT TILL THE END OF A LABLE
#============================= ======================================
print <<END_HTML;
<html> <head>
<title>thanks!</title>
</head>
<body>
<p>thanks for uploading your photo!</p>
<p>your photo:</p>
<img src="/upload/$filename" border="0">
</body> </html>
END_HTML
# IMPORTANT !!!! IF THERE ARE SPACES IN THE PREVIOUS LINE == SYNTAX ERROR
#===================================================================
#check for file existance and other attributes
$readfile="myfile.cgi";
if ( (-e $readfile) && (-r $readfile) )
{
#proceed with your code
}
#=================================================================
#how to check my perl installed modules
#perldoc perllocal OR
#ppm install ExtUtils::Installed save and run this one
#source http://www.cpan.org/misc/cpan-faq.html#How_installed_modules
use ExtUtils::Installed;
my $instmod = ExtUtils::Installed->new();
foreach my $module ($instmod->modules())
{
my $version = $instmod->version($module) || "???";
print "$module -- $version\n";
}
#=================================================================
#How to check version of a module on Windows XP
perl -le "use CGI; print $CGI::VERSION"
#change the @INC withing a perl script
#Is Mail::Sendmail installed correctly? (Can you run a script with just "use Mail::Sendmail;"?) Check your @INC on your web server, and copy Sendmail.pm to a Mail subdirectory of one of the dirs. in your @INC. If you have not access to any of these, add the directory in a BEGIN block:
#BEGIN{ unshift @INC, '/absolute/path/to/my/personal/perl_lib'; } and copy Sendmail.pm to /absolute/path/to/my/personal/perl_lib/Mail/Sendmail.pm.
local( $/, *FILE ) ;
open (FILE , $sessionFile ) or
die "Cannot find $sessionFile !!! " ;
$fileString = <FILE>; #slurp the whole file into one string !!!
close FILE ;
use Encode ;
my $fileByteStream = decode('windows-1251', $fileString );
my $UTFSream = encode('UTF-8', $fileByteStream);
print "Read the whole directory for specific types of files " ;
$dh = DirHandle->new($path) or die "Can't open $path : $!\n";
@files = grep { /\.[ch]$/i } $dh->read();
# START FOREACH LINE
#SET HERE THE PATH TO THE FILE
my $inputfile='DD61_STG_OMDW.vwFactUserRegistration.txt' ;
#READ ALL ROWS OF A FILE TO ALIST
open (INPUT, "<$inputfile") || print "could not open the file!\n";
my @lineArray = <INPUT>;
close INPUT;
#how-to print key values in hashes
while( my ($key, $value) = each %$refHash)
{
print "\n key: $key, value: $value.\n";
}
#<<DOC_END;
The purpose of the script is ...
DOC_END
use Time::Local ;
# get a nicely formated time
my $timestamp = strftime("%d/%m/%y %H:%M:%S", localtime());
# how-to install modules from the cpan
perl -MCPAN -e 'install HTML::Template'
perl -MCPAN -e "install PAR::Packer"
# perl defined or operator
$IniFile //= "$ProductVersionDir/conf/$HostName/ini/run-$EnvironmentName.$HostName.ini";
##################################################
## START install perl modules by tar.gz
##################################################
# go to where you did download the modules tar
cd /path/to/module
# unzip
gzip -d *.tar.gz
# untar verbosely
tar xvf *.tar
# check the dirs created
for dir in `find \`pwd\` -maxdepth 1 -type d ` ; do echo cd $dir ; done ;
# go to the first dir which was created from the untar operation
for dir in `find \`pwd\` -maxdepth 1 -type d ` ; do cd $dir ; done ;
# create the make file
perl Makefile.PL
# run make
make
# optionally test
make test
# optionally create the make html
make html
# install
make install
##################################################
## STOP install perl modules by tar.gz
##################################################
# Purpose
# the pur
# VersionHistory:
# 1.1.0 --- 2012-07-19 19:33:45 --- ysg --- Added module installation instructions
# 1.0.0 --- 2012-06-18 09:01:29 --- ysg --- Initial version
# eof: file:PerlCheatSheet.pl
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!