Telephone +44(0)1524 64544
Email: info(at)shadowcat.co.uk

Sat Dec 22 00:30:00 2012

Slides for the talk enperl-systems at ukuug-spring-2010

Enlightened Perl
for Systems
Administrators

-

Matt S Trout

-

Matt S Trout
shadowcat.co.uk

-

But Perl's
Dying!

-

Netcraft
Confirms?

-

X-Powered-By

-

PERL is
dying

-

Powered
by Perl

-

bbc.co.uk
magazines.com
booking.com

-

Mature
community

-

Mature
practices

-

Converging
Platform

-

Catalyst
DBIx::Class
Moose

-

(all of which
Netcraft now
ask for)

-

It's a great
time to be
writing perl
applications

-

But I said
"for sysadmins"

-

A talk
in two
parts.

-

Part 1

-

Part 1:
Minimising
developer
fatalities

-

It's a great
time to be
deploying perl
applications

-

local::lib

-

  eval $(perl -Mlocal::lib)

-

Sets up
env vars

-

  PERL5LIB
  PATH

-

  PERL5LIB
  PATH
  PERL_MM_OPT
  MODULEBUILDRC

-

  cpan Foo

-

  cpanp Foo

-

  perl Makefile.PL
    && make test
    && make install

-

Relocatable
except

-

  rm $LL_ROOT/.modulebuildrc

-

Then it
works in
/etc/skel

-

Module::Install

-

  requires 'Frobnicator'
    => 1.23;

-

  auto_install;

-

  make installdeps

-

  (eval $(perl -Mlocal::lib=$CWD/support)
    && perl Makefile.PL
    && make installdeps
    && make test
    && make install)

-

Vendor
packages?

-

cpan2dist

-

Debian
Ubuntu
Red Hat
Mandriva
Gentoo
Arch

-

Generic .deb
Generic .rpm

-

CPAN
Client

-

  2>&1 | less

-

  2>&1 | tee logfile | less

-

  script

-

WHY ARE
YOU SO
NOISY?!

-

Miyagawa

-

http://plackperl.org/
Plack - the perl
web server

-

PSGI
specification

-

WSGI
Rack

-

plackup

-

Standardisation
of deployment
is coming

-

... but I
digress

-

Miyagawa
took EVEN
MORE DRUGS

-

App::cpanminus

-

  $ wget cpanmin.us
  $ chmod 755 cpanm

-

  $ ./cpanm Foo::Bar
  Fetching http://.../Foo-Bar-1.23.tar.gz ... OK
  Configuring Foo-Bar-1.23 ... OK
  Building and testing Foo-Bar-1.23 for Foo::Bar ...OK
  Successfully installed Foo-Bar-1.23 (upgraded from 1.09)

-

The toolchain
hackers like
cpanm

-

The toolchain
hackers like
Miyagawa

-

Watch this
space

-

  # And run this occasionally:

  $ ./cpanm --self-upgrade

-

Converging
Platform

-

Task::Kensho

-

Enlightened
Perl
Organisation

-

Best practice
module set

-

Standard
platform

-

This year: binary
builds for every
OS we can

-

Meanwhile ...

-

debian-perl

-

Unpicking
the old

-

Updating
the new

-

Next stable
might be
stable

-

(and not in a
geological
sense :)

-

Win32 (!)

-

Strawberry
Perl

-

Strawberry
Perl Pro

-

Fedora

-

the community
have control

-

one day maybe
Red Hat perls
won't all suck

-

There's a great
future to
deploying perl
applications

-

-

Alarums and
excursions

-

The case for
compartmentalisation

-

Or: Why
I love
FastCGI

-

mod_perl

-

mod_perl
Maypole
Class::DBI

-

"all my form
fields are
type=text"

-

WHAT?

-

*digdigdig*

-

Standalone
script

-

.... works

-

WHAT?

-

*digdigdig*

-

DBD::mysql
... is ...

-

saying
everything
is a
varchar

-

WHAT?

-

*recompile*

-

... still
broken

-

*recompile*

-

libmysqlclient
is saying
everything is
a varchar

-

WHAAAAT?!?!

-

*breaks
out ldd*

-

... nothing

-

WHAAAAAAAAAAAATT?!?!?!

-

... I
upgraded
PHP a few
days ago

-

OH
GOD

-

It built against
its OWN copy of
libmysqlclient

-

... from
the 3.23 era

-

AAAAAAAAAAAAAAAAAAA

-

mod_$lang
IS EVIL

-

(now I run
PHP under
FastCGI
as well)

