SlideShare a Scribd company logo
Writing Maintainable Perl
David M. Bradford
Who am I?
David Bradford
@tinypig
tinypig.com
Who am I?
● Web Engineer at OmniTI
– Full stack tech consulting
– Remote database management and consulting
– Large Scale / Mission Critical
– We're Hiring!
– omniti.com
Why This Talk?
● Is Perl a "write-only" language?
Why This Talk?
● Is Perl a "write-only" language? No!
Why This Talk?
● Is Perl a "write-only" language? No!
● ...but it can be.
Why This Talk?
● Is Perl a "write-only" language? No!
● ...but it can be.
● It's our responsibility to keep code:
Why This Talk?
● Is Perl a "write-only" language? No!
● ...but it can be.
● It's our responsibility to keep code:
– Easy to read
Why This Talk?
● Is Perl a "write-only" language? No!
● ...but it can be.
● It's our responsibility to keep code:
– Easy to read, so it will be:
Why This Talk?
● Is Perl a "write-only" language? No!
● ...but it can be.
● It's our responsibility to keep code:
– Easy to read, so it will be:
– Easy to maintain
My Function, “listify”
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
Purpose of listify
Take a list, for example:
List # 1, with 10 items
one two three four five six seven eight nine ten
Give me several lists back, each containing no more than N items,
for example, 4:
List #1 with 4 items: one two three four
List #2 with 4 items: five six seven eight
List #3 with 2 items: nine ten
Prepare to call listify
listify( @list, 4 );
Result:
@array = (
[ 'one', 'two', 'three', 'four' ],
[ 'five', 'six', 'seven', 'eight' ],
[ 'nine', 'ten' ],
);
my @list = qw( one two three four five six seven eight nine ten );
Call to listify
my @list = qw( one two three four five six seven eight nine ten );
Result:
@array = (
[ 'one', 'two', 'three', 'four' ],
[ 'five', 'six', 'seven', 'eight' ],
[ 'nine', 'ten' ],
);
listify( @list, 4 );
Result of listify
my @list = qw( one two three four five six seven eight nine ten );
listify( @list, 4 );
Result:
@array = (
[ 'one', 'two', 'three', 'four' ],
[ 'five', 'six', 'seven', 'eight' ],
[ 'nine', 'ten' ],
);
Declare function listify
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
sub listify {
}
Get Parameters
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
my ($aref,$cc) = @_;
Validate Parameters
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
}
return;
Declare Return Variable
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
my $j;
Loop Through Input Array
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
for(my $i=0; $i<=$#$aref; $i+=$cc) {
}
Add List to Output Variable
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
push @$j, [@$aref[$i..$i+$cc-1]];
???
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
Update Input Array In Place
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
@$aref = @$j;
Return 1 to Indicate Success
sub listify {
my ($aref,$cc) = @_;
if( ref $aref eq 'ARRAY' && $cc > 0 ) {
my $j;
for(my $i=0; $i<=$#$aref; $i+=$cc) {
push @$j, [@$aref[$i..$i+$cc-1]];
}
$#{$j->[$#{$j}]}=$#$aref%$cc;
@$aref = @$j;
return 1;
}
return;
}
return 1;
Clever (but not easy to read) Line
$#{$j->[$#{$j}]}=$#$aref%$cc;
Purpose of Clever Line
The purpose of the line is to truncate the final array to the number of remaining
elements so we don't end up with this:
@array = (
[ 'one', 'two', 'three', 'four' ],
[ 'five', 'six', 'seven', 'eight' ],
[ 'nine', 'ten', undef, undef ],
);
Improvement #1: Whitespace
Improvement #2: Refactor
$#{ $j->[ -1 ] } = $#$aref % $cc;
$#{ $j->[ $#{$j} ] } = $#$aref % $cc;
Improvement #3: Naming
$#{ $result_array[ -1 ] } = $#$in_aref % $elements_per_array;
Improvement #4: Multiple Lines
$final_aref = $result_array[ -1 ];
$#$final_aref = $#$in_aref % $elements_per_array;
Improvement #5: Split Another Line
my $final_aref = $result_array[ -1 ];
my $elements_in_final = $#$in_aref % $elements_per_array;
$#$final_aref = $elements_in_final;
Improvement #6: More Whitespace
my $final_aref = $result_array[ -1 ];
my $elements_in_final = $#$in_aref % $elements_per_array;
$#$final_aref = $elements_in_final;
Improvement #7: A comment!
my $final_aref = $result_array[ -1 ];
my $elements_in_final = $#$in_aref % $elements_per_array;
# Truncate final array
$#$final_aref = $elements_in_final;
Easier to Read Now?
Original:
$#{$j->[$#{$j}]}=$#$aref%$cc;
Revised:
my $final_aref = $result_array[ -1 ];
my $elements_in_final = $#$in_aref % $elements_per_array;
# Truncate final array
$#$final_aref = $elements_in_final;
Apply Principles to Entire Function
sub listify {
my ( $in_aref, $elements_per_array ) = @_;
return if (
ref $in_aref ne 'ARRAY' or
$elements_per_array <= 0
);
my @result_array;
for( my $i = 0; $i <= $#$in_aref; $i += $elements_per_array ) {
push @result_array, [
@$in_aref[ $i..$i + $elements_per_array - 1 ]
];
}
# Continued on next slide
Apply Principles to Entire Function
# Continued from previous slide
my $final_aref = $result_array[ -1 ];
my $elements_in_final = $#$in_aref % $elements_per_array;
# Truncate final array
$#$final_aref = $elements_in_final;
@$in_aref = @result_array;
}
Helpful Modules
Perl::Critic
based on the book “Perl Best Practices” by Damian
Conway
Perl::Tidy
increase readability of code
perlcritic Result
$ perlcritic -1 listify.pl
Code not contained in explicit package at line 1, column 1. Violates
encapsulation. (Severity: 4)
Code before strictures are enabled at line 1, column 1. See page
429 of PBP. (Severity: 5)
Subroutine "listify" does not end with "return" at line 1, column 1.
See page 197 of PBP. (Severity: 4)
Code before warnings are enabled at line 1, column 1. See page 431 of
PBP. (Severity: 4)
No package-scoped "$VERSION" variable found at line 1, column 1. See
page 404 of PBP. (Severity: 2)
C-style "for" loop used at line 10, column 7. See page 100 of PBP.
(Severity: 2)
Double-sigil dereference at line 10, column 26. See page 228 of PBP.
(Severity: 2)
(more double-sigil warnings follow)
Critical Fixes
Code not contained in explicit package at line 1, column 1. Violates
encapsulation. (Severity: 4)
Code before strictures are enabled at line 1, column 1. See page 429
of PBP. (Severity: 5)
Code before warnings are enabled at line 1, column 1. See page 431
of PBP. (Severity: 4)
sub listify {
my ( $in_aref, $elements_per_array ) = @_;
# --- cut ---
#!/usr/bin/perl
use strict;
use warnings;
Explicit Return
Subroutine "listify" does not end with "return" at line 2,
column 1. See page 197 of PBP. (Severity: 4)
# Truncate final array
$#$final_aref = $elements_in_final;
@$in_aref = @result_array;
}
return;
$VERSION
No package-scoped "$VERSION" variable found at line 1,
column 1. See page 404 of PBP. (Severity: 2)
#!/usr/bin/perl
use strict;
use warnings;
sub listify {
my ( $in_aref, $elements_per_array ) = @_;
# --- cut ---
our $VERSION = '1.19';
C-style "for" Loop
C-style "for" loop used at line 11, column 7. See page 100
of PBP. (Severity: 2)
C-style "for" Loop
C-style "for" loop used at line 11, column 7. See page 100
of PBP. (Severity: 2)
● “What?” No C-style “for” loops
C-style "for" Loop
C-style "for" loop used at line 11, column 7. See page 100
of PBP. (Severity: 2)
● “What?” No C-style “for” loops
● “Why?” More difficult to read and maintain
C-style "for" Loop
C-style "for" loop used at line 11, column 7. See page 100
of PBP. (Severity: 2)
● “What?” No C-style “for” loops
● “Why?” More difficult to read and maintain
● “I disagree.” That's OK. Modify Perl::Critic settings!
C-style "for" Loop
C-style "for" loop used at line 11, column 7. See page 100
of PBP. (Severity: 2)
● “What?” No C-style “for” loops
● “Why?” More difficult to read and maintain
● “I disagree.” That's OK. Modify Perl::Critic settings!
But today, I'm going to fix all the defaults
Change to “while” loop
C-style "for" loop used at line 11, column 7. See page 100 of
PBP. (Severity: 2)
Old:
for( my $i = 0; $i <= $#$in_aref; $i += $elements_per_array ) {
# (loop contents)
}
New:
my $i = 0;
while($i <= $#$in_aref) {
# (loop contents)
$i += $elements_per_array;
}
Double-sigil dereference
● Caused by
– @$my_array_reference
● Fixed with
– @{ $my_array_reference }
JAPH: The Do-Not Example
sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){
s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95?
&qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "},
$q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].)
JAPH: The Do-Not Example
sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){
s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95?
&qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "},
$q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].)
● Ugly variables $" $; $ " and even $_, @_
JAPH: The Do-Not Example
sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){
s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95?
&qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "},
$q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].)
● Ugly variables $" $; $ " and even $_, @_
– use English;
JAPH: The Do-Not Example
sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){
s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95?
&qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "},
$q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].)
● Ugly variables $" $; $ " and even $_, @_
– use English;
● 74% non-alpha/space
JAPH: The Do-Not Example
sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){
s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95?
&qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "},
$q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].)
● Ugly variables $" $; $ " and even $_, @_
– use English;
● 74% non-alpha/space
– More non-alpha characters, more difficult to read
Other Considerations
● Conformity can be good
Other Considerations
● Conformity can be good
● How to implement good change?
Other Considerations
● Conformity can be good
● How to implement good change?
● The “next guy” might be YOU.
Other Considerations
● Conformity can be good
● How to implement good change?
● The “next guy” might be YOU.
● Comment to preempt disputes or misunderstandings
Best Book on this Topic
Perl Best Practices by Damian Conway
Questions?
Thank You!
David Bradford
@tinypig
tinypig.com
Web Engineer at OmniTI
omniti.com/presents
Ad

