Top Banner
GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS
22

GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Dec 29, 2015

Download

Documents

Kerry Benson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

GAP AnalysisHOW PERL KICKED MY ASS IN TEN MINUTES OR LESS

Page 2: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Introductory Remarks

# A function to demonstrate how terse Perl can be # Use it to impress your friends and family.

sub mysteryFunction() { print "#" x (length($_[0]) * 2),"\n$_[0]\n"."#" x

(length($_[0]) * 2),"\n" }

Page 3: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Why Perl? Perl frequent topic in Linux user groups.

Can be used with and to support shell scripting.

People who know Perl seem smart.

Page 4: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

What is Perl? Pathologically Eclectic Rubbish Lister / Practical Extraction and Reporting Language.

Created by Larry Wall in the mid 80s to circumvent a number of limitations (buffer limits, speed) of various shell tools (awk, sed).

Mid-Level language. Bridges the gap between low level languages (fast, hard) and high level languages (slow, easy).

Famously difficult to learn. Favors programmer over student, making learning the difficult but using it fast.

Uses default variables and tons of shorthand to reduce code length. An equivalent application written in C is 1/4 the length.

Optimized for working with text (XML, HTML, CSV, etc). Typical Perl program is 90% text processing and 10% everything else.

Page 5: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

What went wrong? Took wrong approach to learning Perl.

Thought it would be easier.

Approach: Learn basic syntax and then create examples.

Perl is used in ways which we haven't looked at (except in UNIX).

Complexity of the language and tendency for programs to use shorthand and modules.

Tried to bring OOP approach from start to learning the language.

Wasted time on projects above my skill level (magic square, spell checker, maze solver).

Lack of tutorials to bridge gap between basic syntax and medium sized programs.

Page 6: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Mystery Function Two # Demonstration of Perl's readability while (<>) {

chomp; print join("\t", (split /:/)[0, 2, 1, 5] ), "\n";

}

Page 7: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Revised Approach Spent most of my time going through basic syntax examples.

Perl documentation’s “Introduction to Perl”.

Tutorials Point “Introduction to Perl” syntax modules.

Read “Learning Perl” from O’Reilly chapters I-IV.

Create a simple program which contains packages which demonstrate the language’s basic syntax.

Learn how to make use of pre-built Perl modules.

Create a small OOP example.

Page 8: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

require Module; # demonstrations of modules require Functions; # demonstrations of functions require Variables; # demonstrations of variables require Scalars; # demonstrations of scalars require Hashes; # demonstrations of hashes require InputOutput; # demonstrations of IO require Arrays; # demonstrations of arraysrequire Loops; # demonstrations of loopsrequire Conditions; # demonstrations of conditions

# Demonstration of Variables Functions::mysteryFunction("Variables"); Variables::demonstrateInterpolation("String"); Variables::demonstrateStringMultiplication(); Variables::demonstrateVariableTypes();

# Demonstration of Scalars Functions::mysteryFunction("Scalars"); Scalars::demonstrateScalarOperations(); Scalars::demonstrateScalars();

# Demonstration of Modules Functions::mysteryFunction("Modules");Module::demonstrateModuleCall();

Syntax Helper Program

Page 9: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Syntax (We’ll come back to this if there is time)

If time remains in the presentation we can look in more detail at some of the basic syntax required to get up and running with Perl. I will make the project files available to anyone who is interested but the basic syntax portion roughly corresponds to the Tutorials Point code and the Perl Documentation’s introduction section.

Page 10: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Example from Syntax Helper (Moby Dick)

# replace each line foreach(@lines) {

$_ =~ s/Whale/Cat/g; $_ =~ s/WHALE/CAT/g; $_ =~ s/whale/cat/g; $_ =~ s/whales/cats/g; push(@newlines,$_);

}

"Sometimes the cat shakes its tremendous tail in the air, which, cracking like a whip, resounds to the distance of three or four miles." --SCORESBY.

Page 11: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Modules and CPAN Much of Perl is “one off” functions used to support other applications.

Modules are like library members in Java (ie, Java.Util).

Most Perl programming is done in modules.

CPAN: The Comprehensive Perl Archive Network

CPAN has thousands of Perl modules which can be brought in to your program.

CPAN modules can be managed from the command line in Linux through the “cpan” program which comes with most Linux distributions.

CPAN modules can also be built by hand but resolving dependencies is an agonizing task.

Page 12: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.
Page 13: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Small CPAN Project (Magic Square)

Page 14: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Using Magic Square Module #!/usr/bin/perl

use Math::MagicSquare; $A = Math::MagicSquare->new( [ 8, 1, 6 ], [ 3, 5, 7 ], [ 4, 9, 2 ]);

# display it to the console. $A->print("Magic Square A:\n");

# write it to HTML open( FH, '>', 'square.html' ) or die "cannot open file"; select FH; $A->printhtml();

# write it to an image my $filename = 'square.png'; open( FH, '>', 'square.png' ) or die "cannot open file"; select FH; print $A->printimage(); close FH; # in the end

Page 15: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Magic Square Output

HTML PNG

Page 16: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

OOP with Perl Goal: To produce three basic shapes (circle, rectangle and triangle) as objects in Perl.

Goal: Objects will all inherit from shape and implement an abstract “draw” and “area” method.

Goal: All shapes will be able to be rendered to a PNG image using the GD::Simple module and a polymorphic routine (shown in driver).

Note: Driver, Shape, Circle, Rectangle, Triangle are all included with this project. Time permitting I will walkthrough the implementation of “Shape” and a shape subclass and the draw method.

Page 17: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Drawing Shapes with GD::Simple Drawing shapes with GD::Simple is akin to working with the AWT for Java with ellipses, rectangles and paths.

# Circle Draw Method sub draw() {

my ($self) = @_; my $size = $self->{radius}; my $img = GD::Simple->new($size * 4, $size * 4); #set the canvas $img->bgcolor('purple'); # set the colour $img->move($size*2, $size * 2); # position the brush$img->ellipse($size, $size); open my $out, '>', 'circle.png' or die; binmode $out; print $out $img->png;

}

Page 18: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

GD::Simple Shape Output

Page 19: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Unit Testing in Perl Unit testing is available directly from within the Perl library through the “Test” package.

The test package allows you to run a suite of tests and to generate a report on the test.

Testing at the lowest level is more script based.

Testing is primary accomplished through assertions as we have seen in C#.

In a more advanced scenario a suite of tests would be conducted within a testing module and the result would be returned to the assertion statement.

Perl has support for more advanced types of testing (timings, etc) available through the Test::More package.

Page 20: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Driver / Tests of OOP Project BEGIN { plan tests => 8}#todo => [3,4] }

# Run a series of tests on the rectangle object we've created # Test the getters / setters for demo purposes print "Testing Rectangle Getters/Setters\n"; my $rectangle = $shapes[0]; ok($rectangle->getWidth(), $width); ok($rectangle->getHeight(), $height); ok($rectangle->getY(), $y_pos); ok($rectangle->getX(), $x_pos);

# Test the Rectangle's Get Area Method print "Testing Rectangle's Area Method:\n";my $area = $shapes[0]->getArea(); ok($area, $width * $height);

Page 21: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Test Output (Pass)

Page 22: GAP Analysis HOW PERL KICKED MY ASS IN TEN MINUTES OR LESS.

Test Output (Fail)