-

-

Part 2

-

Part 2:
Maximising
automation
banality

-

It's a great
time to be
developing perl

-

It's a great
time to be
SCRIPTING perl

-

Excitement

-

Stuff
excitement.

-

"Wouldn't it
be cool if"

-

Downtime
is exciting

-

All nighters
are exciting

-

My local
landlady
approves of
exciting

-

I ...
don't.

-

Simple.
Elegant.
Well
Understood.

-

  open my $fh, '<', $file
    or die "Couldn't open ${file}: $!";

-

  print $fh $string
    or die "..."

-

  close $fh
    or die "..."

-

Are we
seeing
a pattern
here yet?

-

autodie!

-

Klingon
Code

-

It is better to
die() than to
return() in failure.

-

autodie

-

  use autodie;
  open my $fh, '<', $file;

-

Error?

-

Error?
die()

-

  if ($@ and $@->isa(
    'autodie::exception'
  )) {

-

  if ($@->matches('open'))

-

  if ($@->matches(':io'))

-

system()

-

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

-

WHAT?

-

${^CHILD_ERROR_NATIVE}
and POSIX:: W*

-

WHAAAAT?

-

  use autodie qw(:all);

-

IPC::System::Simple

-

  system($cmd, @args);

-

  system([ 0, 1, 2 ], $cmd, @args);

-

perl5
v10.1

-

autodie
is core!

-

IO::All

-

io()

-

  my $contents
    < io($file);

-

  my $contents
    = io($file)->all;

-

  my @lines
    = io($file)->all;

-

  my @lines
    = @{io($file)};

-

  $new_contents
    > io($file);

-

  $append_this
    >> io($file);

-

  io($file)->print(
    $new_contents
  );

-

  io($file)->append(
    $append_this
  );

-

  io($dir)->all_files;

-

  io($dir)->filter(sub {
    $_->filename =~ /\.pm$/
  })->all_files;

-

  # fixed depth search
  io($dir)->all(3);

-

  # full depth search
  io($dir)->all(0);

-

  io('-'); # stdin/out
  io('='); # stdeer
  io('$'); # string

-

Networking

-

  perl -MIO::All -e
    'io(":8003")->print("O HAI\n");'

-

  perl -MIO::All -e 'for (
    io(":110")->fork->accept
  ) { print $_->getline }'

-

(gods but I
hate outlook
express ...)

-

IO::All::LWP

-

  my $page
    < io('http://www.google.com');

-

  my $page
    < io->http('www.google.com');

-

IO::File
IO::Dir
IO::Socket
IO::String

-

Tie::File
File::Spec
File::Path
File::ReadBackwards

-

DBM
MLDBM
LWP

-

IO::All
OF IT!

-

Deployment

-

Systems
scripts

-

Scripts should
not *need*
"deployment"

-

So ...

-

Well,
there's
PAR

-

PAR::Packer

-

Powerful

-

Powerful
Flexible

-

Powerful
Flexible
Exciting

-

... oh
dear ...

-

App::FatPacker

-

  $ fatpack trace script.pl
  $ cat fatpacker.trace

-

  App/Ack.pm
  File/Next.pm
  ...

-

  $ fatpack packlists-for ...

-

.packlist

-

CPAN install
writes this

-

all files
in the
distribution

-

  $ fatpack packlists-for
      `cat fatpacker.trace`

-

  ../File/Next/.packlist
  ../auto/ack/.packlist

-

  $ fatpack tree fatlib
    `cat packlists`

-

  fatlib/File/Next.pm
  fatlib/App/Ack.pm
  ...

-

  $ fatpack file <file list>

-

  $ fatpack file `cd fatlib; find`

-

  BEGIN {

-

  BEGIN {
    <code here>

-

  unshift @INC, sub {

-

  $ (fatpack file ...;
     cat script.pl)
    >script.pl.packed

-

  $ scp script.pl.packed
      server:script.pl

-

Presto!

-

More
interestingly

-

__END__

-

perl stops
reading

-

  ssh host perl
    <$(cat script.pl;
       echo "__END__";
       cat script-input)

-

Real zero
install

-

Caveats

-

No XS
code

-

PAR::Packer

-

... or just
rsync out a
local::lib

-

next cpanm
is fatpacked

-

expect more
to come

-

It's a great
time to be
scripting perl

-

Join us

-

Resistance
is futile

-

Resistors
are colour
coded

-

Thank you.

-

http://enlightenedperl.org/
http://lists.scsys.co.uk/
these slides will be on
http://shadowcat.co.uk/