More Related Content

What's hot (20)

WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Php Basic
Php BasicPhp Basic
Php Basic
Md. Sirajus Salayhin
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Business Rules with Brick
Business Rules with BrickBusiness Rules with Brick
Business Rules with Brick
brian d foy
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
lichtkind
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_Codesniffer
Sebastian Marek
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
sagaroceanic11
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6
Andrew Shitov
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
Ricardo Signes
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
lichtkind
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
Dave Cross
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String Functions
Geshan Manandhar
 
What's New in Perl? v5.10 - v5.16
What's New in Perl?  v5.10 - v5.16What's New in Perl?  v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Business Rules with Brick
Business Rules with BrickBusiness Rules with Brick
Business Rules with Brick
brian d foy
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
lichtkind
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_Codesniffer
Sebastian Marek
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6
Andrew Shitov
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
Ricardo Signes
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
lichtkind
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
Dave Cross
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String Functions
Geshan Manandhar
 
What's New in Perl? v5.10 - v5.16
What's New in Perl?  v5.10 - v5.16What's New in Perl?  v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 

Viewers also liked (20)

연세 밥매니저
연세 밥매니저연세 밥매니저
연세 밥매니저
Moon Gi Choi
 
Module 7: Delivering and Managing Training Programs
Module 7: Delivering and Managing Training ProgramsModule 7: Delivering and Managing Training Programs
Module 7: Delivering and Managing Training Programs
Cardet1
 
