perl

Perl FAQ

in
Perl

Perl tips and tricks

If you simply need to quickly check to see if a module is available, you can check for its documentation.

 perldoc Module::Name

You can also try to include the module in a one-liner to see if perl finds it.

 perl -MModule::Name -e1

To check the version number of a module we assumes the module uses $VERSION (most CPAN modules do):

 perl -MModuleName -e"print $ModuleName::VERSION;"

To list directories used by module:

  1.  perl -MExtUtils::Installed
  2.       -e'$,="\n";print ExtUtils::Installed->new()->directories("ModuleName")," "'

This technique relies on ExtUtils::Installed,which is part of the standard Perl distro these days.

Best way to guess where local perl modules are installed is:

  1. #!/usr/bin/perl
  2. use strict;
  3. use FindBin qw[$Bin];
  4. use Cwd 'abs_path';
  5. BEGIN { $ENV{MODROOT} = abs_path("$Bin..") unless $ENV{MODROOT}; }
  6. use lib "$ENV{MODROOT}/lib/perl5", "$ENV{MODROOT}/lib/perl5/site_perl";
  7. use MyModuleFromTheLocalPath;
  8. print "path=".join(':',@INC)."\n";

Comprehensive discussion is on the "guess module path" page.

Good practices
Protect calls you expect could legitimately fail with evals
Once RaiseError is turned on (see RaiseError), you no longer need to test every operation worked (see exception in "Test inserts/updates worked"). However, if you expect an operation may legitimately fail, wrap it in an eval. e.g.

  1.   $dbh->do(qq/insert into table (x,y) values ($a, $b)/);
  2. };
  3. warn "failed to insert $a,$b into table - $dbh->errstr"
  4.   if ($@);

e.g. if column x was a defined as an integer and the insert could legitimately have $a set to a value that does not convert to an integer, this code would issue a warning instead of dying.

Sleep for 250 milliseconds:

select(undef, undef, undef, 0.25);
select((select(MY_FILE), $| = 1))[0]);

My perl does not understand this code and misinterprets file handle select as POSIX select.

  1. no strict 'refs';
  2. $foo = 5;
  3. $name = "foo";
  4. print $$name;
  5. 5
  1. sub t2s ($) {
  2.   @_ = localtime($_[0]);
  3.   return sprintf '%04d-%02d-%02d %02d:%02d:%02d',
  4.                  $_[5]+1900,$_[4]+1,$_[3],$_[2],$_[1],$_[0];
  5. }
  1. use POSIX;
  2. sub s2t ($) {
  3.   $_ = $_[0];
  4.   return /^(\d\d\d\d)\-(\d\d)\-(\d\d) (\d\d)\:(\d\d)\:(\d\d)\s*/
  5.         ? mktime($6, $5, $4, $3, $2-1, $1-1900, undef, undef, (localtime)[8])
  6.         : undef;
  7. }

Creating Ubuntu packages from CPAN modules

Some of our PERL tools require some CPAN modules that are not part of the standard Ubuntu distribution. It's obviously possible to install the module using CPAN but I like using deb packages where possible as then you only have one repository to manage. Fortunately with dh-make-perl it is possible to quickly turn any CPAN module in to a debian package!Read more

OpenSSL with nonblocking sockets in Perl (devpit.org)

SSL/TLS is complete chaos with little documentation. OpenSSL is a huge library. It has tons of functionality, and has some reasonable documentation, but gives you no idea where to start. Perl has, as usual, lots of modules and few to the point. Net::SSLeay seems to be Perl's raw canonical wrapper for OpenSSL, although if I hadn't told you, you'd have to examine a dozen modules with similar names before you came to this conclusion.Read more

Story URL: 

Perl Net::LDAP

in

This page is just a collection of links.

Perl's DBD::Sybase on Windows

| sybase | perl | windows

Solution

The solution is to create an XML file called perl.exe.manifest and place it in the same directory as your perl.exe and put the following contents in that file:Read more

How to find a perl module path

in
  • How to guess a perl program installation directory:
#!/usr/bin/perl
use strict;
BEGIN {
  use File::Spec; use Cwd;
  $ENV{MODROOT} =
    Cwd::abs_path(File::Spec->catfile((File::Spec->splitpath($0))[1],'..'))
  unless exists $ENV{MODROOT};
}
print "HOME=$ENV{MODROOT}\n";
  • A shorter way:
#!/usr/bin/perl
use strict;
BEGIN {
  use File::Spec::Functions 'splitpath','catfile';
  use Cwd 'abs_path';
  $ENV{MODROOT} = abs_path(catfile((splitpath($0))[1],'..'))
    unless exists $ENV{MODROOT};
}
print "HOME=$ENV{MODROOT}\n";
  • Perl installs modules under ".../lib/perl5", ".../lib/perl5/5.8.3/, ".../lib/perl5/5.8.3/i386-linux-thread-multi". Read more

How to insert a blob into Sybase from Perl

If we attempt to modify a table with text field from Perl:

 my $sth = $dbh->prepare("update table_with_text_field set text_field=? where pk=?");

DBD::Sybase fails with error:

ct_result(ct_dynamic(CS_PREPARE)) returned -205 at
.../site/lib/DBD/Sybase.pm line 133.DBD::Sybase::Database prepare failed:
  Server message number=2782 severity=16 state=1 line=1 server=tst procedure=DBD5
  text=An untyped variable in the PREPARE statement 'DBD5' is being resolved
  to a TEXT or IMAGE type. This is illegal in a dynamic PREPARE statement.

This happens because one cannot use prepared statements with text or image data.The documentation

 perldoc DBD::Sybase

says that IMAGE and TEXT datatypes can not be passed as parameters using ?-style placeholders,and placeholders can't refer to IMAGE or TEXT columns. This is a limitation of the TDS protocolused by Sybase, not a DBD::Sybase limitation.Read more

Syndicate content