Sunday, November 12, 2017

Read Excel files in Perl

1. install the module for Excel xls files.
$ cpan install Spreadsheet::ParseExcel


2. install the module for Excel xlsx/xlsm files.
cpan install Archive::Zip
cpan install Spreadsheet::ParseXLSX
* on OpenSUSE, because of the bug in libzip, it is needed to do make install in the build folder.

3. try to parse the xls, xlsx, xlsm files.

# TestXLS.pl

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;

my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('TestPerl.xls');

if ( !defined $workbook ) { 
    die $parser->error(), ".\n";
}

for my $worksheet ( $workbook->worksheets() ) { 

    my ( $row_min, $row_max ) = $worksheet->row_range();
    my ( $col_min, $col_max ) = $worksheet->col_range();
    my ( $worksheet_name) = $worksheet->get_name();
    print "Worksheet is $worksheet_name which has MinRow($row_min), MaxRow($row_max), MinCol($col_min), MaxCol($col_max)\n";

    for my $row ( $row_min .. $row_max ) { 
        for my $col ( $col_min .. $col_max ) { 

            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            print "Row, Col    = ($row, $col)\n";
            print "Value       = ", $cell->value(),       "\n";
            print "Unformatted = ", $cell->unformatted(), "\n";
            print "\n";
        }   
    }   
}

# TestXLSX.pl

#!/usr/bin/perl -w

use Text::Iconv;
use Spreadsheet::XLSX;

my $converter = Text::Iconv -> new ("utf-8", "windows-1251");

# Text::Iconv is not really required.
# This can be any object with the convert method. Or nothing.

my $excel = Spreadsheet::XLSX -> new ('TestPerl.xlsx', $converter);

foreach my $sheet (@{$excel -> {Worksheet}}) {
    printf("Sheet: %s\n", $sheet->{Name});
    $sheet -> {MaxRow} ||= $sheet -> {MinRow};
    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
        $sheet -> {MaxCol} ||= $sheet -> {MinCol};
        foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {
            my $cell = $sheet -> {Cells} [$row] [$col];
            if ($cell) {
                printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});
            }   
        }   
    }   
}

# TestXLSM.pl

#!/usr/bin/perl -w

use Text::Iconv;
use Spreadsheet::XLSX;

my $converter = Text::Iconv -> new ("utf-8", "windows-1251");

# Text::Iconv is not really required.
# This can be any object with the convert method. Or nothing.

my $excel = Spreadsheet::XLSX -> new ('TestPerl.xlsm', $converter);

foreach my $sheet (@{$excel -> {Worksheet}}) {
    printf("Sheet: %s\n", $sheet->{Name});
    $sheet -> {MaxRow} ||= $sheet -> {MinRow};
    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
        $sheet -> {MaxCol} ||= $sheet -> {MinCol};
        foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {
            my $cell = $sheet -> {Cells} [$row] [$col];
            if ($cell) {
                printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});
            }   
        }   
    }   
}





Monday, January 12, 2015

Install CPAN locally

1. Download HTTP-Tiny and install. http://search.cpan.org/CPAN/authors/id/D/DA/DAGOLDEN/HTTP-Tiny-0.054.tar.gz
2. Install local::lib locally
  2.1 Download local::lib from http://search.cpan.org/CPAN/authors/id/H/HA/HAARG/local-lib-2.000015.tar.gz.
  2.2 Untar the file
  2.3 perl Makefile.PL --bootstrap=~/perl5
  2.4 make && make install
3. Install CPANM
  3.1 Install CPANM from http://search.cpan.org/CPAN/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7024.tar.gz
  3.2 Install other modules for CPANM.

  • Test::Simple 
  • JSON::PP 
  • version 
  • CPAN::Meta::YAML 
  • Parse::CPAN::Meta 
  • CPAN::Meta::Requirements 
  • CPAN::Meta 
  • Locale::Maketext::Simple 
  • Params::Check 
  • Module::Load 
  • Module::CoreList 
  • Module::Metadata 
  • Module::Load::Conditional 
  • IPC::Cmd 
  • Perl::OSType 
  • ExtUtils::CBuilder 
  • CPAN::Meta 
  • CPAN::Meta::Requirements 
  • CPAN::Meta::YAML 
  • ExtUtils::CBuilder
  • ExtUtils::MakeMaker

4. Setup CPAN by using local::lib
5. Download below modules
Test::More http://search.cpan.org/CPAN/authors/id/E/EX/EXODIST/Test-Simple-1.001014.tar.gz
Email::Date::Format http://search.cpan.org/CPAN/authors/id/R/RJ/RJBS/Email-Date-Format-1.005.tar.gz
Date::Format, Date::Parse http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/TimeDate-2.30.tar.gz
Mail::Address http://search.cpan.org/CPAN/authors/id/M/MA/MARKOV/MailTools-2.14.tar.gz
MIME::Types http://search.cpan.org/CPAN/authors/id/M/MA/MARKOV/MIME-Types-2.09.tar.gz
MIME::Lite http://search.cpan.org/CPAN/authors/id/R/RJ/RJBS/MIME-Lite-3.030.tar.gz