Motwalls 140508
Motwalls 140508Motwalls 140508
Motwalls 140508
Motwalls
 
Vasikes ennoies ekpedeftikis_texnologias
Vasikes ennoies ekpedeftikis_texnologiasVasikes ennoies ekpedeftikis_texnologias
Vasikes ennoies ekpedeftikis_texnologias
Cardet1
 
Image analysis
Image analysisImage analysis
Image analysis
jadeashworthx
 
Preaty workshop presentation
Preaty workshop presentationPreaty workshop presentation
Preaty workshop presentation
Cardet1
 
Power point presentation
Power point presentationPower point presentation
Power point presentation
jengpanca
 
Working capital management handout emerg
Working capital management handout emergWorking capital management handout emerg
Working capital management handout emerg
eMERG
 
Blendtec - Final
Blendtec - FinalBlendtec - Final
Blendtec - Final
springmarket14
 
Take control of your financial destiny
Take control of your financial destinyTake control of your financial destiny
Take control of your financial destiny
eMERG
 
Market
MarketMarket
Market
jadeashworthx
 
Magazine advert analysis ellie goulding
Magazine advert analysis ellie gouldingMagazine advert analysis ellie goulding
Magazine advert analysis ellie goulding
jadeashworthx
 
Magazine advert analysis jay z
Magazine advert analysis jay zMagazine advert analysis jay z
Magazine advert analysis jay z
jadeashworthx
 
