Thursday, March 31, 2011

use encoding 'utf8" example

#!/usr/bin/perl 

use warnings;
use strict;

# Sort strings properly and match all letters
use locale;

# Read the source file as UTF-8 and set STDOUT and STDIN to UTF-8
use encoding 'utf8';


# We can write letters in the source code in UTF-8 thanks to "use encoding"
my @pole=qw(šiška marek ucho čaj žička);

# Sort knows our alphabet thanks to "use locale"
@pole=sort(@pole);           # sort array

foreach my $a (@pole) {
    # The pattern matching works fine thanks to "use locale"
    $a =~ s/\W//g;           # remove all non word characters
    print "$a \n";           # print the array field
}

# Write in UTF-8 into the file
open my $handle, ">:utf8", "file.txt" or die "Can't write to file.txt: $!";
# We can convert to upper case thanks to "use locale"
print $handle "\Uěščřžabcd\E\n";     # write upper cased string
close $handle;

use encoding 'utf8' example

#!/usr/bin/perl 

use warnings;
use strict;

# Sort strings properly and match all letters
use locale;

# Read the source file as UTF-8 and set STDOUT and STDIN to UTF-8
use encoding 'utf8';


# We can write letters in the source code in UTF-8 thanks to "use encoding"
my @pole=qw(šiška marek ucho čaj žička);

# Sort knows our alphabet thanks to "use locale"
@pole=sort(@pole);           # sort array

foreach my $a (@pole) {
    # The pattern matching works fine thanks to "use locale"
    $a =~ s/\W//g;           # remove all non word characters
    print "$a \n";           # print the array field
}

# Write in UTF-8 into the file
open my $handle, ">:utf8", "file.txt" or die "Can't write to file.txt: $!";
# We can convert to upper case thanks to "use locale"
print $handle "\Uěščřžabcd\E\n";     # write upper cased string
close $handle;

Deleting files & directories recursively

