Difference between revisions of "Perl"

From Leaky
Jump to: navigation, search
(Accessing SQL Server from Perl)
(Added to programming category)
Line 123: Line 123:
 
  STDERR->autoflush(1);
 
  STDERR->autoflush(1);
 
  STDOUT->autoflush(1);
 
  STDOUT->autoflush(1);
 +
 +
[[Category:Programming]]

Revision as of 10:59, 2 October 2018

Frameworks

Modules

DBIx::Class modules

Custom Perl

Installing a custom version of Perl just for a single user (e.g a project) without breaking the system-wide Perl.

Packages required:

gcc patch bzip2 gcc-c++ make automake

As the unprivileged user:

$ curl -L https://install.perlbrew.pl | bash
$ source ~/perl5/perlbrew/etc/bashrc
$ perlbrew install perl-5.20.1
$ perlbrew switch perl-5.20.1
$ perlbrew install-cpanm
$ echo 'source /home/plates/perl5/perlbrew/etc/bashrc' >> ~/.bashrc

After this you should be always using Perl 5.20.1 as this user (that's what the perlbrew switch does). To install modules, simply use cpanm followed by the module name.

Extra Perl modules for Catalyst dev

After installing a custom version of Perl as above it's a good idea to install a bunch of frequently used modules if you're doing any kind of Catalyst development.

cpanm Catalyst::Devel DBIx::Class::TimeStamp DBIx::Class::PassphraseColumn \
  DBIx::Class::InflateColumn::Serializer Catalyst::Model::DBIC::Schema \
  Catalyst::Plugin::Authentication Catalyst::Plugin::Authorization::Roles \
  Catalyst::Plugin::Session Catalyst::Plugin::Session::Store::DBI \
  Catalyst::Plugin::Session::State::Cookie Catalyst::Plugin::StatusMessage \
  Catalyst::Authentication::Store::DBIx::Class Catalyst::View::TT \
  Catalyst::View::JSON Log::Log4perl::Catalyst DBD::mysql Email::Valid \
  Text::CSV_XS LWP::Protocol::https XML::Simple XML::Writer GD::Image \
  GD::Text FCGI::ProcManager Catalyst::View::Email::Template \
  Template::Plugin::DateTime HTML::Tiny Hashids Term::Size::Any Time::ParseDate

Accessing SQL Server from Perl

Install the freetds software (both main package and devel)

yum install freetds freetds-devel

Configure a server definition in the global config file, I'm not sure there's a way to avoid having to do this.

cat >> /etc/freetds.conf <<EOF
[myserver]
  host = 192.168.1.4
  port = 1433
  tds version = 7.0
EOF

Test the connection using the following:

tsql -S myserver -U myuser

You should be prompted for a password (SQL auth, not Windows!) and hopefully after providing one, you should see a prompt 1) waiting for an SQL query.

Run the following query to see the available databases.

SELECT name, database_id, create_date
FROM sys.databases;
GO

If this didn't work, go back and check your setup.

Install the perl module for DBD::Sybase - don't bother with the ODBC method, this way works just fine.

SYBASE=/usr cpanm DBD::Sybase

Connect using Perl and DBD::Sybase

#!/usr/bin/env perl
use DBI;
my $dbh = DBI->connect("DBI:Sybase:server=myserver","myuser","mypass") or die $DBI::errstr;
$dbh->disconnect();

If this works, you're all set.

DBIx::Class

Relationships

belongs_to means: a field (or fields) in THIS table is a foreign key (contains the primary key of) THAT other table.

has_many means: there are multiple (zero or more) rows in THAT table which contains THIS tables primary key.

has_one means: there is a row in THAT table which contains THIS tables primary key.

might_have means: the same as has_one, except its not guaranteed to be there.

Tips

Mixing STDOUT and STDERR output in the right order

STDOUT and STDERR are block buffered when there is no terminal, or line buffered when there is a terminal. This has the unfortunate side effect of making output from print/warn mess up when running a script from cron when the same script works perfectly run from the command line.

Redirect STDERR to the same stream as STDOUT (equiv to 2>&1 in bash) - this isn't essential, but makes it easier to pipe the output through tail/mail/etc, or redirect the output to a logfile.

close(STDERR);
open(STDERR, ">&STDOUT");

Make both streams non-buffering

select(STDERR);
$| = 1;
select(STDOUT);
$| = 1;

Alternatively, instead of the select() above, you can use IO::Handle

use IO::Handle;
STDERR->autoflush(1);
STDOUT->autoflush(1);