Reputation: 1315
Hi I am very new to perl. as you can see in my code below, I need to fetch a parameter "page" from querystring. and I also want to validate the parameter value. if the parameter is not in querystring or not a number, then assign 1 to $current_page, else just assign the value to $current_page. how can I do this in perl? please help me.
use strict;
use CGI qw/:standard/;
my $querystring = CGI::Vars();
my $current_page = $querystring->{page};
print $current_page; # I would get a warning "Use of uninitialized value..."
Upvotes: 0
Views: 122
Reputation: 386481
This will use 1
if the param is missing:
my $current_page = $querystring->{page} || 1;
It won't check to make sure it's a number, though. That would be something like:
my $current_page = $querystring->{page};
$current_page = 1
if !defined($current_page)
|| $current_page =~ /[^0-9]/
|| $current_page == 0;
If you need to validate multiple parameters, you might want something like Data::FormValidator. Once you start using it, you get all of its benefits including the ability to trim leading and trailing spaces for example.
Upvotes: 2