___________________________
Perl: “Dalam Contoh Praktis 2”
( Manipulasi tanggal jam , Exec command , File dan directory, un/pack. ) Oleh : Faiq Azmi [
[email protected]]
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
1
1. Manipulasi Date. Seperti halnya bahasa pemrograman yang lainnya, date / tanggal , jam dan yang berhubungan dengan hal tersebut sangatlah berguna, terutama didalam penamaan sebuah file ataupun pencarian sebuah data didalam sebuah file atau CDR. a. Contoh script tanpa menggunakan perl modul -----------------------------------------------------------------------------------------------------------------------
Script name : timestamp.pl $datetime = localtime; $timestamp= time; print "Date-Time : $datetime\n"; print "Time-Stamp: $timestamp\n"; Jika dirun akan menghasilkan output :
-----------------------------------------------------------------------------------------------------------------------
Script name : tanggal1.pl @mmm = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); @ddd = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); ($sec,$men,$hour,$day,$mm,$yy,$dy) = localtime(time); $sec = '0'.$sec if length($sec) == 1; $men = '0'.$men if length($men) == 1; $hh = substr(100+$hour,-2); $dd = substr(100+$day,-2); $mmm = $mmm[$mm]; $mm = substr(101+$mm,-2); $ddd = $ddd[$dy]; $yyyy = 1900+$yy; $yy = substr($yyyy,-2); print "Date-Today : $dd-$mmm-$yyyy $hh:$men:$sec\n";
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
2
Jika dirun akan menghasilkan output:
-----------------------------------------------------------------------------------------------------------------------
Script name : tanggal2.pl @mmm = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); @ddd = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); $dc = -86400; # 1 day =86400 second ($sec,$men,$hour,$day,$mm,$yy,$dy) = localtime(time+$dc); $sec = '0'.$sec if length($sec) == 1; $men = '0'.$men if length($men) == 1; $hh = substr(100+$hour,-2); $dd = substr(100+$day,-2); $mmm = $mmm[$mm]; $mm = substr(101+$mm,-2); $ddd = $ddd[$dy]; $yyyy = 1900+$yy; $yy = substr($yyyy,-2); print "Date-Yesterday : $dd-$mmm-$yyyy $hh:$men:$sec\n"; Jika dirun akan menghasilkan output:
-----------------------------------------------------------------------------------------------------------------------
b. Contoh script menggunakan perl modul Didalam web site http://search.cpan.org terdapat berbagai macam modul perl yang bisa dipakai untuk memanipulasi date. Beberapa contoh diantara adalah : 1. Date::Simple 2. Date::Calc
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
3
--------------------------------------------------------------------------------------------------Contoh menggunakan Date::Simple module Nama script simple1.pl --------------------------------------------------------------------------------------------------use Date::Simple (':all'); # uses ISO 8601 format (YYYY-MM-DD) $today = today(); $today_d8 = $today->as_d8; $today_f = $today->format("%d-%b-%Y"); $tomorrow = $today + 1; $yesterday= $today - 1; $diff = date('2001-08-27') - date('1977-10-05'); $date $year $month $day
= Date::Simple->new('1972-01-17'); = $date->year; = $date->month; = $date->day;
$hari
= (('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$today->day_of_week]);
print "Today = $today\n"; print "Today D8 = $today_d8\n"; print "Today Format = $today_f\n"; print "Tomorrow = $tomorrow\n"; print "Yesterday = $yesterday\n"; print "Diff 2001-08-27 & 1977-10-05 = $diff days\n"; print "From 1972-01-17 = year -> $year\n"; print "From 1972-01-17 = month -> $month\n"; print "From 1972-01-17 = day -> $day\n"; print "Today is = $hari\n";
---------------------------------------------------------------------------------------------------
Jika dijalankan akan menghasilkan output :
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
4
Contoh menggunakan Date::Calc module Script name simple2.pl --------------------------------------------------------------------------------------------------------use Date::Calc (':all'); ($year,$month,$day, $hour,$min,$sec) = Today_and_Now([$gmt]); sub first_in_week_delta{ my $weeks = shift; $weeks = -1 * $weeks; my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks); ($y,$m,$d) = first_in_week($y,$m,$d); return ($y, $m, $d); } sub first_in_week{ my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); ($y, $m, $d) = Monday_of_Week(Week_of_Year($y, $m, $d)); return ($y, $m, $d); }
($y, $m, $d) = first_in_week_delta(0); #0 this weeks, -1 last weeks print "Today_and_Now = $day-$month-$year $hour:$min:$sec\n"; print "First date on this week : ".sprintf("%02d-%02d-%04d", $d, $m, $y)."\n";
--------------------------------------------------------------------------------------------------------Jika dijalankan akan menghasilkan output :
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
5
2. Command line windows dan *nix didalam perl. Command line yang bisa dilakukan didalam OS baik windows dan linux/unix juga bisa dilakukan didalam perl, command tersebut bisa diakomodir oleh command didalam perl itu sendiri maupun bisa juga perl menjalankan command asli dari OS tersebut. a. Command copy link("$file-dir1", "$file-dir2"); atau bisa juga rename("$file-dir1", "$file-dir2"); atau bisa juga $com = `/bin/cp $file-dir1 $file-dir2`; $com = `copy $file-dir1 $file-dir2`; b. Command delete unlink("$file-dir1"); atau bisa juga $com = `/bin/rm –fr $file-dir1`; $com = `delete $file-dir1`;
c. Command membuka directoy dan membaca file didalamnya. $path = 'C:/tools/mingw';
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
6
opendir(dir, $path); while( ($fname = readdir(dir))){ next if $fname =~ /.exe$/gi; print "$fname\n"; } closedir(dir); Jika dijalankan akan menghasilkan output:
Bisa juga dengan command sebagai berikut di linux/unix: foreach $file ( `ls –ltr /usr/local`){ $file =~ s/(\r|\n)+//gi; print "$file\n"; } ---------------------------------------------------------------------------------------------------3. Convert file format. File format convert bisa dilakukan didalam perl baik itu binary, hexa , asci dan lainnya . Berikut contoh file convert dari binary ke ascii dengan extract record per 187 dari sebuah CDR binary.
Contoh script binary to ascci dengan extract perl 187 record size.
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
7
# sample format utk extract perl recordsize=187 $template = "a1 a1 a1 a1 ". "a1 a1 a1 a1 ". "a1 ". "a1 ". "a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 a1 a1 ". "a1 a1 a1 a1 ". "a1 ". "a1 ". "a1 ". "a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 a1 ". "a1 ". "a1 ". "a1 ". "a1 ". "a1 ". "a1 ". "a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 ". "a1 a1 a1 a1 ". "a1 a1 a1 a1 a1 a1 a1 a1 ". "a1 a1 a1 ". "a1 a1 a1 ". "a1 a1 a1 ". "a1 a1 a1 "; $recordsize = length(pack($template,( ))); #print $recordsize; open(fin,"2008010100641860.DAT"); while(read(fin,$record,$recordsize)){
@data = unpack($template,$record);
for ($i=0; $i < @data; $i++){ $data[$i] = sprintf('%02X', ord($data[$i])); } $sn = hex("$data[3]$data[2]$data[1]$data[0]"); $snmod = hex("$data[7]$data[6]$data[5]$data[4]"); $modno = hex("$data[8]"); #$cdrtype = unpack("B*", pack("H*", $data[9])); $cdrtype = hex("$data[9]");
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
8
$chgstart = hex("$data[11]$data[10]").sprintf("%.2d",hex("$data[12]")).sprintf("%.2d",hex("$data[13]")).sprintf("%.2d",hex("$data[14 ]")).sprintf("%.2d",hex("$data[15]")).sprintf("%.2d",hex("$data[16]")); $atype_plan = unpack("B*", pack("H*", $data[17])); $an_type = substr($atype_plan,1,3); $an_plan = substr($atype_plan,4); $anumber = "$data[19]$data[20]$data[21]$data[22]$data[23]$data[24]$data[25]$data[26]$data[27]$data[28]$data[29]$data[30]"; $anumber =~ s/\F//gi; $btype_plan = unpack("B*", pack("H*", $data[31])); $bn_type = substr($btype_plan,1,3); $bn_plan = substr($btype_plan,4); $bnumber = "$data[33]$data[34]$data[35]$data[36]$data[37]$data[38]$data[39]$data[40]$data[41]$data[42]$data[43]$data[44]"; $bnumber =~ s/\F//gi; $ctype_plan = unpack("B*", pack("H*", $data[45])); $cn_type = substr($ctype_plan,1,3); $cn_plan = substr($ctype_plan,4); $cnumber = "$data[47]$data[48]$data[49]$data[50]$data[51]$data[52]$data[53]$data[54]$data[55]$data[56]$data[57]$data[58]"; $cnumber =~ s/\F//gi; $call_tldn = ""; print "$sn,$snmod,$modno,$cdrtype,$chgstart,$an_type,$an_plan,$anumber,$bn_type,$bn_plan,$bnumber,$cn_type,$cn_plan,$ cnumber,\n";
} close(fin);
-----------------------------------------------------------------------------------------------Math bin2hex my %hex = ( 0000 => '0', 0001 => '1', 0010 => '2', 0011 => '3', 0100 => '4', 0101 => '5', 0110 => '6', 0111 => '7', 1000 => '8', 1001 => '9', 1010 => 'A', 1011 => 'B', 1100 => 'C', 1101 => 'D', 1110 => 'E', 1111 => 'F', ); my $x = '110111011111101111101000110000'; my $hexnumber = ''; while($x=~/(....)/g){ $hexnumber .= $hex{$1}; } print $hexnumber . "\n";
---------------------------------------------------------------------------------------------Hex2bin script
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
9
%h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011", 4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111", 8 => "1000", 9 => "1001", a => "1010", b => "1011", c => "1100", d => "1101", e => "1110", f => "1111", ); $hex = "414243"; ($binary = $hex) =~ s/(.)/$h2b{lc $1}/g; print $binary. "\n";
---------------------------------------------------------------------------------------------Untuk lebih detailnya kita bisa menggunakan modul perl atau bisa Juga membaca manual pack dan unpack fungsi didalam perl. 4. Mengambil data dari Horisontal CDR. Source_file data.txt ----------------------------------------------------------------------------------------------Mon 01-Mar-2008 16:00:00 Dn = 2199205006 Balance = 4000 Expired = 21-Dec-2008 Type = 1 Mon 01-Mar-2008 16:01:00 Dn = 2199205007 Balance = 40 Expired = 20-Dec-2008 Type = 1 Mon 01-Mar-2008 16:01:50 Dn = 2199205001 Balance = 45 Expired = 29-Dec-2008 Type = 1 -----------------------------------------------------------------------------------------------
Script data.pl @parameter = ( 'log-date', 'dn', 'balance', 'expired', 'type' );
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
10
open(fin,"data.txt"); while(
){ $_ =~ s/(\r|\n)+//gi; next if $_ eq ''; if ( /^\w{3} \d{2}-$\w{3}-\d{4} \d{2}:\d{2}:\d{2}/ ){ $idx = 1; $c++; } if ( $idx == 1 ){ $_ =~ s/\s+//gi; ($par,$val) = split('='); if ( $par =~ /^\w{3} \d{2}-$\w{3}-\d{4} \d{2}:\d{2}:\d{2}/ ){ $val = $par; $par = 'log-date'; } $par =~ s/(^\s|\s$|\r|\n)+//; $val =~ s/(^\s|\s$|\r|\n)+//; $par = "\L$par"; $dat{$c,$par} = $val; } if ( /Type/ ){ $idx = 0; $it = -1; $ti++; for $prm (@parameter){ $it++; $doto = sprintf ("%s%s", $it ? "|" : "", $dat{$c,$prm}); $dot{$ti} .= $doto; } print "$dot{$ti}\n"; $dot{$ti} = undef; }
} close(fin);
-----------------------------------------------------------------------------------------------
Daftar Pustaka 1. www.google.com 2. Pengalaman Penulis.
Untuk umum, hasil kompilasi beberapa sumber. Ver. 12.2008
11