What does “Coding Small” mean?

Simple. It means code small. A small bit of code is easier to debug then a large program.

Take for instance my current Perl coding project. I’m writing a script that will reside on my server and download a comic, and news headlines and then print it out at about 5 in the morning so everybody can have a bit of happiness, and find out what hell has broken loose in the world today.

The comic I want to use is Sheldon. Sometimes, however the creator doesn’t keep strictly to posting the link on his RSS feed. So, I have to make sure that I grab that, or I will get a perl error later on. How you ask?

Until tonight, this comic meant absoloutely nothing to me . . . .

Stand Back!

Yes indeed.

use warnings; use strict;
my @file_contents;

open FILE, "text.txt" or die "$!"; #open with write rights
@file_contents = <FILE>;
close FILE;
foreach my $text (@file_contents){
 chomp $text;
 if ($text =~ m/^Strip for /){
 print $text;
 } else {
 print "No goodness here (Text:$text) n"
 }
}

Pretty much what this snippet does is takes and loads the contents of the file into an array, with each line being an item in the array.  It then stores one value of the array, chomps the newline character off of the end, and check if it starts with “Strip for”, case sensitively via a regular expression.  If the value starts as wanted, then I’m good and it prints that line from the file.  If  the line does not start with “Strip for”, then it prints “No Goodness here” and what the line actually was.

This can then be turned into a subroutine to use with perl.

use warnings; use strict;
my @file_contents;

open FILE, "text.txt" or die "$!"; #open with write rights
@file_contents = <FILE>;
close FILE;

&reg_ex(@file_contents);

sub reg_ex{
 foreach my $text (@file_contents){
 chomp $text;
 if ($text =~ m/^Strip for /){
 print $text;
 } else {
 print "No goodness here (Text:$text) n"
 }
 }
}

Now, no matter how many different types of data I wanna shove at the array, I just call &reg_ex(array of data), and it parse and prints.

Why is this nice?  Let say that I have a more complicated program.  Now that I know that section of code works, I can paste that subroutine in, call when needed, and know that it works and is not affecting other parts of my program.  Then I can work on debugging the rest of my crappy code.

Lemme know what you think in the comments, and hopefully I’ll be posting more of this stuff as I go on . . . .