Tag Archive for perl

Howto list all the file extension types in an SVN log dump

svn log -v > myrepo.log

perl -ne 'print qq{$1
} if m{^s+.*(.[^.s]w+)$}' myrepo.log | sort | uniq -c | sort -r

source

Closure

#!/usr/bin/perl -w
#
#
#
use strict;

my $x = 12;
{ my $x; sub inc { $x++; print "$x
" } sub dec { $x-- ; print "$x
"} }

inc();
inc();
inc();
inc();
dec();
inc();

print "$x
";

source

Find newest file and its modification time

find directory -type f | perl -e 'my $l=0; my $n=""; while( <> ) { chomp; my $t= ${[stat($_)]}[9]; $t > $l and ($l= $t, $n= $_); } print "$n	".localtime($l), "
";'

source

using sendEmail perl script and Gmail SMTP server to send email

./sendEmail -f <a href="mailto:fromadd@site.com">fromadd@site.com</a> -t <a href="mailto:toadd@site.com">toadd@site.com</a> -u "Testing sendEmail script" -m "it works" -s smtp.gmail.com -xu gmailUsername -xp gmailPassword

source

Process PHP-style array (square brackets) from HTML form in Perl CGI

use CGI;  

$query = CGI::new(); 

my %fields_temp = $query->Vars;
my %fields      = ();

foreach my $key (keys %fields_temp) {
    if ( $key =~ /(.+)[]$/ ) { ## Handle PHP style array inputs 
        if(exists $fields{$1}){
            $fields{$1} = $fields{$1} . ",". $fields_temp{$key};
        }
        else{
            $fields{$1} = $fields_temp{$key};
        }        
    }
    else {
	$fields{$key} = $fields_temp{$key};
    }
}

foreach my $key (sort keys %fields) {
    unless ($key =~ /(.+)[]$/ ){
	push(@keys, $key);
	push(@values, $fields{$key});
    }
}

source

Perl Environment Variable Listing

print ($_."=",$ENV{$_}."<br />") foreach (keys %ENV);

source

comparing the checksums of two files with Perl and cksum

cksum file1 file2 | perl -ane '$x ||= $F[0]; warn if $x != $F[0]'

# This can be shortened slightly, to:

cksum file1 file2 | perl -ane 'warn if $F[0] != ($x ||= $F[0])'

# If the checksum value we need to match is already known, that can be
# shortened to:

cksum file1 file2 | perl -ane 'warn if "664253628" != $F[0]'

# on Mac, if you have copied a checksum value into the clipboard, use
# pbpaste to do the comparison

cksum file1 file2 | perl -ane 'warn if `pbpaste` != $F[0]'

source

Perl Keyword Search (One Liner)

#!/bin/sh

tail -n5000 somefile | perl -ne "print $_ if /$1/i && /$2/i && /$3/i && /$4/i && /$5/i;"

source

Fuzzy string matching with Perl

use String::Approx 'amatch';
use Test::More(no_plan);

sub fuzm {

$_ = shift;

return amatch("homer_simpson", [        # this array sets match options:
"i",    # match case-insensitively
"10%",  # tolerate up to 1 character in 10 being wrong
"S0",   # but no substituting one character for another
"D1",   # although, tolerate up to one deletion
"I2"    # and tolerate up to two insertions
]);

}


ok(fuzm("homer_simpson"),         "exact match for 'homer_simpson'");
ok(fuzm("homersimpson"),          "still matches without the underscore");
ok(fuzm("homers_impson"),         "putting the underscore in a different place, still matches");
ok(fuzm("ho_mer_simpson"),        "an extra underscore still matches");
ok(fuzm("ho_mer_simp_son"),       "2 extra underscores still matches");
ok((not fuzm "ho_mersimp_son"),   "2 underscores, both in the wrong places, doesn't match");
ok((not fuzm "ho_mer_sim_ps_on"), "3 extra underscores doesn't match");
ok((not fuzm "homer____simpson"), "3 extra underscores doesn't match");

source

(Perl) using WMI to get system information

#!/usr/bin/perl -w

# use binperl.exe wmi.pl to run.
# have fun!
# 2009/6/17 twitter.com/vinocui
#
# useful links:
# (WMI space definition) <a href="http://msdn.microsoft.com/en-us/library/aa394084" >http://msdn.microsoft.com/en-us/library/aa394084</a>(VS.85).aspx
# (OLE usage on CPAN)    <a href="http://cpan.uwinnipeg.ca/htdocs/Win32-OLE/Win32/OLE.html#Object_methods_and_properties" >http://cpan.uwinnipeg.ca/htdocs/Win32-OLE/Win32/OLE.html#Object_methods_and_properties</a>
#

use Win32::OLE;

#my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2") or die "failed to retrieve cimv2.";
# winmgmts means to access WMI service.

my $wmi = Win32::OLE->GetObject("WinMgmts://./root/cimv2") or die "Failed: GetObject
";
my $list, my $v;


$list = $wmi->InstancesOf("Win32_Processor") or die "Failed: InstancesOf
";

foreach $v (Win32::OLE::in $list){
print "CPU:
";
print "	", $v->{Name}, "
";
print "	", $v->{Caption}, "
";
}

$list = $wmi->InstancesOf("Win32_OperatingSystem") or die "Failed: InstancesOf
";

foreach $v (Win32::OLE::in $list){
print "OS:
";
print "	", $v->{Name}, "
";
}

0;

source