Eisagogi sto windows_vista
Eisagogi sto windows_vistaEisagogi sto windows_vista
Eisagogi sto windows_vista
Cardet1
 
Unit 4 Photography
Unit 4 PhotographyUnit 4 Photography
Unit 4 Photography
Dominika Fillinger
 
Preaty workshop activities
Preaty workshop activitiesPreaty workshop activities
Preaty workshop activities
Cardet1
 
BlendTec Updated 06.04.14
BlendTec Updated 06.04.14BlendTec Updated 06.04.14
BlendTec Updated 06.04.14
springmarket14
 
Overview of Online Teaching and Learning
Overview of Online Teaching and LearningOverview of Online Teaching and Learning
Overview of Online Teaching and Learning
Cardet1
 
Aao gay-se-prem-kare-by-radhe-shyam-ravoria
Aao gay-se-prem-kare-by-radhe-shyam-ravoriaAao gay-se-prem-kare-by-radhe-shyam-ravoria
Aao gay-se-prem-kare-by-radhe-shyam-ravoria
Govats Radhe Ravoria
 
Magazine advert eg
Magazine advert egMagazine advert eg
Magazine advert eg
jadeashworthx
 
연세 밥매니저
연세 밥매니저연세 밥매니저
연세 밥매니저
Moon Gi Choi
 
Module 7: Delivering and Managing Training Programs
Module 7: Delivering and Managing Training ProgramsModule 7: Delivering and Managing Training Programs
Module 7: Delivering and Managing Training Programs
Cardet1
 
Motwalls 140508
Motwalls 140508Motwalls 140508
Motwalls 140508
Motwalls
 
Vasikes ennoies ekpedeftikis_texnologias
Vasikes ennoies ekpedeftikis_texnologiasVasikes ennoies ekpedeftikis_texnologias
Vasikes ennoies ekpedeftikis_texnologias
Cardet1
 
Preaty workshop presentation
Preaty workshop presentationPreaty workshop presentation
Preaty workshop presentation
Cardet1
 
Power point presentation
Power point presentationPower point presentation
Power point presentation
jengpanca
 
Working capital management handout emerg
Working capital management handout emergWorking capital management handout emerg
Working capital management handout emerg
eMERG
 
Take control of your financial destiny
Take control of your financial destinyTake control of your financial destiny
Take control of your financial destiny
eMERG
 
Magazine advert analysis ellie goulding
Magazine advert analysis ellie gouldingMagazine advert analysis ellie goulding
Magazine advert analysis ellie goulding
jadeashworthx
 
Magazine advert analysis jay z
Magazine advert analysis jay zMagazine advert analysis jay z
Magazine advert analysis jay z
jadeashworthx
 
Eisagogi sto windows_vista
Eisagogi sto windows_vistaEisagogi sto windows_vista
Eisagogi sto windows_vista
Cardet1
 
Preaty workshop activities
Preaty workshop activitiesPreaty workshop activities
Preaty workshop activities
Cardet1
 
BlendTec Updated 06.04.14
BlendTec Updated 06.04.14BlendTec Updated 06.04.14
BlendTec Updated 06.04.14
springmarket14
 
Overview of Online Teaching and Learning
Overview of Online Teaching and LearningOverview of Online Teaching and Learning
Overview of Online Teaching and Learning
Cardet1
 
Aao gay-se-prem-kare-by-radhe-shyam-ravoria
Aao gay-se-prem-kare-by-radhe-shyam-ravoriaAao gay-se-prem-kare-by-radhe-shyam-ravoria
Aao gay-se-prem-kare-by-radhe-shyam-ravoria
Govats Radhe Ravoria
 
Ad

Similar to Writing Maintainable Perl (20)

Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
Php2
Php2Php2
Php2
Keennary Pungyera
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenics
Giorgio Cefaro
 
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
osfameron
 
wget.pl
wget.plwget.pl
wget.pl
Yasuhiro Onishi
 
