achieving the impossible with perl

22
Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence. 1 Perl Perl A Beginner's Guide to A Beginner's Guide to Achieving The Impossible. Achieving The Impossible. Adam John Trickett Adam John Trickett www.iredale.net [email protected] PGP Key: 0x166C4BF0

Upload: adam-trickett

Post on 18-May-2015

1.066 views

Category:

Technology


0 download

DESCRIPTION

How to write Perl programs the easy way - use someone else's code via CPAN.

TRANSCRIPT

Page 1: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

1

PerlPerlA Beginner's Guide toA Beginner's Guide to

Achieving The Impossible.Achieving The Impossible.

Adam John TrickettAdam John Trickett

[email protected]

PGP Key: 0x166C4BF0

Page 2: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

2

Or...Or...

● Standing on the Shoulders of Giants● Laziness is a Virtue in Perl● Leveraging the power of CPAN

Page 3: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

3

An Example...An Example...

● Write a command line script to show the current running times of trains between two stations in the UK.

Page 4: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

4

Here Is One IHere Is One IPrepared EarlierPrepared Earlier

Page 5: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

5

CPAN Is Your Friend...CPAN Is Your Friend...

● Comprehensive Perl Archive Network● Globally distributed over 253 mirrors● On-line since 26 October 1995● 4247 MiB of data (more in archive)● 6303 Authors● 12652 Modules

● It's all FREE!

Page 6: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

6

Web InterfaceWeb Interface

● There are two front end web interfaces:● search.cpan.org● kobesearch.cpan.org

● An upload service for authors:● pause.perl.org

● Testing, quality, bug tracking and more:● cpantesters.perl.org● rt.cpan.org

Page 7: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

7

Page 8: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

8

Page 9: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

9

Page 10: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

10

Page 11: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

11

Page 12: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

12

Page 13: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

13

Page 14: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

14

Command LineCommand Line

● CPAN comes with it's own installation tool suite called cpan● Search for modules● Download modules● Install modules● Resolve dependencies● Update modules

Page 15: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

15

Page 16: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

16

Find your moduleFind your module

● Search using the web interface:● Read the documentation● Look at errors and problems● Check the rating● Check how fresh it is

● Download the tarball over the web, or● Install with CPAN if you can – it automatically

resolves dependencies● Carefully study the manpage using perldoc

Page 17: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

17

Page 18: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

18

Page 19: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

19

Page 20: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

20

Page 21: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

21

The CodeThe Code

#!/usr/bin/perl

use strict;use WWW::LiveDepartureBoards;

my $basing = WWW::LiveDepartureBoards->new({station_code => 'BSK'});my $overton = WWW::LiveDepartureBoards->new({station_code => 'OVR'});my $time = localtime();

print "\nTime Now: $time\n\n";

my @details = $basing->destination('ovr');print "Starting at Basingstoke, going to Overton\n";print_details(\@details, 'OVR');

print "\n";

Page 22: Achieving the Impossible with Perl

Version 1.0.0 © Adam Trickett August 2007 Distributed under a creative commons Attribution-NonCommercial-ShareAlike licence.

22

The Code, cont'The Code, cont'

my @details = $overton->destination('bsk');print "Starting at Overton, going to Basingstoke\n";print_details(\@details, 'BSK');

sub print_details { my $details = shift; my $final = shift;

print "Time\tStatus\t\tFinal Destination\n";

foreach my $detail (@$details) { print $detail->{time}, "\t", $detail->{status}, " \t", $detail->{$final}, "\n"; }}