aka let's write some fucking perl by william orrworr/seminars/programming/intro to... · 2013....

38
Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Upload: others

Post on 31-Mar-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Introductory PerlAKA LET'S WRITE SOME FUCKING PERLby William Orr

Page 2: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

What do I use Perl for?

• Scripting

• Applications

• Web programming

• Pretty much everything you could want

Page 3: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Why?

• Perl has a very, very large set of available libraries through CPAN

• TIMTOWTDI

• Language is very flexible - many modules on CPAN influence behavior of language

Page 4: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

How to get Perl - Windows

• dwimperl.com

• Comes with Padre (a Perl "IDE") as well as tons of useful Perl modules, and an old-ish version Strawberry Perl

Page 5: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

How to get Perl - OSX and Linux

You already have it.

Page 6: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Multiple Perl Installs (you want this)

# cpancpan> install App::perlbrew$ perlbrew init$ perlbrew install perl-5.16.2$ perlbrew switch perl-5.16.2Install other listed modules

Page 7: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

How to make Perl more useful

Install some modules

• App::perlbrew

• App::cpanminus

• Const::Fast

• Try::Tiny

• Devel::REPL

Page 8: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Documentation - perldoc

$ perldoc -f <functionname>$ perldoc <modulename>$ man perl

Page 9: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Documentation - MetaCPAN

• All the perldoc documentation online and searchable

• Includes 3rd party docs, links to the bug tracker, automated testing platforms, authors website, etc.

Page 10: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's write some fucking code

use strict;

use warnings;

use v5.10;

say "WUT UP WRRRRLD";

print "WUT UP WRRRRLD\n";

Page 11: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's write some fucking code

Things to notice

• use strict

• use warnings

• use v5.10

• say

Page 12: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's use some fucking variables

my $first_variable = "This is a scalar";

my $second_variable = 5;

say $first_variable;

say("This is also a scalar: $second_variable");

my $third_variable = 4.5;

say "Guess what else is a fucking scalar? $third_variable";

my $fourth_variable = qr| \d{1,2} / \d{1,2} / \d{2,4} |x;

say "Also a scalar: $fourth_variable";

Page 13: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's use some fucking variables

Things to notice here

• Scalars!

• Interpolation

• Parentheses are optional

Page 14: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Interpolation

• Variables resolve to their value in strings quoted with "", `` and most other quote-like operators

Page 15: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

I want a lot of scalars!

my @lets_talk_about_arrays = ( "an", "array", "is", "a", "set", "of", "scalars", "only");

say "@lets_talk_about_arrays";

# We can index into them!

say $lets_talk_about_arrays[1];

# We can do ranges!

say @lets_talk_about_arrays[6..7];

# We can call functions on them!

say join " ", map { uc } @lets_talk_about_arrays[6..7];

say(join(" ", map({ uc } @lets_talk_about_arrays[6..7])));

Page 16: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

I want a lot of scalars!

Things to notice here:

• Array declaration & initialization

• Ranges with ..

Page 17: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

How do we get array length?

my $array_size = @lets_talk_about_arrays;

say $array_size;

Page 18: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Context

• Functions and operators impose context on the variables you use

• Context determines result of expression

• Three kinds of contexto scalaro listo void

Page 19: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Context

# Scalar context

my $now = localtime;

say $now;

# List context

my @now = localtime;

say "(" . join(", ", @now) . ")";

Page 20: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Context

To review:

• Operators and functions impose context

• Values of expression depend on context

• Arrays used in scalar context return size

• Can force scalar context with scalar()

• Can force list context with ()=

• Documentation!

Page 21: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Dictionar-err, hashes!

my %hash = ( key => "value", other_key => 3, "new key", 5.2 );

say %hash;

say "$hash{key}";

say "$hash{other_key}";

say "$hash{new_key}";

# Scalar context?

say scalar %hash;

# WOAH

my %empty_hash;

say scalar %empty_hash;