Functional perl
Functional perlFunctional perl
Functional perl
Errorific
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl style
Bo Hua Yang
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
Michael Schwern
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
heumann
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
tutorialsruby
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenics
Giorgio Cefaro
 
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
osfameron
 
Functional perl
Functional perlFunctional perl
Functional perl
Errorific
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl style
Bo Hua Yang
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
Michael Schwern
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
heumann
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
Ad

Recently uploaded (20)

How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 

Writing Maintainable Perl

  • 2. Who am I? David Bradford @tinypig tinypig.com
  • 3. Who am I? ● Web Engineer at OmniTI – Full stack tech consulting – Remote database management and consulting – Large Scale / Mission Critical – We're Hiring! – omniti.com
  • 4. Why This Talk? ● Is Perl a "write-only" language?
  • 5. Why This Talk? ● Is Perl a "write-only" language? No!
  • 6. Why This Talk? ● Is Perl a "write-only" language? No! ● ...but it can be.
  • 7. Why This Talk? ● Is Perl a "write-only" language? No! ● ...but it can be. ● It's our responsibility to keep code:
  • 8. Why This Talk? ● Is Perl a "write-only" language? No! ● ...but it can be. ● It's our responsibility to keep code: – Easy to read
  • 9. Why This Talk? ● Is Perl a "write-only" language? No! ● ...but it can be. ● It's our responsibility to keep code: – Easy to read, so it will be:
  • 10. Why This Talk? ● Is Perl a "write-only" language? No! ● ...but it can be. ● It's our responsibility to keep code: – Easy to read, so it will be: – Easy to maintain
  • 11. My Function, “listify” sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; }
  • 12. Purpose of listify Take a list, for example: List # 1, with 10 items one two three four five six seven eight nine ten Give me several lists back, each containing no more than N items, for example, 4: List #1 with 4 items: one two three four List #2 with 4 items: five six seven eight List #3 with 2 items: nine ten
  • 13. Prepare to call listify listify( @list, 4 ); Result: @array = ( [ 'one', 'two', 'three', 'four' ], [ 'five', 'six', 'seven', 'eight' ], [ 'nine', 'ten' ], ); my @list = qw( one two three four five six seven eight nine ten );
  • 14. Call to listify my @list = qw( one two three four five six seven eight nine ten ); Result: @array = ( [ 'one', 'two', 'three', 'four' ], [ 'five', 'six', 'seven', 'eight' ], [ 'nine', 'ten' ], ); listify( @list, 4 );
  • 15. Result of listify my @list = qw( one two three four five six seven eight nine ten ); listify( @list, 4 ); Result: @array = ( [ 'one', 'two', 'three', 'four' ], [ 'five', 'six', 'seven', 'eight' ], [ 'nine', 'ten' ], );
  • 16. Declare function listify sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } sub listify { }
  • 17. Get Parameters sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } my ($aref,$cc) = @_;
  • 18. Validate Parameters sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } if( ref $aref eq 'ARRAY' && $cc > 0 ) { } return;
  • 19. Declare Return Variable sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } my $j;
  • 20. Loop Through Input Array sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } for(my $i=0; $i<=$#$aref; $i+=$cc) { }
  • 21. Add List to Output Variable sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } push @$j, [@$aref[$i..$i+$cc-1]];
  • 22. ??? sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } $#{$j->[$#{$j}]}=$#$aref%$cc;
  • 23. Update Input Array In Place sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } @$aref = @$j;
  • 24. Return 1 to Indicate Success sub listify { my ($aref,$cc) = @_; if( ref $aref eq 'ARRAY' && $cc > 0 ) { my $j; for(my $i=0; $i<=$#$aref; $i+=$cc) { push @$j, [@$aref[$i..$i+$cc-1]]; } $#{$j->[$#{$j}]}=$#$aref%$cc; @$aref = @$j; return 1; } return; } return 1;
  • 25. Clever (but not easy to read) Line $#{$j->[$#{$j}]}=$#$aref%$cc;
  • 26. Purpose of Clever Line The purpose of the line is to truncate the final array to the number of remaining elements so we don't end up with this: @array = ( [ 'one', 'two', 'three', 'four' ], [ 'five', 'six', 'seven', 'eight' ], [ 'nine', 'ten', undef, undef ], );
  • 28. Improvement #2: Refactor $#{ $j->[ -1 ] } = $#$aref % $cc; $#{ $j->[ $#{$j} ] } = $#$aref % $cc;
  • 29. Improvement #3: Naming $#{ $result_array[ -1 ] } = $#$in_aref % $elements_per_array;
  • 30. Improvement #4: Multiple Lines $final_aref = $result_array[ -1 ]; $#$final_aref = $#$in_aref % $elements_per_array;
  • 31. Improvement #5: Split Another Line my $final_aref = $result_array[ -1 ]; my $elements_in_final = $#$in_aref % $elements_per_array; $#$final_aref = $elements_in_final;
  • 32. Improvement #6: More Whitespace my $final_aref = $result_array[ -1 ]; my $elements_in_final = $#$in_aref % $elements_per_array; $#$final_aref = $elements_in_final;
  • 33. Improvement #7: A comment! my $final_aref = $result_array[ -1 ]; my $elements_in_final = $#$in_aref % $elements_per_array; # Truncate final array $#$final_aref = $elements_in_final;
  • 34. Easier to Read Now? Original: $#{$j->[$#{$j}]}=$#$aref%$cc; Revised: my $final_aref = $result_array[ -1 ]; my $elements_in_final = $#$in_aref % $elements_per_array; # Truncate final array $#$final_aref = $elements_in_final;
  • 35. Apply Principles to Entire Function sub listify { my ( $in_aref, $elements_per_array ) = @_; return if ( ref $in_aref ne 'ARRAY' or $elements_per_array <= 0 ); my @result_array; for( my $i = 0; $i <= $#$in_aref; $i += $elements_per_array ) { push @result_array, [ @$in_aref[ $i..$i + $elements_per_array - 1 ] ]; } # Continued on next slide
  • 36. Apply Principles to Entire Function # Continued from previous slide my $final_aref = $result_array[ -1 ]; my $elements_in_final = $#$in_aref % $elements_per_array; # Truncate final array $#$final_aref = $elements_in_final; @$in_aref = @result_array; }
  • 37. Helpful Modules Perl::Critic based on the book “Perl Best Practices” by Damian Conway Perl::Tidy increase readability of code
  • 38. perlcritic Result $ perlcritic -1 listify.pl Code not contained in explicit package at line 1, column 1. Violates encapsulation. (Severity: 4) Code before strictures are enabled at line 1, column 1. See page 429 of PBP. (Severity: 5) Subroutine "listify" does not end with "return" at line 1, column 1. See page 197 of PBP. (Severity: 4) Code before warnings are enabled at line 1, column 1. See page 431 of PBP. (Severity: 4) No package-scoped "$VERSION" variable found at line 1, column 1. See page 404 of PBP. (Severity: 2) C-style "for" loop used at line 10, column 7. See page 100 of PBP. (Severity: 2) Double-sigil dereference at line 10, column 26. See page 228 of PBP. (Severity: 2) (more double-sigil warnings follow)
  • 39. Critical Fixes Code not contained in explicit package at line 1, column 1. Violates encapsulation. (Severity: 4) Code before strictures are enabled at line 1, column 1. See page 429 of PBP. (Severity: 5) Code before warnings are enabled at line 1, column 1. See page 431 of PBP. (Severity: 4) sub listify { my ( $in_aref, $elements_per_array ) = @_; # --- cut --- #!/usr/bin/perl use strict; use warnings;
  • 40. Explicit Return Subroutine "listify" does not end with "return" at line 2, column 1. See page 197 of PBP. (Severity: 4) # Truncate final array $#$final_aref = $elements_in_final; @$in_aref = @result_array; } return;
  • 41. $VERSION No package-scoped "$VERSION" variable found at line 1, column 1. See page 404 of PBP. (Severity: 2) #!/usr/bin/perl use strict; use warnings; sub listify { my ( $in_aref, $elements_per_array ) = @_; # --- cut --- our $VERSION = '1.19';
  • 42. C-style "for" Loop C-style "for" loop used at line 11, column 7. See page 100 of PBP. (Severity: 2)
  • 43. C-style "for" Loop C-style "for" loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) ● “What?” No C-style “for” loops
  • 44. C-style "for" Loop C-style "for" loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) ● “What?” No C-style “for” loops ● “Why?” More difficult to read and maintain
  • 45. C-style "for" Loop C-style "for" loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) ● “What?” No C-style “for” loops ● “Why?” More difficult to read and maintain ● “I disagree.” That's OK. Modify Perl::Critic settings!
  • 46. C-style "for" Loop C-style "for" loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) ● “What?” No C-style “for” loops ● “Why?” More difficult to read and maintain ● “I disagree.” That's OK. Modify Perl::Critic settings! But today, I'm going to fix all the defaults
  • 47. Change to “while” loop C-style "for" loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) Old: for( my $i = 0; $i <= $#$in_aref; $i += $elements_per_array ) { # (loop contents) } New: my $i = 0; while($i <= $#$in_aref) { # (loop contents) $i += $elements_per_array; }
  • 48. Double-sigil dereference ● Caused by – @$my_array_reference ● Fixed with – @{ $my_array_reference }
  • 49. JAPH: The Do-Not Example sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){ s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95? &qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "}, $q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].)
  • 50. JAPH: The Do-Not Example sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){ s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95? &qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "}, $q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].) ● Ugly variables $" $; $ " and even $_, @_
  • 51. JAPH: The Do-Not Example sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){ s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95? &qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "}, $q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].) ● Ugly variables $" $; $ " and even $_, @_ – use English;
  • 52. JAPH: The Do-Not Example sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){ s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95? &qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "}, $q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].) ● Ugly variables $" $; $ " and even $_, @_ – use English; ● 74% non-alpha/space
  • 53. JAPH: The Do-Not Example sub q{ord($_[0])}sub qq{chr($_[0])}(@q=(j,q,q,,q,s,,u,o,[$;=@q,$"=sub{for(@q){ s/(.)/&qq(&q($1)-1)/e}},$_=q,*534`!./4(%2`0%2{`(!#+%2,,tr;{;,;,s/(.)/&q($1)>95? &qq(&q($1)-64):&qq(&q($ 1)+64)/eg,% q=(q,qq,,sub{eval$ _[0]},q,q,,1)])),&{$ "}, $q{qq}->(qq.$;->[$q=$q{q}]${$;}[++$q]$;->[$[]${$;}[-1+$)-$ )+$#q]$;->[$#q-$q].) ● Ugly variables $" $; $ " and even $_, @_ – use English; ● 74% non-alpha/space – More non-alpha characters, more difficult to read
  • 55. Other Considerations ● Conformity can be good ● How to implement good change?
  • 56. Other Considerations ● Conformity can be good ● How to implement good change? ● The “next guy” might be YOU.
  • 57. Other Considerations ● Conformity can be good ● How to implement good change? ● The “next guy” might be YOU. ● Comment to preempt disputes or misunderstandings
  • 58. Best Book on this Topic Perl Best Practices by Damian Conway
  • 60. Thank You! David Bradford @tinypig tinypig.com Web Engineer at OmniTI omniti.com/presents

Editor's Notes

  • #43: C-style &amp;quot;for&amp;quot; loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) “What?” Damian recommends against using C-style “for” loops” “Why?” His main argument is that C-style “for” loops are more difficult to read and therefore more difficult to maintain “I disagree.” That&amp;apos;s OK and in fact Perl::Critic allows for a lot of customizations to alter or remove those policies you don&amp;apos;t agree with BUT, I&amp;apos;m going to fix all the default suggestions for the purpose of this presentation
  • #44: C-style &amp;quot;for&amp;quot; loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) “What?” Damian recommends against using C-style “for” loops” “Why?” His main argument is that C-style “for” loops are more difficult to read and therefore more difficult to maintain “I disagree.” That&amp;apos;s OK and in fact Perl::Critic allows for a lot of customizations to alter or remove those policies you don&amp;apos;t agree with BUT, I&amp;apos;m going to fix all the default suggestions for the purpose of this presentation
  • #45: C-style &amp;quot;for&amp;quot; loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) “What?” Damian recommends against using C-style “for” loops” “Why?” His main argument is that C-style “for” loops are more difficult to read and therefore more difficult to maintain “I disagree.” That&amp;apos;s OK and in fact Perl::Critic allows for a lot of customizations to alter or remove those policies you don&amp;apos;t agree with BUT, I&amp;apos;m going to fix all the default suggestions for the purpose of this presentation
  • #46: C-style &amp;quot;for&amp;quot; loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) “What?” Damian recommends against using C-style “for” loops” “Why?” His main argument is that C-style “for” loops are more difficult to read and therefore more difficult to maintain “I disagree.” That&amp;apos;s OK and in fact Perl::Critic allows for a lot of customizations to alter or remove those policies you don&amp;apos;t agree with BUT, I&amp;apos;m going to fix all the default suggestions for the purpose of this presentation
  • #47: C-style &amp;quot;for&amp;quot; loop used at line 11, column 7. See page 100 of PBP. (Severity: 2) “What?” Damian recommends against using C-style “for” loops” “Why?” His main argument is that C-style “for” loops are more difficult to read and therefore more difficult to maintain “I disagree.” That&amp;apos;s OK and in fact Perl::Critic allows for a lot of customizations to alter or remove those policies you don&amp;apos;t agree with BUT, I&amp;apos;m going to fix all the default suggestions for the purpose of this presentation
  • #50: $&amp;quot;, $;, $ &amp;quot; (ignored space - crazy! Whitespace thus used to my advantage) even $_, @_ use English; My obfuscated code is 74% non-alpha/space, and that isn&amp;apos;t considering the abuse of alpha conventions like q qq s tr etc The higher percentage of non-alpha characters, the more difficult to read
  • #51: $&amp;quot;, $;, $ &amp;quot; (ignored space - crazy! Whitespace thus used to my advantage) even $_, @_ use English; My obfuscated code is 74% non-alpha/space, and that isn&amp;apos;t considering the abuse of alpha conventions like q qq s tr etc The higher percentage of non-alpha characters, the more difficult to read
  • #52: $&amp;quot;, $;, $ &amp;quot; (ignored space - crazy! Whitespace thus used to my advantage) even $_, @_ use English; My obfuscated code is 74% non-alpha/space, and that isn&amp;apos;t considering the abuse of alpha conventions like q qq s tr etc The higher percentage of non-alpha characters, the more difficult to read
  • #53: $&amp;quot;, $;, $ &amp;quot; (ignored space - crazy! Whitespace thus used to my advantage) even $_, @_ use English; My obfuscated code is 74% non-alpha/space, and that isn&amp;apos;t considering the abuse of alpha conventions like q qq s tr etc The higher percentage of non-alpha characters, the more difficult to read
  • #54: $&amp;quot;, $;, $ &amp;quot; (ignored space - crazy! Whitespace thus used to my advantage) even $_, @_ use English; My obfuscated code is 74% non-alpha/space, and that isn&amp;apos;t considering the abuse of alpha conventions like q qq s tr etc The higher percentage of non-alpha characters, the more difficult to read
  • #55: Conformity can be good: code like everyone else has in the apps you inherit or join. Unless it&amp;apos;s horrible, then start a consistent style. Then how to implement good change? One idea: implement across the entire code base, a little at a time. For example, get &amp;quot;use strict&amp;quot; working everywhere first. Make it easy on the &amp;quot;next guy&amp;quot;. The next guy might be YOU. Comment to pre-empt disputes or misunderstandings that will break production.
  • #56: Conformity can be good: code like everyone else has in the apps you inherit or join. Unless it&amp;apos;s horrible, then start a consistent style. Then how to implement good change? One idea: implement across the entire code base, a little at a time. For example, get &amp;quot;use strict&amp;quot; working everywhere first. Make it easy on the &amp;quot;next guy&amp;quot;. The next guy might be YOU. Comment to pre-empt disputes or misunderstandings that will break production.
  • #57: Conformity can be good: code like everyone else has in the apps you inherit or join. Unless it&amp;apos;s horrible, then start a consistent style. Then how to implement good change? One idea: implement across the entire code base, a little at a time. For example, get &amp;quot;use strict&amp;quot; working everywhere first. Make it easy on the &amp;quot;next guy&amp;quot;. The next guy might be YOU. Comment to pre-empt disputes or misunderstandings that will break production.
  • #58: Conformity can be good: code like everyone else has in the apps you inherit or join. Unless it&amp;apos;s horrible, then start a consistent style. Then how to implement good change? One idea: implement across the entire code base, a little at a time. For example, get &amp;quot;use strict&amp;quot; working everywhere first. Make it easy on the &amp;quot;next guy&amp;quot;. The next guy might be YOU. Comment to pre-empt disputes or misunderstandings that will break production.