Archive

Archive for the ‘Perl’ Category

Compiling Perl Modules on Windows

April 22, 2011 4 comments

Compiling Perl Modules for Windows systems includes:

  1.   Executing Makefile.PL
  2.   NMAKE.EXE Test
  3.   Generating .ppd Files

    Commands for compiling Perl Modules on Windows Systems:

    Ensure you have Visual Studio installed in-order to support the name.exe. For this run VCVARS32.BAT file from VS.

    C:\>path | grep -i vc98

    C:\>"C:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT"

    Setting environment for using Microsoft Visual C++ tools.

    C:\>

    Navigate to the folder where your modules are existing:

    C:\src\Systems-Windows-Log>perl Makefile.PL

    Checking if your kit is complete…

    Looks good

    Writing Makefile for Systems::Windows::Log

    C:\src\Systems-Windows-Log>

    Run Nmake.exe

    C:\src\Systems-Windows-Log>C:\Tools\bin\NMAKE.EXE

    Microsoft (R) Program Maintenance Utility Version 1.50

    Copyright (c) Microsoft Corp 1988-94. All rights reserved.

    cp Log.pm blib\lib\Systems\Windows\Log.pm

    C:\src\Systems-Windows-Log>C:\Tools\bin\NMAKE.EXE test

    Microsoft (R) Program Maintenance Utility Version 1.50

    Copyright (c) Microsoft Corp 1988-94. All rights reserved.

    C:\Perl\bin\perl.exe "-Iblib\lib" "-Iblib\arch" test.pl

    1..1

    ok 1

    C:\>

    Generate ppd files and have them copied to your ppm repositories:

    C:\src\Systems-Windows-Log>C:\Tools\bin\NMAKE.EXE ppd

    Microsoft (R) Program Maintenance Utility Version 1.50

    Copyright (c) Microsoft Corp 1988-94. All rights reserved.

    C:\src\Systems-Windows-Log>perl C:\perlmod\bin\generate-ppd-files Systems-Windows-Log.ppd

    Creating archive file Systems-Windows-Log-1.1.tar.gz …

    Copying Systems-Windows-Log-1.1.ppd …

    1 File(s) copied

    Copying Systems-Windows-Log-1.1.tar.gz …

    1 File(s) copied

    Done!

    C:\src\Systems-Windows-Log>

     

    Note: Verify that ARCHITECTURE NAME="MSWin32-x86-multi-thread-5.8" in the ppd file at PPM repository folder.

    Categories: Perl

    Perl References/Dereferences

    April 22, 2011 Leave a comment

    An enumerated or comma-separated list always returns the last element in a scalar context.

    An array reference is created by either:

    1. * the [ ] operator around a list
    2. * the \ operator in front of a list variable (@)

    A hash reference is created by either:

    1. * the { } operator around a list (of pairs)
    2. * the \ operator in front of a hash variable (%)

    Example Code:

    #############################################################

    references.pl

    #!/usr/bin/perl -w

    use strict;

    use Data::Dumper;

    my @a = qw( hello there you guys ); # a regular array

    my %grade = qw( A 4.0 B 3.1 C 2.0 D 1.0 ); # a regular hash

    my $aref = \@a;

    my $bref = [ 'X', 'Y', 'Z' ];

    print Dumper $aref, $bref;

    print "——————————— 1\n";

    my $graderef = \%grade;

    my $ageref = {

    John => 50,

    Joe => 34,

    Ellen => 15,

    Marty => 44,

    };

    print Dumper $graderef, $ageref;

    print "——————————— 2\n";

    # The @mixture array represents an object whose structure is complex

    my @mixture = ( $aref, [ 15..18 ], $graderef, $ageref );

    # individual components can be accessed in various ways

    print Dumper \@mixture;

    print "——————————— 3\n";

    print $mixture[0], "\n"; # this is the reference $aref

    print $mixture[0]->[2], "\n"; # explicit deference of $mixture[0] with "->"

    print $mixture[1][0], "\n"; # implicit deference of $mixture[1]

    print "——————————— 4\n";

    print $mixture[2]->{A}, "\n"; # explicit deference of $mixture[2] with "->"

    print $mixture[2]{B}, "\n"; # implicit deference of $mixture[2] with "->"

    print "——————————— 5\n";

    my @b = @{ $mixture[1] }; # cast reference to back to an array with @{ }

    my %h = %{ $mixture[2] }; # cast reference to back to a hash with %{ }

    print "$b[2] $h{C}\n"; # print to confirm that they are what you think

    #############################################################

    Categories: Perl

    Reading a File via Perl Script

    April 22, 2011 Leave a comment

    Reading with <> into an array: gets lines into array as is in file along with new line chars. each $line_$i in file = $arr[$i]

    Reading with <> into hash : sets $has{$key} = $value. where $key = line_$i and $value = line_$i+1 respectively.

    Reading with <> using while loop: gets each line along with "\n" chars

    Categories: Perl

    Perl System Command and Return Codes

    April 22, 2011 2 comments

    Many of you must have used Perl as part of day to day automation. Some teams use test harnesses made exclusively of Perl. This makes it easier to leverage various functionalitiesof text manipulation, file operations and process monitoring, etc which is needed for a functional test harness.

    More often, system command is used to run an executable and its return code ($?) is used to compare with expected return code. Normally, a successs means value of $? is zero and failure if otherwise. This is good if you plan to write tests from scratch and team decides to go with certain conventions.

    But, if you have no option but to run a set of tests which have been developed by other teams, and they might have decided to go with different return scheme, make sure you understand the following.

    According to Perl documentation <http://search.cpan.org/~nwclark/perl-5.8.6/pod/perlfunc.pod> (search for "system PROGRAM LIST"): The return value is the exit status of the program as returned by the wait call. To get the actual exit value shift right by eight.

    if ($? == -1) {
            print "failed to execute: $!";
    } elsif ($? & 127) {
           printf "child died with signal %d, %s coredump",  ($? & 127), ($? & 128) ? ‘with’ : ‘without’;
    } else {
            printf "child exited with value %d", $? >> 8;
    }

    Thus, if a test returns 10, then the value of $? after running system is 2560. You shift 8 bits to right and you get 1.

    But, what they don’t tell you is, if a test is returning 666, instead of returning 170496, you will get 154 (10011010) system command will return only lowermost 8 bits after you have already shifted 8 bits (for signals and core dumps) Thus, If test is expected to return 666                                        — 10 1001 1010 0000 0000
    After shifting lowermost 8 bits, you would expect system to return — 10 1001 1010 <- gone ->

    But, system will return only lowermost 8 bits again :)         — <-gone-> 1001 1010 <- gone ->
    so after shifting 8 bits you will get 154 (1001 1010 ) instead of 666 (10 1001 1010 )

     

    Reference: http://blogs.msdn.com/b/shamit/archive/2005/02/01/365217.aspx

    Categories: Perl

    Perl Module to parse and update Mozilla Firefox Configuration files

    February 14, 2011 4 comments

    I’ll soon publish a Perl module that I developed for parsing and updating Mozilla Firefox user settings configuration files

    1. user.js
    2. prefs.js
    3. hostperm.1
    4. bookmarks.html or places.sqlite
    5. profiles.ini
    6. mimeTypes.rdf

    Firefox configuration files location: %APPDATA%\Mozilla\Firefox\Profiles

    in Windows 7:   C:\Users\<UserProfile>\AppData\Roaming\Mozilla\Firefox\Profiles\

     

     

    Categories: Perl

    Perl and Shell Scritping Windows

    January 9, 2011 Leave a comment
    • Code to list all Windows environment variables in current user shell

    C:\>perl -e “foreach (sort keys %ENV) {print \”$_ = $ENV{$_}\n\”; } “; > \Temp\perl-env.txt

    C:\>

    • Single Command to combine all files in directory into single file.
      • C:\Temp\test>dir /w *.json | grep -i json
        tasklist-level-1.json   tasklist-level-2.json   tasklist-level-3.json
        tasklist-level-4.jsonC:\Temp\test>copy *.json combined-file.json
        tasklist-level-1.json
        tasklist-level-2.json
        tasklist-level-3.json
        tasklist-level-4.json
        1 file(s) copied.

        C:\Temp\test>dir /w *.json | grep -i json
        combined-file.json      tasklist-level-1.json   tasklist-level-2.json
        tasklist-level-3.json   tasklist-level-4.json

        C:\Temp\test>wc -l

        C:\Temp\test>wc -l combined-file.json
        2230 combined-file.json

        C:\Temp\test>

    • Single command line to find and replace string in all files of a folder.  Use Cygwin on Windows.
      • gunnalag@GMD /cygdrive/c/temp
        $ grep -r -i idle ./test
        ./test/tasklist-level-1.json:   “System idle Process”:{
        ./test/tasklist-level-2.json:   “System idle Process”:[
        ./test/tasklist-level-3.json:   “System idle Process”:[
        ./test/tasklist-level-4.json:   “System idle Process”:[
        gunnalag@GMD /cygdrive/c/temp
        $ perl -e “s/idle/ideal/ig;” -pi $(find test -type f)
        gunnalag@GMD /cygdrive/c/temp
        $ grep -r -i idle ./test
        ./test/tasklist-level-1.json.bak:   “System idle Process”:{
        ./test/tasklist-level-2.json.bak:   “System idle Process”:[
        ./test/tasklist-level-3.json.bak:   “System idle Process”:[
        ./test/tasklist-level-4.json.bak:   “System idle Process”:[
        gunnalag@GMD /cygdrive/c/temp
        $ grep -r -i ideal ./test
        ./test/tasklist-level-1.json:   “System ideal Process”:{
        ./test/tasklist-level-2.json:   “System ideal Process”:[
        ./test/tasklist-level-3.json:   “System ideal Process”:[
        ./test/tasklist-level-4.json:   “System ideal Process”:[
        gunnalag@GMD /cygdrive/c/temp
        $
      • Single command line to append a new line “with text” to all files in a directory
        • gunnalag@GMD /cygdrive/c/temp/test
          $ for FILE in *; do echo “Newly Added Line” >> $FILE; done

          gunnalag@GMD /cygdrive/c/temp/test
          $ tail -3 tasklist-level-1.json
          }
          “n”
          Newly Added Line

          gunnalag@GMD /cygdrive/c/temp/test
          $

      • h
    Categories: Perl

    Windows Perl Scripting Info

    January 9, 2011 Leave a comment

    Best and latest Perl books are avail on O’Reilly’s CD Bookshelf online

    C:\>perl -v

    This is perl, v5.8.6 built for MSWin32-x86-multi-thread <= This indicates it’s a 32bit distribution.
    (with 3 registered patches, see perl -V for more detail)

    Copyright 1987-2004, Larry Wall

    Binary build 811 provided by ActiveState Corp. http://www.ActiveState.com
    ActiveState is a division of Sophos.
    Built Dec 13 2004 09:52:01

    Perl may be copied only under the terms of either the Artistic License or the
    GNU General Public License, which may be found in the Perl 5 source kit.

    Complete documentation for Perl, including FAQ lists, should be found on
    this system using `man perl’ or `perldoc perl’.  If you have access to the
    Internet, point your browser at http://www.perl.org/, the Perl Home Page.

    C:\>perl -e “print \”This is a one line program out\n\”"; -e “print \”This is the second line\”";
    This is a one line program out
    This is the second line
    C:\>

    C:\>perl -V -e “print \”\n\nAbove is my detailed configuration\n\”";
    Summary of my perl5 (revision 5 version 8 subversion 6) configuration:
    Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname=”
    config_args=’undef’
    hint=recommended, useposix=true, d_sigaction=undef
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    Compiler:
    cc=’cl’, ccflags =’-nologo -Gf -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT  -DNO_HASH_SEED -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT
    optimize=’-MD -Zi -DNDEBUG -O1′,
    cppflags=’-DWIN32′
    ccversion=”, gccversion=”, gccosandvers=”
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
    ivtype=’long’, ivsize=4, nvtype=’double’, nvsize=8, Off_t=’__int64′, lseeksize=8
    alignbytes=8, prototype=define
    Linker and Libraries:
    ld=’link’, ldflags =’-nologo -nodefaultlib -debug -opt:ref,icf  -libpath:”C:\Perl\lib\CORE”  -machine:x86′
    libpth=\lib
    libs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib  version.l
    perllibs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib  versi
    libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl58.lib
    gnulibc_version=’undef’
    Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=’ ‘
    cccdlflags=’ ‘, lddlflags=’-dll -nologo -nodefaultlib -debug -opt:ref,icf  -libpath:”C:\Perl\lib\CORE”  -machine:x86′

    Characteristics of this binary (from libperl):
    Compile-time options: MULTIPLICITY USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS
    Locally applied patches:
    ActivePerl Build 811
    21540 Fix backward-compatibility issues in if.pm
    23565 Wrong MANIFEST.SKIP
    Built under MSWin32
    Compiled at Dec 13 2004 09:52:01
    @INC:
    C:/Perl/lib
    C:/Perl/site/lib
    .

    Above is my detailed configuration

    C:\>

    One line program to print all the PERL environment variables sorted in Ascending into a file.

    C:\>perl -e “foreach (sort keys %ENV) {print \”$_ = $ENV{$_}\n\”; } “; > \Temp\perl-env.txt

    C:\>

    Categories: Perl
    Follow

    Get every new post delivered to your Inbox.

    Join 96 other followers