DC-Baltimore Perl Workshop 2013
Structures, Context, Functions and Flow
Hello, Perl
~ Joshua Eric Turcotte
please don't spook the speaker!
(...he's never done this before...)
For seriously, though...
There may be a few topics:
Before we start...
Deimos,
because I forgot how to funny
A show of hands!
How many here have some working knowledge of perl?
Well, crap...
=head2
dangit!
=cut��sub where_to {
my $slides = shift;
my %crowd = @_;
return knowledge(\%crowd) <= $me->{$comfort_level} ?
shift @$slides : skip_a_bunch_of(@$slides);
}
too soon?
I spent way too much time on that slide...
... and Deimos was harassing me�the entire time. (go chase a rubber band!)
NO DON'T!
Save yourself a little pain...
These things will help you learn
... or, possibly, never learn how to debug!
Are we there yet?
$gotta->start(%somewhere);
First up; Sigils...
$%@&!
No, really... $%@&�
say "$me, $myself, and $I";
%They are a @bunch of $people;
Among these %people, who am $I;
why can't I find my $line[$place];
meet the %band;
Calling attendance!
Is it a %Bag or a %Book?
Am I at the back of the @line?
In and Out of the @line;
First in or Last in?
Take a $number and &wait(@line);
You can even do a @grid!
*In exchange for your very soul...
As stolen from the YAPC'11 talk...
# that ->, btw, is reference operator (later...)
Okay, but what do we do with this?
Characteristics of Operators
And then there's Fixity...
Numerical Operators
Numerical Comparation
String Operators
Logical Operators
Need LESS input!
*yes, johnny 5 runs on perl 5.10+
*
You want me to do what, now?
All the while, until otherwise...
Do this at least once...
For Tom, for Dick, and for Harry;
Not while you live under my roof!
Let's count�some rocks!
my $rocks = 0;
foreach my $x(0..$maxx){
foreach my $y(0..$maxy){
$rocks++ if $grid[$x][$y] eq 'rock';
}
}
say "$rocks rocks found!";
So, about that whole reference thing
%hash = { array => (1, 2, 3) };�# the inner scalar 'array' contains a reference to # anonymous array in memory�# => allows unquoted key names, btw
This is why we like Data::Dumper;
my $hashref = { # note the $� name => 'josh',� age => $obfuscated_age,� pet_plants => [� { name => 'president', type => 'sago' },� { name => 'thing', type => 'cactus' }
]
};��say $hashref; # HASH(0x30ec2c0)
To whom are you referring?
Are you referring to $$me?
By why even bother with refs?
my $pets = \%hash_of_cat_and_plants;�my $condo_pets = { josh => $pets, kath => $kathpets };�my $vetqueue = [ $kathpets, $pets, $billpets ];
Let's do a little routine;
Self documenting routines...
�sub obfuscate_age {� my $me = shift; # @_ can be implied silently� my $age = shift || 35; # you can 'or' a default
$me->{age} = (int(rand(5)) - 2) + $age;
}�
Wait a minute... what's going on?
sub twoarrays {
my @a1 = (1, 2, 3);� my @a2 = (4, 5, 6);� return (@a1, @a2);
}
my (@one, @two) = twoarrays();�say join(", ",@one); # 1, 2, 3, 4, 5, 6
say join(", ",@two); # ... huh ...
Oh wait... REFERENCES!
For a little bit of context;
Umm... if you really want me to;
sub a_to_e {
$people = shift;
my @changed;
foreach my $p (keys %$people){
if ($p =~ /a/){
$people->{$p} =~ s/a/e/g;
push @changed, $people->{$p};
}
}
...
... just a little bit more;
if (wantarray){ # list context!
return @changed; # give us all changed!
}
elsif (defined wantarray){ # scalar context!
return scalar @changed; # return a count
} # ^ that's also a forced context!
else { # void context#
... # we're already done!
}
}
Forcing contexts...
Finally, Coercion; (get off the stage!)
References... no, no... SOURCES
joshua.eric.turcotte@gmail.com
@je_turcotte
How'd I do?
I'm sorry... you were talking
about something?