<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Custom Perl</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/custom_perl/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/custom_perl/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/custom_perl//1444</id>
    <updated>2012-08-01T01:09:51Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Extensible Maintainable Subroutines and Methods</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/custom_perl/2012/08/extensible-maintainable-subroutines-and-methods.html" />
    <id>tag:blogs.perl.org,2012:/users/custom_perl//1444.3645</id>

    <published>2012-08-01T01:09:41Z</published>
    <updated>2012-08-01T01:09:51Z</updated>

    <summary> I&apos;ve been thinking about the way I write Perl subroutines and methods as compared to some other Perl programmers, and I&apos;ve decided to write a post about it. To make my subroutines and methods more reusable, extensible, and maintainable,...</summary>
    <author>
        <name>Andrew Proper</name>
        <uri>http://about.me/andrew.proper</uri>
    </author>
    
    <category term="hash" label="hash" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="methods" label="methods" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parameters" label="parameters" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl" label="Perl" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="reference" label="reference" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="subroutines" label="subroutines" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/custom_perl/">
        <![CDATA[<p>
I've been thinking about the way I write Perl subroutines and methods as compared to some other Perl programmers, and I've decided to write a post about it.
</p><p>
To make my subroutines and methods more reusable, extensible, and maintainable, I make them receive a hash reference as their only argument and return a hash reference. This has multiple benefits.
</p><p>
First, the parameters going into the subroutine are named when passed in, and these names are used to identify them inside the subroutine. This means that new parameters can be added with keys in the hash, a very minimal change. The same is true of the return hash. Using keys in a hash is far simpler to maintain than using positions in a list. I've spent enough time wrestling with indexed parameters in subroutines that I never use lists of parameters in code that I expect to use more that once (which is basically all of it).
</p><p>
Second, a hash reference is passed in instead of a hash because the reference can be tested using the <i>ref</i> function to ensure that is is actually a HASH ref. Assigning @_ to a hash is not as robust. The same is true of the returned hash reference.
</p><p>
Here is a brief example of a subroutine that receives and returns references to hashes of named parameters:
</p>
<pre style=" color: #669; ">
#!/usr/bin/env perl
use strict;
use warnings;

use feature qw(say); # perl 5.10+

# CPAN modules
use Data::Dumper;
$Data::Dumper::Purity = 1;

my $sendRef = { string => 'Wow! Perl is so awesome, right?' };
my $returnRef = no_punct( $sendRef );
unless ( defined $returnRef and ref $returnRef eq 'HASH' ) {
  say 'no_punct failed to return a HASH ref!';
  exit;
}
print 'with punctuation: '.Dumper( $sendRef );
print 'punctuation removed: '.Dumper( $returnRef );

<i>
=head1 Subroutines Begin

=cut

=head2 no_punct( \%params )

Processes each scalar value of a copy of %params and removes any punctuation in it.

Returns a hash reference.

=cut
</i>
sub no_punct {
  my $paramsRef = shift;
  my %params = ();
  %params = %$paramsRef if defined $paramsRef and ref $paramsRef eq 'HASH';

  my %return = %params;
  foreach my $k ( keys %return ) {
    if ( ref $return{ $k } eq 'HASH' ) {
      $return{ $k } = no_punct( $return{ $k } ); # recursive call
    } elsif ( ref $return{ $k } eq 'ARRAY' ) {
      foreach my $i ( 0 .. ( scalar @{ $return{ $k } - 1 ) ) {
        $return{ $k }[ $i ] =~ s/[^\w\s\d]//g;
      }
    } else {
      next unless ref $return{ $k } eq 'SCALAR';
      $return{ $k } =~ s/[^\w\s\d]//g;
    } 
  }

  return \%return;
} 
</pre>
<p>
And that's the basics of it. What do you think? Comments are very welcome.
</p>]]>
        
    </content>
</entry>

<entry>
    <title>Perl Coding for the Win</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/custom_perl/2012/07/perl-coding-for-the-win.html" />
    <id>tag:blogs.perl.org,2012:/users/custom_perl//1444.3637</id>

    <published>2012-07-31T02:05:41Z</published>
    <updated>2012-07-31T02:10:25Z</updated>

    <summary>I’ve been writing programs in Perl since about 1999. Wow, that’s a long time now. That was way back when I was studying Computer Science and working for the IT department of the University I went to. I still write...</summary>
    <author>
        <name>Andrew Proper</name>
        <uri>http://about.me/andrew.proper</uri>
    </author>
    
    <category term="perl" label="Perl" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="programming" label="programming" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/custom_perl/">
        <![CDATA[<p>I’ve been writing programs in Perl since about 1999. Wow, that’s a long time now. That was way back when I was studying Computer Science and working for the IT department of the University I went to. I still write Perl programs these days, at my current job, where we call it Application Development.</p>

<p>Since I like Perl development a lot, and I like learning about Perl, I figure I might like writing about Perl too. So that’s what I’m thinking of doing in this blog. I hope some of you find it interesting and useful. After all these years of experience, I might have something to say that you might find useful and/or interesting, especially if you are starting out with programming and/or Perl coding.</p>

<p>Enjoy. :)</p>

<p>--<br />
<a href="http://customperl.tumblr.com/post/28168037134/perl-coding-for-the-win">original post on customperl.tumblr.com</a></p>]]>
        
    </content>
</entry>

</feed>
