read data from excel in perl - perl

I am trying to read data from excel, i have below code.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use feature 'say';
use Spreadsheet::XLSX;
use Spreadsheet::Read qw(ReadData);
my $book = ReadData ('/tmp/simple.xlsx');
say 'A1: ' . $book->[1]{A1};
my #row = Spreadsheet::Read::row($book->[1], 1);
for my $i (0 .. $#row) {
say 'A' . ($i+1) . ' ' . ($row[$i] // '');
}
my #rows = Spreadsheet::Read::rows($book->[1]);
foreach my $i (1 .. scalar #rows) {
foreach my $j (1 .. scalar #{$rows[$i-1]}) {
say chr(64+$i) . " $j " . ($rows[$i-1][$j-1] // '');
}
}
getting the error as:
Can't locate OLE/Storage_Lite.pm in #INC (#INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/perl5/Spreadsheet/ParseExcel.pm line 21.
BEGIN failed--compilation aborted at /usr/local/share/perl5/Spreadsheet/ParseExcel.pm line 21.
Compilation failed in require at /usr/local/share/perl5/Spreadsheet/XLSX.pm line 14.
BEGIN failed--compilation aborted at /usr/local/share/perl5/Spreadsheet/XLSX.pm line 14.
Compilation failed in require at read_excel.pl line 6.
BEGIN failed--compilation aborted at read_excel.pl line 6.
I have every related module. but as per error when i am going to download OLE/Storage_Lite.pm from the CPAN, page is going blank, and from metacpan below error is coming.
https://cpan.metacpan.org/authors/id/M/MS/MSCHWARTZ/OLE-Storage-0.386.tar.gz
Resolving cpan.metacpan.org... 151.101.128.249, 151.101.192.249, 151.101.64.249, ...
Connecting to cpan.metacpan.org|151.101.128.249|:443... connected.
ERROR: certificate common name “a.ssl.fastly.net” doesn’t match requested host name “cpan.metacpan.org”.
To connect to cpan.metacpan.org insecurely, use ‘--no-check-certificate’.

Try downloading the module tar zip from cpan or metacpan. Then build the module locally using any make utility(e.g.dmake). You can find more info for building module locally from here.

Related

Bug with use if REGEX_COND

I would like to conditional load a package if the program name is a test script ending in .t.
However, I'm running into a bug where use if fails when the condition is a regex. I've tested this in Perl 5.10 and 5.16.
The following is my test script ending in .t:
#!/usr/bin/env perl
use v5.10;
BEGIN { say "\$0 is '$0'" }
use if $0 =~ /\.t\z/, 'List::Util', ('pairmap');
say "List::Util is " . ( $INC{"List/Util.pm"} ? '' : 'NOT ' ) . 'included';
Outputs:
$ ./test.t
$0 is './test.t'
List::Util is included
However, the same file with a .pl extension fails:
$ ./test.pl
$0 is './test.pl'
Can't locate pairmap.pm in #INC (#INC contains: /usr/lib64/perl5/5.10.0 /usr/lib64/perl5 /usr/local/share/perl5/x86_64-linux-thread-multi /usr/local/share/perl5 /usr/local/lib64/perl5 /usr/share/perl5 /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/share/perl5/if.pm line 13.
BEGIN failed--compilation aborted at ./test.pl line 7.
I can coerce the code into working if I double bang the regex or change it to a substr comparison:
use if !!( $0 =~ /\.t\z/ ), 'List::Util', ('pairmap');
use if substr( $0, -2 ) eq '.t', 'List::Util', ('pairmap');
Outputs:
$ ./test.pl
$0 is './test.pl'
List::Util is NOT included
Is this a known bug? If so, in what version was it fixed?
This is a bug in your code.
The argument list after use MODULE is in, well, list context.
m// in list context returns a list of captured strings if successful (or 1 if the regex contains no capturing groups), or the empty list on failure.
Thus:
use if "test.pl" =~ /\.t\z/, 'List::Util', ('pairmap');
is equivalent to
use if (), 'List::Util', ('pairmap');
(The match failed, so an empty list is returned.)
, in list context is the list concatenation operator, so this gives:
use if 'List::Util', 'pairmap';
'List::Util' is a true value, so this ultimately ends up loading pairmap.pm:
use pairmap;
The fix is to give the match scalar context:
use if scalar($0 =~ /\.t\z/), 'List::Util', 'pairmap';
(! also supplies scalar context to its operand, so !! has the same effect.)

using perl to connect to apache cassandra 2.0.1

I've been working on Cassandra from the past two weeks and am stuck in a few places i was hoping you could help me out with it.
I've installed the apache-cassandra-2.0.1,perlcassa-master,Perl5.10, Thrift::XS and Time::HiRes.I am still not able to connect to Cassandra .when i run perl Makefile.PL i get the follow warning
"Warning: prerequisite Class::Accessor 0 not found. Writing Makefile
for perlcassa" Ignoring the warning when i ran my script i got the
following error. "Base class package "Class::Accessor" is empty.
(Perhaps you need to 'use' the module which defines that package
first,or make that module available in #INC (#INC contains:
/usr/local/lib64/perl5 /usr/local/share/perl5
/usr/lib64/perl5/vendor_perl enter code
here/usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5
.). at /usr/local/share/perl5/Cassandra/Types.pm line 38 BEGIN
failed--compilation aborted at
/usr/local/share/perl5/Cassandra/Types.pm line 38. Compilation failed
in require at /usr/local/share/perl5/Cassandra/Cassandra.pm line 11.
BEGIN failed--compilation aborted at
/usr/local/share/perl5/Cassandra/Cassandra.pm line 11. Compilation
failed in require at /usr/local/share/perl5/perlcassa/Client.pm line
18. BEGIN failed--compilation aborted at /usr/local/share/perl5/perlcassa/Client.pm line 18. Compilation failed
in require at /usr/local/share/perl5/perlcassa.pm line 159. BEGIN
failed--compilation aborted at /usr/local/share/perl5/perlcassa.pm
line 159. Compilation failed in require at ./cassconn.pl line 2. BEGIN
failed--compilation aborted at ./cassconn.pl line 2."
my script cassconn.pl
#!/usr/bin/perl
use perlcassa;
my $result = $obj->exec(
"SELECT n,ei,toj,sal,d FROM sample.test WHERE n='divya'",
{key_value => 'n'}
);
my $row = $result->fetchone();
print "Row key, col01: ".$row->{key}.", ".$row->{col01}."\n";
I don't know if it will help you, I will explain how I did it.
Download and install Thrift compiler on your machine.
Install Thrift library for Perl client application using cpan
cpan Thrift
Move to the directory you installed Cassandra, there is interface directory inside it
cd /path/to/cassandra/interface/
Then generate Cassandra library for Perl using Thrift compiled you installed earlier
/path/to/thrift/binary/Thrift --gen Perl cassandra.thrift
It will create a directory gen-perl in the present directory, you will need to include it using use statement in your Perl scripts. Here's an example script, althou it doesn't work with newest versions of Cassandra and Thrift it is pretty similar, in case if you have issues I provide you with example that work with Cassandra-2.0.8 and Thrift-0.9.1.
#!/usr/bin/perl -w
use strict;
use warnings;
# Change for your environment
use lib '/path/to/cassandra/interface/gen-perl';
use Cassandra::Cassandra;
use Cassandra::Constants;
use Cassandra::Types;
use Thrift;
use Thrift::BinaryProtocol;
use Thrift::Socket;
use Thrift::FramedTransport;
use Data::Dumper;
# localhost and 9160 are default in storage conf for rpc listener
my $socket = new Thrift::Socket('localhost', 9160);
my $transport = new Thrift::FramedTransport($socket);
my $protocol = new Thrift::BinaryProtocol($transport);
my $client = new Cassandra::CassandraClient($protocol);
eval {
$transport->open();
my $consistency_level = Cassandra::ConsistencyLevel::ONE;
my $keyspace = 'DEMO';
my $column_parent = new Cassandra::ColumnParent();
$column_parent->{column_family} = "User";
$column_parent->{super_column} = undef;
$column_parent->{column} = 'age';
$client->set_keyspace($keyspace);
my $column = new Cassandra::Column();
my $row_key = 'Spider-Man';
$column->{name} = 'first';
$column->{value} = 'Peter';
$column->{timestamp} = time;
$client->insert($row_key, $column_parent, $column, $consistency_level);
$column->{name} = 'last';
$column->{value} = 'Parker';
$column->{timestamp} = time;
$client->insert($row_key, $column_parent, $column, $consistency_level);
$column->{name} = 'age';
$column->{value} = '18';
$column->{timestamp} = time;
$client->insert($row_key, $column_parent, $column, $consistency_level);
$transport->close();
}; if($#){
warn(Dumper($#));
}
1;
It adds an entry to Cassandra keyspace DEMO with key spider-man and following column values peter, parker, 18.
Here is the result of query on cassandra-cli after execution of this script:
[default#DEMO] list User;
Using default limit of 100
Using default cell limit of 100
(...)
-------------------
RowKey: spidey
=> (name=age, value=18, timestamp=1404222046)
=> (name=first, value=Peter, timestamp=1404222046)
=> (name=last, value=Parker, timestamp=1404222046)
(...)
4 Rows Returned.
Elapsed time: 367 msec(s).

How to install all dependencies of a CPAN module?

How to install all dependencies of the module Nmap::Scanner with cpan in Perl? I did, cpan Nmap::Scanner. But, needing other dependencies for module.
#!/bash/perl
use Nmap::Scanner;
my $scan = Nmap::Scanner->new();
$scan->add_target('localhost');
$scan->add_target('host.i.administer');
$scan->add_scan_port('1-1024');
$scan->add_scan_port('31337');
$scan->tcp_syn_scan();
$scan->noping();
my $results = $scan->scan();
my $hosts = $results->gethostlist();
while (my $host = $hosts->get_next()) {
print "On " . $host->hostname() . ": \n";
my $ports = $host->get_port_list();
while (my $port = $ports->get_next()) {
print join(' ',
'Port',
$port->service() . '/' . $port->portid(),
'is in state',
$port->state(),
"\n"
);
}
}
I did, but when running the script in perl, shows this in console.
Can't locate XML/SAX/Exception.pm in #INC (#INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl` /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5 .) at /usr/local/share/perl5/XML/SAX/ParserFactory.pm line 12.
BEGIN failed--compilation aborted at /usr/local/share/perl5/XML/SAX/ParserFactory.pm line 12.
Compilation failed in require at /usr/local/share/perl5/Nmap/Scanner/Backend/XML.pm line 8.
BEGIN failed--compilation aborted at /usr/local/share/perl5/Nmap/Scanner/Backend/XML.pm line 8.
Compilation failed in require at /usr/local/share/perl5/Nmap/Scanner/Scanner.pm line 4.
BEGIN failed--compilation aborted at /usr/local/share/perl5/Nmap/Scanner/Scanner.pm line 4.
Compilation failed in require at /usr/local/share/perl5/Nmap/Scanner.pm line 10.
BEGIN failed--compilation aborted at /usr/local/share/perl5/Nmap/Scanner.pm line 10.
Compilation failed in require at e1-insecure.pl line 3.
BEGIN failed--compilation aborted at e1-insecure.pl line 3.
cpan resolves dependencies automatically. To install execute the following command:
cpan Nmap::Scanner
An alternative installer for CPAN modules is cpanminus. Using cpanm:
cpanm Nmap::Scanner

cannot run perl program: Can't locate Time/Piece.pm in #INC with perl5.8.4 and Can't locate DBI.pm in #INC with perl 5.12.3 [duplicate]

This question already has answers here:
What's the easiest way to install a missing Perl module?
(24 answers)
Closed 7 years ago.
I have Solaris 10,i am trying to run a Perl program.
I have two perl versions installed:
/usr/bin/perl of version 5.8.4
and
/usr/local/bin/perl of version 5.12.3
I have installed the DBI package (it got installed here, /usr/perl5/site_perl/5.8.4/sun4-solaris-64int/auto/DBI/.packlist),the problem I get by executing the Perl program with different perl version is (In ubuntu its working fine).
bash-3.00# perl temp.pl
Can't locate Time/Piece.pm in #INC (#INC contains: /usr/perl5/5.8.4/lib/sun4-
solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int
/usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-
solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .) at
temp.pl line 4.
BEGIN failed--compilation aborted at temp.pl line 4.
bash-3.00# /usr/local/bin/perl temp.pl
Can't locate DBI.pm in #INC (#INC contains: /usr/local/lib/perl5/site_perl/5.12.3
/sun4-solaris /usr/local/lib/perl5/site_perl/5.12.3 /usr/local/lib/perl5/5.12.3/sun4-
solaris /usr/local/lib/perl5/5.12.3 /usr/local/lib/perl5/site_perl .) at temp.pl line5.
BEGIN failed--compilation aborted at temp.pl line 5.
I have tried hell lot of ways but not getting how to run my Perl program on solaris.
Could anybody help please.
Below is my program. In fact it was redefined by #Borodin. Thanks a lot to him.
use strict;
use warnings;
use Time::Piece;
use DBI;
open my $log, '<', '/opt/testlogs/test.log' or die "Unable to open log file: $!";
my ( $count_start, $count_interim, $count_stop ) = ( 0, 0, 0 );
while (<$log>) {
if (/server start/) {
$count_start++;
}
elsif (/server interim-update/) {
$count_interim++;
}
elsif (/server stop/) {
$count_stop++;
}
}
print <<END;
Start: $count_start
Interim: $count_interim
Stop: $count_stop
END
print localtime->strftime("%b %e %H:%M:%S"), "\n";
my $dbh = DBI->connect( "DBI:Pg:dbname=postgres;host=localhost", "postgres", "postgres", { 'RaiseError' => 1 } );
my $rows = $dbh->do(
"insert into radius (server_start, server_stop, server_interim)
Values ($count_start, $count_stop, $count_interim)"
);
printf "%d %s affected\n", $rows, $rows == 1 ? 'row' : 'rows';
You don't have Time::Piece installed for /usr/bin/perl, so install it.
/usr/bin/perl -MCPAN -e install Time::Piece
You don't have DBI installed for /usr/local/bin/perl, so install it.
/usr/local/bin/perl -MCPAN -e install DBI

Perl script compilation into .exe error

I am trying to create a .exe in perl. It works fine until I try to compile it into an exe. I am using Komodo IDE 5. I have posted my script and the error below. I have added the modules, LWP::UserAgent, NET, and Google::Voice and it still doesnt work. I use perlapp to create the .exe
#!/usr/bin/perl -w
use strict;
use warnings;
use Google::Voice;
use Date::Calc qw(Delta_Days);
use Net::Twitter;
#Set Days
my #today = (localtime)[5,4,3];
$today[0] += 1900;
$today[1]++;
my #RT = (2012, 7, 7);
my $days = Delta_Days(#today, #RT);
#Get Quotes and Phone Numbers
open FILE, "c:/Countdown/countdownNumbers.txt" or die "Couldn't open file: $!";
my $numbers = join("", <FILE>);
close FILE;
open FILETWO, "c:/Countdown/Quotes.txt" or die "Couldn't open file: $!";
my $quotes = join("", <FILETWO>);
close FILETWO;
#Create Arrays and Lengths
my #numbersArray = split(/[\n\r\l]+/, $numbers);
my #quotesArray = split(/[\n\r\l]+/, $quotes);
my $length = #numbersArray;
my $QuotesLength = #quotesArray;
#Send Text Message
for(my $i = 0; $i < $length; $i++){
my $g = Google::Voice->new->login('secret', 'secret');
$g->send_sms($numbersArray[$i] => " Countdown\nDays Left: " . $days . "\n Quote:\n" . $quotesArray[0]);
}
#Send Twitter Message
my $nt = Net::Twitter->new(
traits => [qw/OAuth API::REST/],
consumer_key => 'secret',
consumer_secret => 'secret',
access_token => 'secret',
access_token_secret => 'secret'
);
my $result = $nt->update($days .' Days left!');
$result = $nt->update('Quote: ' . $quotesArray[0]);
#Rewrite the file and close it
open FILETWO, ">c:/Countdown/Quotes.txt";
for(my $i = 1; $i < $QuotesLength; $i++){
print FILETWO $quotesArray[$i] . "\n";
}
close FILETWO;
errors
Algorithm\Diff\XS.pm:
error: Can't locate Algorithm\Diff\XS.pm
refby: C:\Perl\site\lib\Array\Diff.pm line 7
Date\Calc\XS.pm:
error: Can't locate Date\Calc\XS.pm
refby: C:\Perl\lib\Date\Calc.pm line 26
I18N\Langinfo.pm:
error: Can't locate I18N\Langinfo.pm
refby: C:\Perl\lib\Encode\Locale.pm line 51
JSON\PP58.pm:
error: Can't locate JSON\PP58.pm
refby: C:\Perl\lib\JSON\PP.pm
Net.pm:
error: Can't locate Net.pm
refby: perlapp --add Net::
Can't locate Mojo/EventEmitter.pm in #INC (#INC contains:) at /<C:\Users\Chris\Desktop\Countdown\RT.exe>Mojo/Base.pm line 32.
BEGIN failed--compilation aborted at /<C:\Users\Chris\Desktop\Countdown\RT.exe>Mojo/UserAgent.pm line 2.
BEGIN failed--compilation aborted at /<C:\Users\Chris\Desktop\RTCountdown\RT.exe>Google/Voice.pm line 6.
BEGIN failed--compilation aborted at RT.pl line 4.
Compiling perl script into exe file is not so straight-forward, I'm afraid. ) Check this discussion at Perlmonks for details.
From what you've quoted it seems that you might start fixing that with installing additional modules: Algorithm::Diff::XS, Date::Calc::XS, etc.
If you use latest version of perlapp, send this error to ActiveState support.
Temporarily you can use PAR::Packer instead of perlapp. Install PAR::Packer with cpan shell (ppm may not work). Then run
pp -c t1.pl
It will create a.out. If it would not work, install Module::ScanDeps from svn: http://svn.openfoundry.org/par/Module-ScanDeps/trunk/ - I fixed a couple of possible problems for your program.
I never used perlapp, but it may have command line switches to provide a list of modules to include.
I have downloaded Langinfo.pm from http://search.cpan.org/src/RJBS/perl-5.16.1/ext/I18N-Langinfo/Langinfo.pm to C:\Perl\lib\I18N and it works.
Komodo IDE, version 6.1.3
Perl Dev Kit Pro v9.1.1.
ActivePerl-5.14.2.1402-MSWin32-x86-295342