use strict;
use warnings;
cleanup("d:/tempo");
sub cleanup {
            my $dir = shift;
        local *DIR;
        opendir DIR, $dir or die "opendir $dir: $!";
            for (readdir DIR) {
                next if /^\.{1,2}$/;
                my $path = "$dir/$_";
            unlink $path if -f $path;
            cleanup($path) if -d $path;
            print "deleting $path\n";   
            }
        closedir DIR;
            print "Deleting $dir";
        rmdir $dir or print "error - $!";

The Pattern Component Order of Precedence

Table 10.7 - The Pattern Component Order of Precedence
Precedence Level Component
1 Parentheses
2 Quantifiers
3 Sequences and Anchors
4 Alternation

Friday, March 25, 2011

use 'eval' function in perl

use XML::LibXML;
my $parser = XML::LibXML->new();

my $tree;
eval {
    $tree = $parser->parse_file($file) # parses the file contents into the new libXML object.
};
warn("Error encountered: $@") if $@;

duplicate element count in array

my @array = qw(foo bar foo bar baz foo baz bar foo);
my %counts = ();
for (@array) {
$counts{$_}++;
}
foreach my $keys (keys %counts) {
print "$keys = $counts{$keys}\n";
}

date sort in perl

#!/usr/bin/perl 
use warnings;
use strict;

use Date::Parse;

print for sort { str2time($a) <=> str2time($b) } <DATA>;

__DATA__
March 9, 2010
April 2, 2008
January 23, 2009
April 1, 2008

reading .ini file without using a module

use strict;
use warnings;
use Data::Dumper;

open my $fh, '<', "my.ini" or die "$!\n";
my $ini = {};

{
local $/ = "";
while (<$fh>) {
next unless ( s/\[([^]]+)\]// );
my $header = $1;
$ini->{$header} = {};
while (m/(\w+)=(.*)/g) {
$ini->{$header}->{$1} = $2;
}
}
}
close $fh;

print Dumper $ini;


__DATA__
[Build]
BuildID=20110209115208
Milestone=2.0b11
SourceStamp=f9d66f4d17bf
SourceRepository=http://hg.mozilla.org/mozilla-central
; This file is in the UTF-8 encoding

[Strings]
Title=SeaMonkey Update
Info=SeaMonkey is installing your updates and will start in a few
moments

Tie::IxHash Example in perl

use Tie::IxHash;

tie my %unique => 'Tie::IxHash';

my @duplicates = (1, 1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 2, 3, 3,);

@unique{@duplicates} = ();

my @unique_elements = keys %unique;

print "@unique_elements\n";

How to I convert a string to a number

#How to I convert a string to a number

sub atoi {
 my $t;
 foreach my $d (split(//, shift())) {
     $t = $t * 10 + $d;
     print "$t\n";
  }
return $t;
}

$number = atoi("123");

print "string '123' number value is $number";

How to compare 2 arrays and differences store in 3 array

#!/usr/bin/perl
use strict;
use warnings;
use List::Compare;

my @new = qw/a b c d e/;
my @old = qw/a b   d e f/;

my $lc = List::Compare->new(\@new, \@old);

# an array with the elements that are in @new and not in @old : c
my @Lonly = $lc->get_Lonly;
print "\@Lonly: @Lonly\n";

# an array with the elements that are not in @new and in @old : f
my @Ronly = $lc->get_Ronly;
print "\@Ronly: @Ronly\n";

# an array with the elements that are in both @new and @old : a b d e
my @intersection = $lc->get_intersection;
print "\@intersection: @intersection\n";

__END__
** prints

@Lonly: c
@Ronly: f
@intersection: a b d e


OR


#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @new = qw/a b c d e/;
my @old = qw/a b   d e f/;
my %new = map{$_ => 1} @new;
my %old = map{$_ => 1} @old;

my (@new_not_old, @old_not_new, @new_and_old);
foreach my $key(@new) {
    if (exists $old{$key}) {
        push @new_and_old, $key;
    } else {
        push @new_not_old, $key;
    }
}
foreach my $key(@old) {
    if (!exists $new{$key}) {
        push @old_not_new, $key;
    }
}

print Dumper\@new_and_old;
print Dumper\@new_not_old;
print Dumper\@old_not_new;

Runtime vs Compile time

The difference between compile time and run time is an example of what pointy-headed theorists call the phase distinction. It is one of the hardest concepts to learn, especially for people without much background in programming languages. To approach this problem, I find it helpful to ask

    What invariants does the program satisfy?
    What can go wrong in this phase?
    If the phase succeeds, what are the postconditions (what do we know)?
    What are the inputs and outputs, if any?

Compile time

    The program need not satisfy any invariants. In fact, it needn't be a well-formed program at all. You could feed this HTML to the compiler and watch it barf...
    What can go wrong at compile time:
        Syntax errors
        Typechecking errors
        (Rarely) compiler crashes
    If the compiler succeeds, what do we know?
        The program was well formed---a meaningful program in whatever language.
        It's possible to start running the program. (The program might fail immediately, but at least we can try.)
    What are the inputs and outputs?
        Input was the program being compiled, plus any header files, interfaces, libraries, or other voodoo that it needed to import in order to get compiled.
        Output is hopefully assembly code or relocatable object code or even an executable program. Of if something goes wrong, output is a bunch of error messages.

Run time

    We know nothing about the program's invariants---they are whatever the programmer put in. Run-time invariants are rarely enforced by the compiler alone; it needs help from the programmer.

    What can go wrong are run-time errors:
        Division by zero
        Deferencing a null pointer
        Running out of memory

    Also there can be errors that are detected by the program itself:
        Trying to open a file that isn't there
        Trying find a web page and discovering that an alleged URL is not well formed
    If run-time succeeds, the program finishes (or keeps going) without crashing.
    Inputs and outputs are entirely up to the programmer. Files, windows on the screen, network packets, jobs sent to the printer, you name it. If the program launches missiles, that's an output, and it happens only at run time

Runtime vs Compile time

The difference between compile time and run time is an example of what pointy-headed theorists call the phase distinction. It is one of the hardest concepts to learn, especially for people without much background in programming languages. To approach this problem, I find it helpful to ask

    What invariants does the program satisfy?
    What can go wrong in this phase?
    If the phase succeeds, what are the postconditions (what do we know)?
    What are the inputs and outputs, if any?

Compile time

    The program need not satisfy any invariants. In fact, it needn't be a well-formed program at all. You could feed this HTML to the compiler and watch it barf...
    What can go wrong at compile time:
        Syntax errors
        Typechecking errors
        (Rarely) compiler crashes
    If the compiler succeeds, what do we know?
        The program was well formed---a meaningful program in whatever language.
        It's possible to start running the program. (The program might fail immediately, but at least we can try.)
    What are the inputs and outputs?
        Input was the program being compiled, plus any header files, interfaces, libraries, or other voodoo that it needed to import in order to get compiled.
        Output is hopefully assembly code or relocatable object code or even an executable program. Of if something goes wrong, output is a bunch of error messages.

Run time

    We know nothing about the program's invariants---they are whatever the programmer put in. Run-time invariants are rarely enforced by the compiler alone; it needs help from the programmer.

    What can go wrong are run-time errors:
        Division by zero
        Deferencing a null pointer
        Running out of memory

    Also there can be errors that are detected by the program itself:
        Trying to open a file that isn't there
        Trying find a web page and discovering that an alleged URL is not well formed
    If run-time succeeds, the program finishes (or keeps going) without crashing.
    Inputs and outputs are entirely up to the programmer. Files, windows on the screen, network packets, jobs sent to the printer, you name it. If the program launches missiles, that's an output, and it happens only at run time

Thursday, March 3, 2011

add module path in INC

BEGIN { push @INC, 'd:/purand/' }

# or

BEGIN { unshift @INC, 'e:/purand/temp' }

# or

use lib 'c:/cgywin/';
print "@INC";