# 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;
#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;
}
#OPEN FILE AND START TO WRITE TO IT FROM A LIST
open(FILEHANDLE,">$outputfile") || die "cannot open output file";
$num=0;
foreach $line (@myfile)
{
$num= $num + 1;
@line_tokens= split('=',$line);
foreach $token(@line_tokens)
{
if ($token =~/(4FID[[:digit:]]{5})|(1USD[[:digit:]]{5})|(ID[[:digit:]]{7})|(2SGD[[:digit:]]{5})|(2KRD[[:digit:]]{5})|(1BRD[[:digit:]]{5})|(4DED[[:digit:]]{5})|(4HUD[[:digit:]]{5})/ )
{
print (FILEHANDLE "$token");
}
else {;}
}
}
# READ ALL ROWS TO LIST
open (FILE,$file);
@rows = ;
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 some query ";
$sql .= "inner join plah ";
$sql .= "where something 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 myNewRepository C:\Temp\TEMP
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
(Section contributed by Y. Georgiev)
#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);
#===================================================================
#================================================================
#Add some servers to thte local ppm's repository
ppm rep add name1 http://server.nam.com
ppm rep add name2 ftp://server.name.com
#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";
}
#=================================================================
Getting the Current TabItem when the Tab is not selected in WPF
-
[image: Banner]
This is a quick reminder to self on how to retrieve a TabItem from a WPF
TabControl *when the tab is not currently selected* because I ru...
1 week ago
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 !!!!