Download presentation
Presentation is loading. Please wait.
Published byHamdani Jayadi Modified over 6 years ago
1
AKA LET'S WRITE SOME FUCKING PERL by William Orr
Introductory Perl AKA LET'S WRITE SOME FUCKING PERL by William Orr
2
What do I use Perl for? Scripting Applications Web programming
Pretty much everything you could want
3
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
4
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
5
How to get Perl - OSX and Linux
You already have it.
6
Multiple Perl Installs (you want this)
# cpan cpan> install App::perlbrew $ perlbrew init $ perlbrew install perl $ perlbrew switch perl Install other listed modules
7
How to make Perl more useful
Install some modules App::perlbrew App::cpanminus Const::Fast Try::Tiny Devel::REPL Const::Fast gives you interpolable constants Try::Tiny gives you nice exception syntax Devel::REPL gives you a fantastic REPL
8
Documentation - perldoc
$ perldoc -f <functionname> $ perldoc <modulename> $ man perl
9
Documentation - MetaCPAN
All the perldoc documentation online and searchable Includes 3rd party docs, links to the bug tracker, automated testing platforms, authors website, etc.
10
Let's write some fucking code
use strict; use warnings; use v5.10; say "WUT UP WRRRRLD"; print "WUT UP WRRRRLD\n";
11
Let's write some fucking code
Things to notice use strict use warnings use v5.10 say use strict prevents you from doing things you probably don't want - like using variables without declaring them use warnings turns on additional warnings when you're about to do something stupid use v5.10 allows you to specify what the lowest version of perl you want to use is, as well as turning on additional features say is print with benefits (the only benefits are the automatic newline)
12
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";
13
Let's use some fucking variables
Things to notice here Scalars! Interpolation Parentheses are optional Scalars are the first kind of variables we have in Perl. They represent anything with a singular value. These can be numbers, strings, regexes, file handles, etc. Interpolation is simple - if you use a variable in a string, it is interpolated into the value of that variable
14
Interpolation Variables resolve to their value in strings quoted with "", `` and most other quote-like operators
15
I want a lot of scalars! = ( "an", "array", "is", "a", "set", "of", "scalars", "only"); say # We can index into them! say $lets_talk_about_arrays[1]; # We can do ranges! # We can call functions on them! say join " ", map { uc say(join(" ", map({ uc
16
I want a lot of scalars! Things to notice here:
Array declaration & initialization Ranges with ..
17
How do we get array length?
my $array_size say $array_size;
18
Context Functions and operators impose context on the variables you use Context determines result of expression Three kinds of context scalar list void
19
Context # Scalar context my $now = localtime; say $now; # List context
say "(" . join(", . ")";
20
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!
21
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;
22
Let's talk about operators
They're all pretty much the same as you're used to! Except for the new ones!
23
New operators . x <=> cmp, lt, gt, lte, gte, eq, ne // =~, !~
or, and, not, xor ... open my $fh, ">", "filename" or die All the useful operators, and one useless one (yadda yadda yadda)
24
Control flow - loops my @some_array = 1..10;
foreach my $iter { say $iter; } my $done = 0; while (not $done) { $done = int rand 2; say $done; Loops are the same. Talk about foreach assigning to the default variable - discourage it. Talk about looping over dictionaries
25
Control flow - if if ($some_var ne "done") {
} elsif ($other_var == 5) { } else { } It's the same! Talk about truthiness 0 is false "" is false undef is false empty lists and dictionaries kind of return false
26
Control flow - switch given ($foo) { when (1) { } when (2) {
when ("45hundred") { default { Uses smart match operator No fall through when clause can contain a function call too Perl gives you this feature
27
Special operator - ~~ Smart match operator
Returns true based on various, sensible conditions Full table in perlsyn - /Smart matching in detail $a $b Type of Match Implied Matching Code ====== ===== ===================== ============= Any undef undefined !defined $a Any Object invokes ~~ overloading on $object, or dies Hash CodeRef sub truth for each key[1] !grep { !$b->($_) } keys %$a Array CodeRef sub truth for each elt[1] !grep { !$b->($_) Any CodeRef scalar sub truth $b->($a) Hash Hash hash keys identical (every key is found in both hashes) Array Hash hash keys intersection grep { exists $b->{$_} Regex Hash hash key grep grep /$a/, keys %$b undef Hash always false (undef can't be a key) Any Hash hash entry existence exists $b->{$a} Hash Array hash keys intersection grep { exists $a->{$_} Array Array arrays are comparable[2] Regex Array array grep grep undef Array array contains undef grep Any Array match against an array element[3] grep $a ~~ Hash Regex hash key grep grep /$b/, keys %$a Array Regex array grep grep Any Regex pattern match $a =~ /$b/ Object Any invokes ~~ overloading on $object, or falls back: Any Num numeric equality $a == $b Num numish[4] numeric equality $a == $b undef Any undefined !defined($b) Any Any string equality $a eq $b
28
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 = (1, 2, 3, 4, 5, 6); # NEVER &my_func Arguments are put in array All arrays and hashes are flattened
29
SHIT HOW DO I NOT FLATTEN ARRAYS IN FUNCTION CALLS?!
or also "HOW THE HELL CAN I HAVE COMPOSITE DATA STRUCTURES?!"
30
Let's talk about references
Can pass around references to _anything_ 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 Make a fucking diagram
31
Let's talk about references
= ( ); # This won't compile ); = ( ); # We'll see some reference addresses say # One reference address say $two_d_arr[0]; # Dereferencing say
32
Let's talk about references
# Hashes? # YOU'RE DAMN RIGHT my %hash; $hash{first} = say $hash{first}->[0]; # OR say ${hash{first}}->[0]; # Lets get fancy $hash{second} = \%hash; say $hash{second}->{first}->[0]; While this all looks messy, most of the time arrow syntax or simple dereference syntax (bare sigil before variable) is all that's needed
33
Let's talk about references
sub print_shit { say "shit"; } $hash{third} = \&print_shit; $hash{third}->(); This is the only time you should ever use the function sigil
34
Anonymous references my $arr_ref = [ 1, 2, 3, 4 ]; say @$arr_ref;
say 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->();
35
Anonymous references anonymous array references declared with []
anon hash refs declared with {} anon sub refs declared with sub { }
36
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"; Talk about 2 argument open and why it's bad Talk about alternate open modes
37
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"; my $output = qx/ls/; # `` also works foreach my $line (split /\n/, $output) { say $line;
38
In conclusion This is some basic Perl, and is not even close to conclusive
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.