5. Test MIME::Lite
#!/usr/bin/env perl
use MIME::Lite;
$msg = MIME::Lite->new(
        From    =>      'long.hu@longstar.com',
        To      =>      'long.hu@longstar.com',
        Cc      =>      'long.hu@longstar.com',
        Subject =>      'Hello, World',
        Data    =>      'Here is a Test Email'
);
$msg->send();


Wednesday, January 7, 2015

Use cpanm locally

wget -O- http://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib
eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`
echo 'eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`' >> ~/.bash_profile
echo 'export MANPATH=$HOME/perl5/man:$MANPATH' >> ~/.bash_profile

Sunday, December 22, 2013

Install Catalyst

1. Initialize cpan

cpan> o conf init

2. Install CPAN::SQLite and LWP

cpan[4]> install CPAN::SQLite LWP YAML Test::Output Test::LeakTrace Algorithm::C3 SUPER Module::Refresh Data::Visitor Test::Builder Specio IO::String

3. Switch use_sqlite if it was not

cpan> o conf use_sqlite 0
cpan> o conf commit
cpan> reload index
cpan> install CPAN::SQLite
cpan> o conf use_sqlite 1
cpan> o conf commit

4. Install Catalyst::Runtime Catalyst::Devel

cpan[4]> install Catalyst::Runtime Catalyst::Devel

5. Check Installation


[catalyst@catalyst ~]$ perlbrew list
  perl-5.6.2
  perl-5.8.9
  perl-5.10.1
  perl-5.12.5
  perl-5.14.4
  perl-5.16.3
  perl-5.18.1
  perl-5.19.7
[catalyst@catalyst ~]$ perlbrew use perl-5.8.9

A sub-shell is launched with perl-5.8.9 as the activated perl. Run 'exit' to finish it.

bash-4.1$ source perlbrew.sh
[perl-5.8.9] catalyst@catalyst ~$ [ 164 ]>> : perl -MCatalyst::Runtime -e 'print Catalyst::Runtime->VERSION'
5.90052[perl-5.8.9] catalyst@catalyst ~$ [ 165 ]>> :


Saturday, December 21, 2013

Install Perlbrew

1. Install the Perlbrew

Install development tools.
yum groupinstall "Development Tools"
yum install wget lynx ncftpget ncftp ftp gpg patch applypatch


Copy & Paste this line into your terminal:
\curl -L http://install.perlbrew.pl | bash
 
Or, if your system does not have curl but something else:
# Linux
\wget -O - http://install.perlbrew.pl | bash

# FreeBSD
\fetch -o- http://install.perlbrew.pl | sh
 
If you prefer to install with cpan, there are two steps:
sudo cpan App::perlbrew
perlbrew init 
If it is installed with cpan, the perlbrew executable should be installed as /usr/bin/perlbrew or /usr/local/bin/perlbrew. For all users who want to use perlbrew, a prior perlbrew init needs to be executed.

The default perlbrew root directory is ~/perl5/perlbrew, which can be changed by setting PERLBREW_ROOT environment variables before the installation and initialization. For more advanced installation process, please read the perlbrew document.

export PERLBREW_ROOT=$HOME/perl5/perlbrew
export PERLBREW_HOME=$PERLBREW_ROOT
export PATH=$PATH:$PERLBREW_HOME/bin


2. Check available Perl distribution.

[catalyst@catalyst ~]$ perlbrew available
  perl-5.19.7
  perl-5.18.1
  perl-5.16.3
  perl-5.14.4
  perl-5.12.5
  perl-5.10.1
  perl-5.8.9
  perl-5.6.2
  perl5.005_04
  perl5.004_05
  perl5.003_07



3. Install Perl distribution


perlbrew install-multiple  perl-5.6.2 perl-5.8.9 perl-5.10.1 perl-5.12.5 perl-5.14.4 perl-5.16.3 perl-5.18.1 perl-5.19.7




4. Configure the shell variables : perlbrew.sh


update_for_perlbrew() {
   local name="$( perlbrew list | perl -lane'print $F[1] if /^\*/' )"

   if [ -z "$name" ]; then
      name="$( perl -e'$v = $^V || $]; $v =~ s/^v//; print $v' )"
   fi

   # Adjust window title here

   echo "[$name]"
}


PS1='$( update_for_perlbrew ) \u@\h \W$ [ \! ]>> : '

alias ll="ls -l"
alias lla="ls -al"