<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1104591510845793821</id><updated>2024-09-25T10:03:19.118+08:00</updated><category term="Perl Programming"/><category term="Date"/><category term="Convert"/><category term="File"/><category term="System Administrator"/><title type='text'>Amazing Perl</title><subtitle type='html'>Chin Siang&#39;s Perl Journal</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-299268936199126004</id><published>2015-02-15T16:12:00.000+08:00</published><updated>2015-02-15T16:29:33.990+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>A Simple Perl Logger with Log Timestamp in Milliseconds</title><content type='html'>A simple Per Logger with log timestamp in milliseconds.&lt;br /&gt;

&lt;pre&gt;#!/usr/bin/perl
use strict;
use Time::HiRes qw/gettimeofday/;
use POSIX qw/strftime/;

log_error(&quot;This is error&quot;);
log_info(&quot;This is information&quot;);

############################################################
# Logger - Error
############################################################
sub log_error {
    my $message = shift;
    log_it( $message, &quot;ERROR&quot; );
}

############################################################
# Logger - Info
############################################################
sub log_info {
    my $message = shift;
    log_it( $message, &quot;INFO&quot; );
}

############################################################
# Logger - Info
############################################################
sub log_it {
    my $message = shift;
    my $level   = shift;
    my ( $time, $ms ) = gettimeofday();
    my $logtimestamp = 
         strftime( &quot;%Y-%m-%d %H:%M:%S&quot;, localtime($time) );

    $ms = sprintf( &quot;%03d&quot;, $ms / 1000 );
    printf( &quot;%s.%03d %-5s %s\n&quot;, 
         $logtimestamp, $ms, $level, $message );
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Sample Output&lt;/b&gt;&lt;/p&gt;

&lt;pre&gt;
2015-02-15 16:27:34.822 ERROR This is error
2015-02-15 16:27:34.823 INFO  This is information
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/299268936199126004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2015/02/a-simple-perl-logger-with-log-timestamp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/299268936199126004'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/299268936199126004'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2015/02/a-simple-perl-logger-with-log-timestamp.html' title='A Simple Perl Logger with Log Timestamp in Milliseconds'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-4689954448318084259</id><published>2015-01-22T21:11:00.001+08:00</published><updated>2015-01-22T21:11:44.515+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>How to Find UTC Time Offset from your Local Time? </title><content type='html'>This is the sample script to find the offset in seconds of your local time from GMT.


&lt;pre&gt;#!/usr/bin/perl
use strict;

use POSIX;

my @local = localtime();
my @utc   = gmtime();

my $timezone_offset = mktime(@local) - mktime(@utc);

print &quot;Offset in second(s) from GMT = $timezone_offset\n&quot;;
print &quot;Offset in hour(s) from GMT = &quot; . ($timezone_offset / 3600) . &quot;\n&quot;;

&lt;/pre&gt;
&lt;b&gt;Output:&lt;/b&gt;
&lt;pre&gt;
Offset in second(s) from GMT = 28800
Offset in hour(s) from GMT = 8
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/4689954448318084259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2015/01/how-to-find-utc-time-offset-from-your.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/4689954448318084259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/4689954448318084259'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2015/01/how-to-find-utc-time-offset-from-your.html' title='How to Find UTC Time Offset from your Local Time? '/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-717598901379605163</id><published>2013-01-12T23:58:00.001+08:00</published><updated>2013-01-12T23:58:23.706+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Use Notepad++ as Perl IDE</title><content type='html'>If you are looking for a fast and small in size text editor as Perl IDE, Notepad++ probably one of the best choice. By default, Notepad++ will not run Perl script within the editor, you need install and configure &quot;NppExec&quot; plugin to allow this.
&lt;br /&gt;
Follows the below steps:
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Install &quot;NppExec&quot; from &quot;Plugin Manager&quot;.&lt;/li&gt;
&lt;li&gt;After installed the plugin, from &quot;Plugins&quot; menu, choose &quot;NppExec&quot; -&amp;gt; &quot;Execute...&quot;.&lt;/li&gt;
&lt;li&gt;Copy the following script and click &quot;Save...&quot; to save it as &quot;Run Perl&quot;. Click &quot;OK&quot; to close the dialog.&lt;br /&gt;
&lt;blockquote&gt;
&lt;pre&gt;NPP_SAVE
cd &quot;$(CURRENT_DIRECTORY)&quot;
C:\perl\bin\perl &quot;$(FILE_NAME)&quot; &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDEAwMtiTi9IyGxLZSHZHF-R55MS99pMeN6Kxy6GapcdZPCi_ZTrTAlSkRoD9xgUiPoyyMUZVMR8T7x4LKx1u02aLR4NsMB4TCfuBW-Ig_MRF5RanW2pRheyNfnhCbKSt33gmuWWHJlDzq/s1600/Execute.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDEAwMtiTi9IyGxLZSHZHF-R55MS99pMeN6Kxy6GapcdZPCi_ZTrTAlSkRoD9xgUiPoyyMUZVMR8T7x4LKx1u02aLR4NsMB4TCfuBW-Ig_MRF5RanW2pRheyNfnhCbKSt33gmuWWHJlDzq/s1600/Execute.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;From &quot;Plugins&quot; menu, choose &quot;NppExec&quot; -&amp;gt; &quot;Advanced Options...&quot;.&lt;/li&gt;
&lt;li&gt;Check &quot;Place to the Macros submenu&quot; which allows us to execute the script from &quot;Macros&quot; menu.&lt;/li&gt;
&lt;li&gt;Type &quot;Run Perl&quot; in &quot;Item name&quot; text box and associate it with &quot;Run Perl&quot; script, and click &quot;Add/Modify&quot; to create it. menu item. Click &quot;OK&quot; to save the settings.
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6sQK_2ItW8KZ9gdYJJpxNnahllqWXjuyg8_D0Ht-C5iTvDOEywK6aV0gGapNGKxtsktkaNBp7PlX6U0KRFCZXBJr6GIA851lA3Ig57QFA_gD3REJIVinI-w95A3IrIb6eEe3hPf1MudZk/s1600/Advanced+Options.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6sQK_2ItW8KZ9gdYJJpxNnahllqWXjuyg8_D0Ht-C5iTvDOEywK6aV0gGapNGKxtsktkaNBp7PlX6U0KRFCZXBJr6GIA851lA3Ig57QFA_gD3REJIVinI-w95A3IrIb6eEe3hPf1MudZk/s1600/Advanced+Options.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Now, assign a shortcut key e.g. &quot;Ctrl+Shift+B&quot; to execute the script. From &quot;Settings&quot; menu, choose &quot;Shortcut Mapper&quot; -&amp;gt; &quot;Plugin commands&quot; tab, select &quot;Run Perl&quot; and click &quot;Modify&quot; to assign &quot;Ctrl+Shift+B&quot; or any preferable key. Click &quot;OK&quot; to save it.&lt;/li&gt;
&lt;li&gt;Restart Notepad++.&lt;/li&gt;
&lt;li&gt;After restarted, you can execute an opened Perl script by pressing &quot;Ctrl+Shift+B&quot;.&amp;nbsp;&lt;/li&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRKM3bjdaGVfW8edm3U0hJrLSu1BGQFU9pfIC3at-UTmHzJpBWwoscsC8kRlCTh2cGKGo66jlIFJDnNR-2YJ_3yN9KjSQq2RZK6WDSjENoI2EIphFTaB8P2uAmnBogxu6ZpRg6LCDKIP2C/s1600/Npp.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRKM3bjdaGVfW8edm3U0hJrLSu1BGQFU9pfIC3at-UTmHzJpBWwoscsC8kRlCTh2cGKGo66jlIFJDnNR-2YJ_3yN9KjSQq2RZK6WDSjENoI2EIphFTaB8P2uAmnBogxu6ZpRg6LCDKIP2C/s1600/Npp.png&quot; /&gt;&lt;/a&gt;


&lt;/ol&gt;
</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/717598901379605163/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2013/01/use-notepad-as-perl-ide.html#comment-form' title='36 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/717598901379605163'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/717598901379605163'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2013/01/use-notepad-as-perl-ide.html' title='Use Notepad++ as Perl IDE'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDEAwMtiTi9IyGxLZSHZHF-R55MS99pMeN6Kxy6GapcdZPCi_ZTrTAlSkRoD9xgUiPoyyMUZVMR8T7x4LKx1u02aLR4NsMB4TCfuBW-Ig_MRF5RanW2pRheyNfnhCbKSt33gmuWWHJlDzq/s72-c/Execute.png" height="72" width="72"/><thr:total>36</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-6368032108354499726</id><published>2012-04-13T21:00:00.000+08:00</published><updated>2012-04-13T21:00:06.604+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Convert"/><category scheme="http://www.blogger.com/atom/ns#" term="File"/><title type='text'>How to Convert a file from Windows format to Linux format</title><content type='html'>This is a Perl implementation of &quot;dos2unix&quot; (a utility to convert Windows/DOS file format to Linux/UNIX file format.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;#!/usr/bin/perl
use strict;

use File::Copy;
use File::Temp;

my $FILENAME = &quot;MY_FILE.txt&quot;;

dos2ux($FILENAME);

sub dos2ux {
    my $infile = shift;
    my $is_converted = 0;

    # Make a temp file which will self delete automatically.
    my $tmp = File::Temp-&amp;gt;new(
        UNLINK =&amp;gt; 1);

    if (open(INFILE, $infile)) {
        if (open (OUTFILE, &quot;&amp;gt;&quot;, $tmp-&amp;gt;filename)) {
            binmode(OUTFILE);
            while (&lt;infile&gt;) {
                s/\r\n$/\n/;
                print OUTFILE $_;
            }
            close(OUTFILE);
            $is_converted = 1;
        }

        close(INFILE);
    } else {
        print &quot;Fail to open $infile\n&quot;;
    }

    if ($is_converted) {
        copy($tmp-&amp;gt;filename, $infile);
    }
}
&lt;/infile&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/6368032108354499726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2012/04/how-to-convert-file-from-windows-format.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/6368032108354499726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/6368032108354499726'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2012/04/how-to-convert-file-from-windows-format.html' title='How to Convert a file from Windows format to Linux format'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-1460309303852207587</id><published>2012-04-08T12:16:00.000+08:00</published><updated>2012-04-19T21:42:51.239+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="System Administrator"/><title type='text'>Remove Old Log Files with Keeping Minimum Number of Copies</title><content type='html'>It is very common that not all the application will purge their old log files. Hence, you need to purge the old log files (backup ZIP files) before the system runs out of space.&lt;br /&gt;
&lt;br /&gt;
Normally, we will purge the old files based on date. If the application does not run for long time, all the log files will be purged. Sometime, we would like to keep the minimum number of old log files to find out when it was run. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The script will be based on the following conditions:&lt;br /&gt;
&lt;br /&gt;
1. &lt;b&gt;Keep Minimum Number of Copies&lt;/b&gt; - If the application does not run, we will keep the log files even they are few months old. It will not proceed to check the keep days if failed to meet this condition.&lt;br /&gt;
&lt;br /&gt;
2. &lt;b&gt;Keep Days&lt;/b&gt; - It will remove all the logs that older than the specify days. To keep up to today log, specify zero day.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
#!/usr/bin/perl
# $Id: purge_files.pl,v 1.4 2012/04/19 13:38:55 chinsiang Exp chinsiang $
#
use strict;
use Getopt::Long;
use POSIX;
use File::Spec;
use File::Glob;

my $VERSION = &quot;1.0&quot;;
# 0 = keep today, 1 = keep today &amp; yesterday, etc
my $BACKUP_DAYS_TO_KEEP = 14;
# Specify number of copies to keep. If less the this number, 
# housekeeping will be aborted.
my $BACKUP_COPY_TO_KEEP = 10;

my $FILE_PATTERN = &#39;*.log&#39;;

my %option;
die unless GetOptions(
    &quot;file-pattern=s&quot; =&gt; \$option{&#39;file-pattern&#39;},
    &quot;backup-copy=i&quot;  =&gt; \$option{&#39;backup-copy&#39;},
    &quot;keep-days=i&quot;    =&gt; \$option{&#39;keep-days&#39;},
    &quot;test&quot;           =&gt; \$option{&#39;test&#39;},
    &quot;verbose&quot;        =&gt; \$option{&#39;verbose&#39;},
    &quot;help&quot;           =&gt; \$option{&#39;help&#39;},
);

if (defined( $option{&#39;help&#39;} )) {
    print_usage();
}

if (!defined($option{&#39;file-pattern&#39;})) {
    $option{&#39;file-pattern&#39;} = $FILE_PATTERN;
}


if (!defined($option{&#39;backup-copy&#39;})) {
    $option{&#39;backup-copy&#39;} = $BACKUP_COPY_TO_KEEP;
}

if (!defined($option{&#39;keep-days&#39;})) {
    $option{&#39;keep-days&#39;} = $BACKUP_DAYS_TO_KEEP;
}

if (!defined($option{&#39;test&#39;})) {
    $option{&#39;test&#39;} = 0;
}


purge_files($option{&#39;file-pattern&#39;}, 
            $option{&#39;keep-days&#39;}, 
            $option{&#39;backup-copy&#39;},
            $option{&#39;test&#39;},
            $option{&#39;verbose&#39;});

##############################################################################
# Purge the files
sub purge_files() {
    my $file_pattern   = shift;
    my $keep_days      = shift;
    my $backup_to_keep = shift;
    my $test           = shift;
    my $verbose        = shift;
    
    my $DAY_IN_SECONDS = 86400;
    
    my %files = get_sorted_files($file_pattern);
        
    my $file_count = scalar(keys(%files));                
    my $now = time();
    my $delete_time = $now - ($DAY_IN_SECONDS * $keep_days);
    
    # Adjust to localtime 12am    
    $delete_time -= (($delete_time % $DAY_IN_SECONDS ) + tzoffset()); 
    
    if ($verbose) {   
        print &quot;Delete file older than &quot; 
              . strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, localtime($delete_time)) . &quot;\n&quot;;
    }
            
    
    if ($verbose) {
        print &quot;Found files\n&quot;;
        my $index = 0;
        foreach my $file (sort { $files{$a} cmp $files{$b} } keys %files) {
            $index++;
            printf(&quot;%2d. %s %s\n&quot;, 
                    $index,
                    $file,
                    strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, localtime( $files{$file} ))
                    );
        }
    }
            
    foreach my $file (sort { $files{$a} cmp $files{$b} } keys %files) {                
        my $file_modified = $files{$file};


        if ($file_count &gt; $backup_to_keep) {              
            if ($file_modified &lt; $delete_time) {
                $file_count--;                

                if ($test) {
                    print &quot;Test: Delete $file, &quot;
                          . &quot;Modified: &quot; 
                          . strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, localtime($file_modified)) 
                          . &quot;\n&quot;;
                }
                else {
                    if (unlink($file) &gt; 0) {
                        print &quot;Deleted: $file, &quot;
                              . &quot;Modified: &quot; 
                              . strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, localtime($file_modified)) 
                              . &quot;\n&quot;;
                    }
                }

            }
        }     
        else {
            # Exit the loop
            last;
        }            
    }
}

##############################################################################
# Get sorted files into a hash table.
sub get_sorted_files {
    my $path  = shift;
    my $regex = shift;

    my @files = glob($path);
    my %hash = ();

    foreach my $file (@files) {
        $hash{$file} = (stat($file))[9];
    }

    return %hash;
}

##############################################################################
# Find the timezone in seconds.
sub tzoffset {
    my $t = time();
    my $utc = mktime(gmtime($t));
    my $local = mktime(localtime($t));

    return ($local - $utc);
}

##############################################################################
# Print usage
sub print_usage {
    my $usage =&amp;lt;&amp;lt;EOF
DESCRIPTION:
    Purge old records    

OPTIONS:
    --[f]ile-pattern File pattern in regex to purge. (Default: $FILE_PATTERN)
    --[b]ackup-copy  Minimum backup copy to keep. (Default: $BACKUP_COPY_TO_KEEP copies)
    --[k]eep-days    Number of days to keep. 
                     0 = Today, 1 = Today &amp; Yesterday, etc.
                     (Default: $BACKUP_DAYS_TO_KEEP days)
    --[t]est         Test run without purge the files.
    --[v]erbose      Increase verbosity.
    --[h]elp         Show this help text.
   

EXAMPLE:
    \$ $0 --keep-days 10 --backup-copy 1 --file-pattern &quot;~/tmp/*log&quot; --v
EOF
    die($usage);
}

__END__


&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/1460309303852207587/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2012/04/purge-old-log-files-with-minimum-number.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/1460309303852207587'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/1460309303852207587'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2012/04/purge-old-log-files-with-minimum-number.html' title='Remove Old Log Files with Keeping Minimum Number of Copies'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-4293471892533994285</id><published>2011-11-16T22:18:00.005+08:00</published><updated>2011-11-17T21:41:17.473+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Perl Quick References</title><content type='html'>Posting my personal Perl quick reference.

&lt;iframe frameborder=&quot;0&quot; height=&quot;560px&quot; src=&quot;https://docs.google.com/gview?a=v&amp;pid=explorer&amp;chrome=false&amp;api=true&amp;embedded=true&amp;srcid=0B9tmWJ012gA0YTZkNmM5MDQtOTNjNy00MzY4LThjMjQtMjQyMDkzZGU4ZWIx&amp;hl=en&quot; width=&quot;100%&quot;&gt;&lt;/iframe&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/4293471892533994285/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2011/11/perl-quick-references.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/4293471892533994285'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/4293471892533994285'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2011/11/perl-quick-references.html' title='Perl Quick References'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-3952926918121090360</id><published>2011-10-31T23:16:00.003+08:00</published><updated>2012-04-08T12:49:26.002+08:00</updated><title type='text'>Perl Quick Start Template</title><content type='html'>When starting a new script, you can use this template as a base which consists of:
&lt;ol&gt;
&lt;li&gt;RCS, CVS or SVN &quot;Id&quot; keyword to keep the version information.&lt;/li&gt;
&lt;li&gt;Support script arguments.&lt;/li&gt;
&lt;li&gt;Show usage function.&lt;/li&gt;
&lt;li&gt;A trim function.&lt;/li&gt;
&lt;/ol&gt;


&lt;pre&gt;&lt;code&gt;
#!/usr/bin/perl -w
# $Id: $
#
use strict;
use Getopt::Long;

my $VERSION = &quot;1.0&quot;;

my %option;
die unless GetOptions(
    &quot;help&quot;         =&amp;gt; \$option{&#39;help&#39;},
);

if (defined( $option{&#39;help&#39;} )) {
    print_usage();
}

...


# Perl trim function to remove whitespace from the start and end of the string
sub trim {
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

# Print usage
sub print_usage {
    my $usage = &amp;lt;&amp;lt;EOF;
DESCRIPTION:
    &lt;BLAH BLAH BLAH&gt;

OPTIONS:
    --[h]elp    Show this help text.

EXAMPLE:
    \$ $0
EOF
    die($usage);
}
&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/3952926918121090360/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2011/10/perl-quick-start-template.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/3952926918121090360'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/3952926918121090360'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2011/10/perl-quick-start-template.html' title='Perl Quick Start Template'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-3213886873238069206</id><published>2011-10-08T22:29:00.011+08:00</published><updated>2011-10-09T09:55:33.535+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>How to Change Linux Password via SSH</title><content type='html'>&lt;p&gt;Script to change Linux password remotely via SSH.&lt;/p&gt;


&lt;p&gt;
&lt;span style=&quot;font-weight:bold;&quot;&gt;Prerequisite&lt;/span&gt;&lt;br/&gt;

For ActivePerl users, you need to download the Net::SSH2 package.

Version 5.10.x
&lt;pre&gt;&lt;code&gt;C:\&gt;ppm install http://cpan.uwinnipeg.ca/PPMPackages/10xx/Net-SSH2.ppd&lt;/code&gt;&lt;/pre&gt;
Version 5.12.x
&lt;pre&gt;&lt;code&gt;C:\&gt;ppm install http://cpan.uwinnipeg.ca/PPMPackages/12xx/Net-SSH2.ppd&lt;/code&gt;&lt;/pre&gt;
&lt;/p&gt;

&lt;span style=&quot;font-weight:bold;&quot;&gt;Scripts&lt;/span&gt;
&lt;pre&gt;&lt;code&gt;
#!/usr/bin/perl 

use strict;
use Net::SSH2;

use Getopt::Long;

my %option = (
    &#39;server&#39;       =&gt; &#39;localhost&#39;,
    &#39;userid&#39;       =&gt; &#39;&#39;,
    &#39;password&#39;     =&gt; &#39;&#39;,
    &#39;newpassword&#39;  =&gt; &#39;&#39;,
);

if (!GetOptions(&quot;server|s=s&quot;       =&gt; \$option{&#39;server&#39;},
                &quot;userid|u=s&quot;       =&gt; \$option{&#39;userid&#39;}, 
                &quot;password|p=s&quot;     =&gt; \$option{&#39;password&#39;}, 
                &quot;newpassword|n=s&quot;  =&gt; \$option{&#39;newpassword&#39;})) {
    print_usage();  
}

my $ssh2 = Net::SSH2-&gt;new();

$ssh2-&gt;connect($option{&#39;server&#39;}) or die &quot;Unable to connect Host $option{&#39;server&#39;} \n&quot;;

$ssh2-&gt;auth_password($option{&#39;userid&#39;}, $option{&#39;password&#39;}) or die &quot;Unable to login\n&quot;;

my $chan = $ssh2-&gt;channel();
$chan-&gt;blocking(0);
$chan-&gt;ext_data(&#39;merge&#39;);

my $cmd = &quot;chage -l $option{&#39;userid&#39;}; &quot; 
        . &quot;echo -e \&quot;$option{&#39;password&#39;}\\n$option{&#39;newpassword&#39;}\\n$option{&#39;newpassword&#39;}\\n\&quot; | passwd 2&gt;&amp;1\n&quot;;

# print $cmd;

$chan-&gt;exec($cmd);

while (&lt;$chan&gt;) { 
    print $_;
} 

#########################################################################
# Subroutines
#########################################################################
sub print_usage {
    # ...
}
&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/3213886873238069206/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2011/10/how-to-change-linux-password-via-ssh.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/3213886873238069206'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/3213886873238069206'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2011/10/how-to-change-linux-password-via-ssh.html' title='How to Change Linux Password via SSH'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-2963900784460096355</id><published>2011-04-02T22:08:00.001+08:00</published><updated>2012-04-08T13:09:16.359+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Check for Holiday with Perl</title><content type='html'>&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
In some cases we would like to avoid to execute the script during public holiday e.g. Don’t send alert during holiday. This script contains 2 functions i.e. “load_holiday_file” and “is_holiday” where you can include in your script to check for holiday.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: lime;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;
use strict;

my @holidays = ();
my @now = localtime(time());

my $date = sprintf(&quot;%02d-%02d-%04d&quot;, $now[3], $now[4]+1, $now[5]+1900 );

load_holiday_file(\@holidays, &#39;holiday.txt&#39;);

if (is_holiday(\@holidays, $date)) {
    print &quot;Today is holiday&quot;;
}

&lt;span style=&quot;color: lime;&quot;&gt;# Check the date is holiday&lt;/span&gt;
&lt;span style=&quot;color: lime;&quot;&gt;# &quot;date_to_check&quot; is in &quot;DD-MM-YYYY&quot; format.&lt;/span&gt;
sub is_holiday {
    my $holidays = shift;
    my $date_to_check = shift;
       
    my $counter = 0;
    my $found = 0;
       
    while($counter &amp;lt;= $#{holidays} &amp;amp;&amp;amp; !$found) {
        my $holiday = $holidays-&amp;gt;[$counter];
               
        if ($date_to_check =~ /^$holiday/) {
            $found = 1;
        }
        else {           
            $counter++;
        }   
    }

    return $found;
}

&lt;span style=&quot;color: lime;&quot;&gt;# Load holiday file
# Holiday file
# 1. DD-MM-YYYY
# 2. DD-MM (Every year on this date)&lt;/span&gt;
sub load_holiday_file {
    my $holidays     = shift;
    my $holiday_file = shift;
       
    if (open(HOLIDAY_FILE, $holiday_file)) {
   
        my $line;
        while (defined($line = &amp;lt;HOLIDAY_FILE&amp;gt;)) {   
            chomp($line);
             
            next if ($line =~ /^\s*#/ );
            next if ($line =~ /^$/);
           
            # Trim the string
            $line =~ s/^\s+//;   
            $line =~ s/\s+$//;
                                               
            push @{holidays}, $line;
        }        
        close(HOLIDAY_FILE);
    }
} 



&lt;h3&gt;








Holiday File&lt;/h3&gt;
&lt;pre&gt;&lt;span style=&quot;color: lime;&quot;&gt;# Holiday file
# 1. DD-MM-YYYY
# 2. DD-MM (Every year on this date)&lt;/span&gt;
27-01-2011       
03-02-2011
03-02-2011
22-04-2011
02-05-2011
17-05-2011
09-08-2011
30-08-2011
26-10-2011
06-11-2011
26-12-2011
02-01-2012
&lt;/pre&gt;
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/2963900784460096355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2011/04/check-for-holiday-with-perl.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/2963900784460096355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/2963900784460096355'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2011/04/check-for-holiday-with-perl.html' title='Check for Holiday with Perl'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-9166674955128185000</id><published>2010-09-12T14:20:00.007+08:00</published><updated>2012-04-08T13:09:00.908+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Sort Date in &quot;DD MMM YYYY&quot; format</title><content type='html'>To sort date in &quot;DD MMM YYYY&quot; format (e.g. &quot;02 DEC 2010&quot;)

&lt;pre&gt;&lt;code&gt;
#/usr/bin/perl -w
use strict;

# Constants
my %MONTH_NUMBER = (
    &#39;JAN&#39; =&gt; 1,
    &#39;FEB&#39; =&gt; 2,
    &#39;MAR&#39; =&gt; 3,
    &#39;APR&#39; =&gt; 4,
    &#39;MAY&#39; =&gt; 5,
    &#39;JUN&#39; =&gt; 6,
    &#39;JUL&#39; =&gt; 7,
    &#39;AUG&#39; =&gt; 8,
    &#39;SEP&#39; =&gt; 9,
    &#39;OCT&#39; =&gt; 10,
    &#39;NOV&#39; =&gt; 11,
    &#39;DEC&#39; =&gt; 12,    
);

my @myarray = (
    &#39;08-DEC-10&#39;,
    &#39;30-NOV-10&#39;,
    &#39;26-NOV-10&#39;,
    &#39;25-NOV-10&#39;,
    &#39;25-NOV-10&#39;,
    &#39;24-NOV-10&#39;,
    &#39;11-NOV-10&#39;,
    &#39;10-NOV-10&#39;,
    &#39;10-NOV-10&#39;,
    &#39;08-NOV-10&#39;,
    &#39;03-NOV-10&#39;,
    &#39;02-NOV-10&#39;,    
    &#39;01-NOV-10&#39;,
);

my @sorted_array = sort sort_func @myarray;

print &quot;@sorted_array&quot;; 

####################
# Subroutine
####################
# Sort function
sub sort_func() {
    # To sort in desc, just swap $a and $b
    #return convert_datenumber($b) 
    #   cmp convert_datenumber($a);
  
    return convert_datenumber($a) 
       cmp convert_datenumber($b);
}

# Convert &quot;DD MMM YYYY&quot; into &quot;YYYYMMDD&quot;
sub convert_datenumber() {
    my $in_date = $_[0];
    
    my $out_date = 0;
            
    if (length($in_date)) {
        my $day        = substr($in_date, 0, 2);
        my $month      = $MONTH_NUMBER{ substr($in_date, 3, 3) };
        my $year       = substr($in_date, 7);
        
        $out_date = $year * 10000 
                  + $month * 100
                  + $day;                                    
    }
        
    return $out_date;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;b&gt;Output:&lt;/b&gt;

&lt;pre&gt;&lt;code&gt;
01-NOV-10 02-NOV-10 03-NOV-10 08-NOV-10 10-NOV-10 10-NOV-10 11-NOV-10 24-NOV-10 25-NOV-10 25-NOV-10 26-NOV-10 30-NOV-10 08-DEC-10
&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/9166674955128185000/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/09/sort-date-in-dd-mmm-yyyy-format.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/9166674955128185000'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/9166674955128185000'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/09/sort-date-in-dd-mmm-yyyy-format.html' title='Sort Date in &quot;DD MMM YYYY&quot; format'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-7722821518979918432</id><published>2010-06-07T23:43:00.007+08:00</published><updated>2011-04-02T22:13:09.704+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Arguments By Reference</title><content type='html'>Modify scalar, hash and list through a subroutine.   &lt;pre&gt;&lt;/code&gt;&lt;font color=&quot;#ff6633&quot;&gt;#!/usr/bin/perl -w
use strict;

my $name;
my %contacts;
my @children;

&amp;amp;my_function(\$name, \%contacts, \@children);


print &amp;quot;Name = $name\n&amp;quot;;

foreach my $key ( keys(%contacts) ) {
    print &amp;quot;$key = $contacts{$key}\n&amp;quot;;
}
 
foreach my $child (@children) {
    print &amp;quot;Child = $child\n&amp;quot;;
}

sub my_function() {
    my $ref_name      = $_[0];
    my $ref_contacts  = $_[1];
    my $ref_children  = $_[2];
    
    $$ref_name = &amp;quot;Harry&amp;quot;;
    $$ref_contacts{&#39;home&#39;} = &#39;1234567890&#39;;
    $ref_contacts-&amp;gt;{&#39;work&#39;} = &#39;9876543210&#39;;

    push(@$ref_children, &#39;James&#39;);
    push(@$ref_children, &#39;Mary&#39;);

    print &amp;quot;Name = $$ref_name\n&amp;quot;;
    
    foreach my $key ( keys(%$ref_contacts) ) {
        print &amp;quot;$key = $$ref_contacts{$key}\n&amp;quot;;
    }
    
    foreach my $child (@$ref_children) {
        print &amp;quot;Child = $child\n&amp;quot;;
    }
    print &amp;quot;\n&amp;quot;;       
}&lt;/code&gt;&lt;/font&gt;&lt;/pre&gt;  </content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/7722821518979918432/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/06/arguments-by-reference.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/7722821518979918432'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/7722821518979918432'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/06/arguments-by-reference.html' title='Arguments By Reference'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-8035384507286051080</id><published>2010-06-05T08:20:00.010+08:00</published><updated>2010-06-06T00:23:38.753+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Handle Command Line Arguments</title><content type='html'>&lt;p&gt;Command line argument is commonly use in passing in the configuration to a Perl script. Fortunately, Perl&#39;s core module come with a parsing library i.e. &lt;code&gt;GetOpt::Long&lt;/code&gt; which ease the parsing of arguments.&lt;/p&gt;
&lt;p&gt;Here is the sample code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/usr/bin/perl
use Getopt::Long;

my @files = ();
my $outputfile;

# Checking for missing arguments
if (@ARGV == 0 || 
  !GetOptions(&quot;input=s{1,}&quot; =&gt; \@files, 
              &quot;output|dest=s&quot; =&gt; \$outputfile) || 
  @files == 0 || 
  !defined($outputfile)) { 
  # Print Usage
  print &quot;Invalid arguments!\n&quot;; 
  exit 1;
}
print &quot;Total Input File: &quot; . @files . &quot;\n&quot;;
foreach my $file (@files) {
  print &quot;-&gt; $file\n&quot;;
}
print &quot;Output File: $outputfile\n&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
D:\&gt;GetOptions.pl -i a.txt b.txt -d my.txt
Total Input File: 2
-&gt; a.txt
-&gt; b.txt
Output File: my.txt
&lt;/code&gt;&lt;/pre&gt;

The checking for the missing arguments:
&lt;ul&gt;
&lt;li&gt;Check for zero argument: &lt;code&gt;if (@ARGV == 0 ||&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check options are in &quot;s&quot; or &quot;o&quot; or &quot;d&quot;: &lt;code&gt;!GetOptions(&quot;input=s{1,}&quot; =&gt; \@files, &quot;output|dest=s&quot; =&gt; \$outputfile)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check for no input files: &lt;code&gt;@files == 0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check for missing output file: &lt;code&gt;!defined($outputfile)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
To get &lt;strong&gt;multiple values&lt;/strong&gt; the option, you can use &lt;code&gt;&quot;input=&lt;strong&gt;s{1,}&lt;/strong&gt;&quot;&lt;/code&gt;, it will store the values into a list (&lt;code&gt;@files&lt;/code&gt;).

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; In Perl, a subroutine parameter start with back slash (&quot;\&quot;) e.g. &lt;code&gt;\@files&lt;/code&gt;, indicates pass by reference. It means the parameter can be changed through the subroutine.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/8035384507286051080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/06/handle-command-line-arguments.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/8035384507286051080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/8035384507286051080'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/06/handle-command-line-arguments.html' title='Handle Command Line Arguments'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-8427642099739043518</id><published>2010-05-31T21:50:00.004+08:00</published><updated>2010-06-08T20:34:33.067+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>How to Process Multiple Files from Wildcard Arguments</title><content type='html'>To process multiple files from wildcard arguments, use &lt;code&gt;glob()&lt;/code&gt; function.
&lt;pre&gt;&lt;code&gt;#!/usr/bin/perl
@files = glob(&quot;@ARGV&quot;);
foreach (@files) {
    print &quot;File: $_\n&quot;;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Please notice that the &lt;code&gt;&lt;strong&gt;@ARGV&lt;/strong&gt;&lt;/code&gt; is surrounded by double quotes. Without double quotes, it will return the count of arguments.&lt;/p&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;pre&gt;&lt;code&gt;D:\&gt;process_files.pl test*.pl myperl.pl exec_cmd.pl
File: TestBack.pl
File: TestCSV.pl
File: TestDate.pl
File: myperl.pl
File: exec_cmd.pl&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/8427642099739043518/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/05/how-to-process-multiple-files-from.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/8427642099739043518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/8427642099739043518'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/05/how-to-process-multiple-files-from.html' title='How to Process Multiple Files from Wildcard Arguments'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-441986593112469301</id><published>2010-05-29T07:37:00.000+08:00</published><updated>2010-05-29T07:57:19.280+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Execute an External Command</title><content type='html'>In Perl, to execute an external command and capture its output can be done in &lt;strong&gt;one&lt;/strong&gt; line.
&lt;pre&gt;&lt;code&gt;
#!/usr/bin/perl

# Use backticks (``) operator 
@output = `tasklist.exe`;

# Display the output
foreach $line (@output) {
    print &quot;$line&quot;;
}
&lt;/code&gt;&lt;/pre&gt;
Output:&lt;pre&gt;&lt;code&gt;
D:\&gt;exec_cmd.pl

Image Name        PID Session Name  Session# Mem Usage
================= === ============= ======== ==========
System              4 Services             0   32,236 K
smss.exe          332 Services             0      760 K
csrss.exe         436 Services             0    1,728 K
wininit.exe       476 Services             0    2,020 K
csrss.exe         488 Console              1    8,268 K
&lt;/code&gt;&lt;/pre&gt;

You can use Perl to analysis the output and send an email alert if any error detected.</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/441986593112469301/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/05/execute-external-command.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/441986593112469301'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/441986593112469301'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/05/execute-external-command.html' title='Execute an External Command'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-2524631275107875154</id><published>2010-05-28T20:50:00.000+08:00</published><updated>2011-01-15T17:04:10.151+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>How to Debug a Perl Script</title><content type='html'>&lt;i&gt;Programmers do not know how to debug is blind.&lt;/i&gt; Perl comes with a built-in debugger that allows you to do debugging without separately install a debugger.   &lt;div&gt;&lt;/div&gt; To debug a Perl script, simply use &amp;quot;-d&amp;quot; flag:   &lt;pre&gt;&lt;code&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGNND2FdNG1ia3J9sMGXMVNbeq5aMnzPAK2O967OSHiA_rj8y_GlBQazfVRayKznBPSHWPFL0a1yJjpsGQ3sx4fqYYrCgzA8sgEDJfIA9LQcXw0j-GtszYEk6UPsH-naOozMToEC9eS7J1/s1600-h/Debug_Perl%5B5%5D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px&quot; title=&quot;Debug_Perl&quot; border=&quot;0&quot; alt=&quot;Debug_Perl&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgohymFAfZy-WTeA3dUR4QaRbNn9Gi1IJ2BsvEZAZA8-PQpTx4NOk-TSnLWoz5Vel_eA0FmTX2C02g6DTnuVvd_UEBS6hhAoGpQbHE6g_vO1dJSx30B4mvUivzla8xROTO0U7yIHVcsDnHE/?imgmax=800&quot; width=&quot;550&quot; height=&quot;354&quot; /&gt;&lt;/a&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;Here are the common use commands: &lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;s - Single step. It will step into a subroutine. &lt;/li&gt;

  &lt;li&gt;n - Next, steps over subroutines. &lt;/li&gt;

  &lt;li&gt;r - Return from the subroutine. &lt;/li&gt;

  &lt;li&gt;c - Continue until a breakpoint if any. &lt;/li&gt;

  &lt;li&gt;b [ln|event|sub] - Set a breakpoint. &lt;/li&gt;

  &lt;li&gt;l [line|sub] - List source code. &lt;/li&gt;

  &lt;li&gt;p expr - Print expression. &lt;/li&gt;

  &lt;li&gt;q - Quit. &lt;/li&gt;
&lt;/ul&gt;
For details, visit &lt;a href=&quot;http://perldoc.perl.org/perldebug.html&quot;&gt;http://perldoc.perl.org/perldebug.html&lt;/a&gt;. Cheers! 

&lt;div&gt;&lt;/div&gt;

&lt;div&gt;&lt;/div&gt;  </content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/2524631275107875154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/05/how-to-debug-perl-script.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/2524631275107875154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/2524631275107875154'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/05/how-to-debug-perl-script.html' title='How to Debug a Perl Script'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgohymFAfZy-WTeA3dUR4QaRbNn9Gi1IJ2BsvEZAZA8-PQpTx4NOk-TSnLWoz5Vel_eA0FmTX2C02g6DTnuVvd_UEBS6hhAoGpQbHE6g_vO1dJSx30B4mvUivzla8xROTO0U7yIHVcsDnHE/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1104591510845793821.post-1767324546342103833</id><published>2010-05-28T10:28:00.000+08:00</published><updated>2010-05-29T08:07:13.820+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl Programming"/><title type='text'>Perl: Easy way to read a fixed length file</title><content type='html'>To parse a fixed length string, you can use &lt;code&gt;unpack()&lt;/code&gt; function:
&lt;pre&gt;&lt;code&gt;#Name(10), Age(2), Sex(6)
#Sample data:
# &quot;John 20MALE  &quot;
# &quot;Mary 22FEMALE&quot;
my $templateformat = &quot;A10A2A5&quot;; # Read unpack() documentations.
my @fields = unpack( $templateformat , &quot;John 20MALE&amp;nbsp; &quot;);&lt;/code&gt;&lt;/pre&gt;
This is a simple example to read a fixed length file:
&lt;pre&gt;&lt;code&gt;#!/usr/bin/perlopen(INFILE, $ARGV[0]);
my $templateformat = &quot;A10A2A5&quot;;
while (&amp;lt;INFILE&amp;gt;) {
  my @fields = unpack( $templateformat , $_);
  print &quot;$fields[0] is $fields[1] years old\n&quot;;
}
close(INFILE);&lt;/code&gt;&lt;/pre&gt;
To run it:
&lt;pre&gt;&lt;code&gt;C:\&amp;gt; readfixedlength.pl sampledata.txt&lt;/code&gt;&lt;/pre&gt;
Output:
&lt;pre&gt;&lt;code&gt;John is 20 years old
Mary is 22 years old&lt;/code&gt;&lt;/pre&gt;
You can store the file format in a configuration file. It makes the script much easier to read and maintain. The script keeps the configuration after the __DATA__ token.

NOTE: __DATA__ is a token that marks end of script. You can use DATA filehandle the text after it.
&lt;pre&gt;&lt;code&gt;#!/usr/bin/perl
my @header_names = ();
my @header_lengths = ();
&amp;amp;load_template(\@header_names, \@header_lengths);
my $templateformat;
foreach (@header_lengths) { 
  $templateformat .= &quot;A&quot; . $_;
}
open(INFILE, $ARGV[0]);
while (&amp;lt;INFILE&amp;gt;) {
  my @fields = unpack( $templateformat , $_);
  print &quot;$fields[0] is $fields[1] years old\n&quot;;
}
close(INFILE);
# Load from the template __DATA__ section
sub load_template() {
  # By-ref parameters
  my $ref_header_names = $_[0];
  my $ref_header_lengths = $_[1];
  # Load the file template from __DATA__  
  while (&amp;lt;DATA&amp;gt;) {
    chomp;
    # Skip comment or empty line.
    next if (/^#/ || /^$/);
    my @fields = split(&quot;,&quot;);
    push(@$ref_header_names, $fields[0]);
    push(@$ref_header_lengths, $fields[1]);
  }
}
__DATA__
FIELD_NAME,10
FIELD_AGE,2
FIELD_SEX,6&lt;/code&gt;&lt;/pre&gt;

It is very simple, right?</content><link rel='replies' type='application/atom+xml' href='http://amazingperl.blogspot.com/feeds/1767324546342103833/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://amazingperl.blogspot.com/2010/05/perl-easy-way-to-read-fixed-length-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/1767324546342103833'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1104591510845793821/posts/default/1767324546342103833'/><link rel='alternate' type='text/html' href='http://amazingperl.blogspot.com/2010/05/perl-easy-way-to-read-fixed-length-file.html' title='Perl: Easy way to read a fixed length file'/><author><name>Chin Siang</name><uri>http://www.blogger.com/profile/00801428130993536005</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHOYFY5Q4lXKiRO4bZ5ihb3CixYK9ZhCkf7ft5KnCceA_8nd-VEMisc4SQEW6fLO0Sm1CS63ITC7gu_DK3gHrwgUBIjUDAEg1552sFAD9lBWK_evVtOP5vhGdjPHty3A/s220/Photos.PNG'/></author><thr:total>0</thr:total></entry></feed>