I am always looking to get better at the way I look, comprehend and write code. And, am completely open to others providing input to help me get better in my coding process. Last week I decided to purchase the Perl best practices book in an effort to make my code uniform for all applications I create and deploy within an enviroment. In the process of reading and speaking with a few other people who work with other scripting languages the main arguement I hear is that Perl isn`t scalable past a few hundred lines of code. I know this isn`t true because I have seen and written well over a thousand lines of clear readable code. Although in looking at past code from previous posts I can see where improvements can be made especially removing repetative subroutines making them a single subroutine accessable from other parts of the application.
Perl Best Practices on Amazon: http://www.amazon.com/Perl-Best-Practices-Damian-Conway/dp/0596001738/ref=sr_1_1?ie=UTF8&qid=1296418240&sr=8-1
Back to the main topic of this post, the reason Perl seems to not be scalable is readability I think this is addressed very well by reading Perl best practices. And, the other reason properly identifying data types in large applications when receiving user data. The latter question is what I am going to try to address with a simple script below.
Data::Util is a Perl module that attemps to identify data types within an application and it does it very well for most of your programming needs. Here is a simple quick and dirty script that validates an SSN from the user is a number and the correct length. Using the Data::Util Perl module along with other Perl Best Practices tools gives you a huge advantage when you need to write small or large applications.
use strict;
use warnings;
use Data::Util qw(:all);
my $bool_val;
$bool_val = is_string('foo');
print "Bool scalar value: $bool_val, Prints out returned 1 value as successfull \n"; # Returns true.
$bool_val = is_integer(1);
print "Bool scalar value: $bool_val, Prints out returned 1 value as successfull \n"; # Returns true.
$bool_val = is_string([]);
print "Bool scalar value: $bool_val, Does not return a value it is null \n"; # Returns false.
$bool_val = is_string(undef);
print "Bool scalar value: $bool_val, Does not return a value it is null \n"; # Returns false.
$bool_val = is_number('number');
print "Bool scalar value: $bool_val, Does not return a value string so null value \n"; #Returns false empty null value.
#How are these used within your application.
# lets say this is a random ssn number that a client enters into your banking web application for a new car credit check
my $client_ssn =456123497;
# Lets validate that this is indeed a interger string and that the length is correct.
if (is_integer($client_ssn) && length($client_ssn) == 9 ){
print " SSN number is correct and valid: $client_ssn \n";
}
else
{
print "Please enter a valid 9 digit ssn: $client_ssn without dashes \n";
}
Here is the Data::Util Perl module link and information below.
http://search.cpan.org/~gfuji/Data-Util-0.58/lib/Data/Util.pm
This module provides utility functions for data and data types, including functions for subroutines and symbol table hashes (stashes).
The implementation of this module is both Pure Perl and XS, so if you have a C compiler, all the functions this module provides are really faster.
There are many benchmarks in the DIST-DIR/benchmark/ directory.
Recent Comments