Page 22: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's talk about operators

• They're all pretty much the same as you're used to!

• Except for the new ones!

Page 23: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

New operators

• . x

• <=>

• cmp, lt, gt, lte, gte, eq, ne

• //

• =~, !~

• or, and, not, xor

• ...

open my $fh, ">", "filename" or die $@;

Page 24: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Control flow - loops

my @some_array = 1..10;

foreach my $iter (@some_array) {

say $iter;

}

my $done = 0;

while (not $done) {

$done = int rand 2;

say $done;

}

Page 25: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Control flow - if

if ($some_var ne "done") {

} elsif ($other_var == 5) {

} else {

}

Page 26: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Control flow - switch

given ($foo) {

when (1) {

}

when (2) {

}

when ("45hundred") {

}

default {

}

}

Page 27: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Special operator - ~~

• Smart match operator

• Returns true based on various, sensible conditions

• Full table in perlsyn - /Smart matching in detail

Page 28: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

FUCKIT LET'S WRITE SOME FUNCTIONS

sub my_func {

my ($first, $second, $third) = @_;

return $first + $second + $third;

}

my_func 1, 2, 3

# Array is flattened - will return 6

my @arr = (1, 2, 3, 4, 5, 6);

# NEVER &my_func

Page 29: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

SHIT HOW DO I NOT FLATTEN ARRAYS IN FUNCTION CALLS?!

• or also "HOW THE HELL CAN I HAVE COMPOSITE DATA STRUCTURES?!"

Page 30: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's talk about references

• Can pass around references to _anything_o this includes functions

• A reference is a scalar - can pop arrays into arrays

• When you modify the value of a reference, it modifies the original variable

Page 31: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's talk about references

my @arr = ( 1 .. 10 );

# This won't compile

#my @two_d_arr = ( @arr, @arr );

my @two_d_arr = ( \@arr, \@arr );

# We'll see some reference addresses

say "@two_d_arr";

# One reference address

say $two_d_arr[0];

# Dereferencing

say "@{$two_d_arr[0]}";

Page 32: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's talk about references

# Hashes?

# YOU'RE DAMN RIGHT

my %hash;

$hash{first} = \@arr;

say $hash{first}->[0];

# OR

say ${hash{first}}->[0];

# Lets get fancy

$hash{second} = \%hash;

say $hash{second}->{first}->[0];

Page 33: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's talk about references

sub print_shit {

say "shit";

}

$hash{third} = \&print_shit;

$hash{third}->();

Page 34: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Anonymous references

my $arr_ref = [ 1, 2, 3, 4 ];

say @$arr_ref;

say scalar @$arr_ref;

say $arr_ref->[1]

my $hash_ref = { key => 1, other_key => 5 };

say %$hash_ref;

say $hash_ref->{key};

my $sub_ref = sub { say "foo"; }

$sub_ref->();

Page 35: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Anonymous references

• anonymous array references declared with []

• anon hash refs declared with {}

• anon sub refs declared with sub { }

Page 36: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

File I/O

open(my $fh, "<", "testfile") or die "Could not open file for reading";

foreach my $line (<$fh>) {

print $line;

}

close($fh);

open($fh, ">>", "testfile") or die "Could not open file for writing";

say $fh "let's append a message";

close($fh);

Page 37: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

Let's run some commands

open(my $cmd, "-|", "ls") or die "Could not run ls";

foreach my $line (<$cmd>) {

print $line;

}

close($cmd);

open($cmd, "|-", "cat") or die "Could not run cat";

say $cmd "cat'd output";

close($cmd);

my $output = qx/ls/; # `` also works

foreach my $line (split /\n/, $output) {

say $line;

}

Page 38: AKA LET'S WRITE SOME FUCKING PERL by William Orrworr/seminars/Programming/Intro to... · 2013. 5. 12. · Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr

In conclusion

This is some basic Perl, and is not even close to conclusive