eBiz Help
Affordable Web Conferencing
Sign up for a free trial or request a free quote today
Write 10,000 lines of code in 10 minutes!
Iron Speed Designer – Free Evaluation
Enhance and Expand the Capabilities of Outlook
Take Control of Outlook With Professional Add-In Solutions


Recent Articles

Rounding Time

It was a long, hard day. Up early to clean a room of furniture so that new carpet can be put in later this week, off to a real estate refinancing closing after that, back to rake leaves and then the usual computer type work. After dinner some refinishing of hardwood floors and a little sealing of cracks noticed when I pulled up some...

Beginning Web Development With Perl (Review)
While Perl may not be the "cool" language for websites anymore, there are some of us who prefer to work with it because we use it for so many other tasks. I've done PHP, and toyed with other things, but I always...

Dynamic DNS Services Update Scripts
Strangely enough, I never had any need for a dynamic DNS service until this week. In retrospect, it really does seem odd that I've never needed such a service before now, but so be it. My problem this week was that I wanted to set...

ASP.NET and IIS Serve More Fortune 1000 Sites Than any Other Web Server Technologies
A new survey of the 2005 Fortune 1000 Web sites reports that Microsoft's ASP.NET, ASP, and Internet Information Services (IIS) serve the majority of leading U.S. corporate sites...

Controlling Concurrent Runs with Perl
Sometimes you have a program that can't be run by more than one person, or one that must run frequently but you don't know for sure how long an instance of it will take. One way to accomplish that is to use a lock file...

A.P. Lawrence On Pro Perl
In the review copy I read, there still were some unfortunate typos that might confuse someone completely new to Perl. I hope those will be fixed before the actual publication. None of them bothered me, but they could be bad for someone starting with no experience at all...


01.23.06


Perl 'Eval' For Data Validation

By A.P. Lawrence

I recently did a little web based Service Schedule. This is something that gets its data from another program: in other words, the details of what will be serviced and when are supplied by that program; this web based app validates things, assigns technicians and does some other things that the first program doesn't.

One of the things entered is the requested date for service, which might look like "Sat 2/6/06". If you happen to have a 2006 calendar handy ("cal 2 2006" will give you one on any Unix/Linux box), you'll notice a small problem: 2/6/2006 is not a Saturday. Because the data entry to the first application isn't validated, my program can receive just about anything and it has to check it.

Checking the day is not too hard in Perl. Here's a little test script that demonstrates.

#!/usr/bin/perl
use Time::Local;
@days=qw(SUN MON TUE WED THU FRI SAT);
checkdate($ARGV[0],$ARGV[1]);

sub checkdate {
  $date=shift;
  $day=shift;
  @d=split /\//,$date;
  $newdate="";
  $newdate=timelocal(0,0,0,$d[1],$d[0]-1,$d[2]+100);
  @newd=localtime($newdate);
  my $line="$date ";
  if ($day !~ /$days[$newd[6]]/i) {
   $warn="!! $days[$newd[6]] !!";
 }
 $line .= "$day $warn";
 print "$line\n";
}

However, there's a big problem. What happens if you pass that script absolute garbage?

$ ./checkdates.pl 2/6/06 sat
2/6/06 sat !! MON !!
$ ./checkdates.pl 2/6/06 mon
2/6/06 mon
$ ./checkdates.pl 2/29/06 mon
Day '29' out of range 1..28 at ./checkdates.pl line 11

Oops. That's not going to work.

Fortunately, it's not hard to fix, and that's where "eval" comes in. It's not much of a change:


Conduct an unlimited number of Web Meetings with Free Phone Conferencing* all for a low price of just $49 per month.
Try it FREE now >>


#!/usr/bin/perl
use Time::Local;
@days=qw(SUN MON TUE WED THU FRI SAT);
checkdate($ARGV[0],$ARGV[1]);

sub checkdate {
  $date=shift;
  $day=shift;
  @d=split /\//,$date;
  $newdate="";
  eval {
  $newdate=timelocal(0,0,0,$d[1],$d[0]-1,$d[2]+100);
  @newd=localtime($newdate);
 };
 my $line="$date ";
 if ($day !~ /$days[$newd[6]]/i) {
  $warn="!! $days[$newd[6]] !!";
 }
 $line .= "$day $warn";
 print "$line\n";
}

The script doesn't crash now:

$ ./checkdates.pl 2/29/06 mon 2/29/06 mon !! SUN !!

In this form it's not particularly helpful in identifying that the input was bad data, but that's easy enough to fix if you want: just check to see if $newdate is still blank. If it is, the date conversion failed outright. You can also test the return value of the "eval".

This "eval" protects a block of code from crashing your script. Be sure to notice the ";" following the close of the block; that's necessary here.

The "eval" can also be used to compile and evaluate strings; examples are in your Perl manual.

*Originally published at APLawrence.com

About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com


About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals

PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com


-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article




Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde