Good Evils In Perl
- 2. $speaker.meta
• • http://handlino.com/
Kang-min Liu
gugod
• http://gugod.org
• http://twitter.com/gugod
• gugod@gugod.org
- 9. pragma = one small
english word.
Module = title-cased
just an convention.
Module::Acme
pragma
- 11. Can anyone tell me if
there’s any problem in
this small program ?
foo.pl
#!/usr/bin/perl
print $foo;
print quot;Helloquot;;
- 13. Can any one see a
problem in this program ?
#!/usr/bin/perl
use warnings;
print $name;
print quot;Helloquot;;
- 24. given - when - default
given ($foo) {
when (1) { say quot;$foo == 1quot; }
when ([2,3]) {
say quot;$foo == 2 || $foo == 3quot;
}
when (/^a[bc]d$/) {
say quot;$foo eq 'abd' || $foo eq 'acd'quot;
}
when ($_ > 100) { say quot;$foo > 100quot; }
default { say quot;None of the abovequot; }
}
- 32. Q: How to test if an
array contains a specific
value ?
- 44. Can anyone see what it
does now ?
Can anyone write a
nested loop version in 10
seconds ?
if ( any(@a) == any(@b) ) {
...
}
- 47. my $range = 10−>to(1);
# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
- 50. sub foo {
eval {
# some code that might die
return quot;return value from fooquot;;
}
if ($@) {
...
}
}
- 51. sub foo {
try {
# some code that might die
return quot;return value from fooquot;;
}
catch (Some::Error $e where { $_->code > 100 } ) {
...
}
}
- 53. sub name { quot;gugodquot; }
alias get_name => 'name';
alias getName => 'name';
- 62. Perl (5) is not like other Object Oriented Languages... does
NOT have an OO built-in
That's why you should learn perl if you want to learn OO!
You can learn how to make an object system, not just how
to use it.
Dan Kogai
- 63. package Point;
use Moose;
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'Int');
sub clear {
my $self = shift;
$self->x(0);
$self->y(0);
}
- 65. class BankAccunt {
has 'balance' => (
isa => 'Num', is => 'rw', default => 0
);
method deposit (Num $amount) {
$self->balance( $self−>balance + $amount );
}
method withdraw (Num $amount) {
my $current_balance = $self−>balance();
( $current_balance >= $amount )
|| confess quot;Account overdrawnquot;;
$self->balance( $current_balance − $amount );
}
}
- 70. sub doMyWork {
my ($arr1, $arr2) = @_;
my @arr1 = @$arr1;
my @arr2 = @$arr2;
...
}
doMyWork(@foo, @bar);
- 71. sub doMyWork(@@) {
my ($arr1, $arr2) = @_;
my @arr1 = @$arr1;
my @arr2 = @$arr2;
...
}
doMyWork(@foo, @bar);
- 76. sub AUTOLOAD {
my $program = $AUTOLOAD;
$program =~ s/.*:://;
system($program, @_);
}
date();
who('am', 'i');
ls('−l');
- 82. # from self.pm
sub _args {
my $level = 2;
my @c = ();
package DB;
@c = caller($level++)
while !defined($c[3]) || $c[3] eq '(eval)';
return @DB::args;
}
- 84. sub inc_x {
my $h = peek_my(1);
${ $h->{'$x'} }++;
}
- 89. How it works
• you define “declarator” keywords
• it let compiler stop at the keywords
• your code parse the current line in your
way, maybe re-write it
• you re-place current line with the new
version
• it resumes the compiler on the current line
- 107. Perl is like the Force. It
has a light side, a dark
side, and it holds the
universe together.
Larry Wall