Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Friday, April 9, 2010

Using Perl modules

  • Let the first line in the .pm file be package Red::Green::Blue;.
  • The .pm file should be named Blue.pm.
  • The .pm file must be stored at dir/Red/Green/Blue.pm.
  • In the perl program where you want to use this module, include use lib "dir";. This tells Perl to also look in dir when searching for modules. (this basically adds an entry to @INC).
  • In the same program, after the previous line, include use Red::Green::Blue;. This tells Perl to find path/Red/Green/Blue.pm for all paths in @INC, including dir.
When this will not work -
  • If you get the error Can't locate loadable object for module qwe, then the module is a non-pure Perl module and uses external code (e.g. C code), which should come in a .so file.
  • The easy way is to do sudo cpan Red::Green::Blue and install the module.
  • If you do this, sure to remove the file dir/Red/Green/Blue.pm so that Perl does not use it instead of the installed module.

    Thursday, February 4, 2010

    Perl tips - Unicode

    First, use Encode;

    Reading/Writing
    • $string = Encode::decode('UTF-8',$text); (assuming the input file (or STDIN) is encoded in UTF-8).
    • You can now handle $string as you would normal strings (e.g. split(//) will split it at character boundaries)
    • Do $text = Encode::encode('UTF-8', $string); before writing it out to file (assuming you want the output file (or STDOUT) in that encoding).
    Regex
    • \p{L} - full glyph (e.g. the letter 'A')
    • \p{M} - partial glyph (e.g. the accent ` on the letter 'A', giving 'À')
    • \p{N} - digit
    • \p{P} - punctuation
    • \p{kannada} - any Kannada character
    • \P{} - invert the condition
    E.g. to match a line if it contains no numerals and no punctuation do
    $line ~= m/\p{N}|\p{P}/

    Wednesday, February 3, 2010

    Perl tips - array of records, command line params, STDIN/STDOUT handles

    set of records or 2D array
    • To store a set of records, use  my @data; $data[i]->[j] = $someval; (ith record, jth field). This basically works similar to a 2-D array except that each row can have different size.
    • To sort the records based on column j, do @sorteddata = sort {$a->[j] <=> $b->[j]} @data;.
    Command line parameters
    • The format for specifying parameters is cmd OPT1=sth OPT2=sth ...
    • Use while(@ARGV){ if(m{OPT1=}){ use $'; }elsif(m{OPT2}){...}}.
    Others
    • Use $opstream = *STDOUT; (note the '*') to pass (both read and write) file handles around and use them via variables (e.g. print $opstream "qwe";).
    • To get floor of a number, do use POSIX; $val = POSIX::floor($num);.

    Perl tips - creating your own utility library using classes

    - Creating your own library of utility functions
    • Create a file MyUtils.pm
    • For each new class in it, start with package MyUtils::MyClass.
    • Create static class attributes using our $attr.
    • Create methods using sub meth1{ my @args = @_; ... }. If this will be used as an object method (not static), remember to treat first entry of @_ as reference to object.
    • Object is nothing but a normal data type (like hash or list) and is allocated in the new method (that you must define) and returned.
    • The new method should be like sub new {my ($class, $otherargs) = @_; my ;...; my $ref_to_somedata; bless , $class; return $ref_to_somedata;}.
    • Start with package MyUtils::MyClass::MySubClass to create a subclass.
    Accessing your library MyUtils.pm
    • First include use lib "path/to/dir/containing/MyUtils.pm"; use MyUtils; in the beginning of the .pl file.
    • Use MyUtils::MyClass::meth1 to access static methods.
    • Use $MyUtils::MyClass::attr to access static attribute $attr of the class (note the $).
    • Use MyUtils::MyClass->new(...) to create an object.
    • Use MyUtils::MyClass::MySubClass wherever you are using MyUtils::MyClass for subclasses.
    Other useful stuff
    • Use $ENV{env_variable} to access the value of environment variable $env_variable in Perl code, (useful for specifying "path/to/dir/" of .pm file).