Command-Line Program Patterns
Argument Validation
Validate a simple name before using it.
Argument Validation
argument_validation.pl
use strict;
use warnings;
my $style = ;
my $name = "report";
if ($style eq "slash") {
$name = "bad/slash";
} elsif ($style eq "empty") {
$name = "";
}
my $valid = $name =~ /^[A-Za-z0-9_]+$/ ? 1 : 0;
my $status = $valid ? "accepted" : "rejected";
print "style=$style\n";
print "name=$name\n";
print "status=$status\n";
use strict;
use warnings;
my $style = ;
my $name = "report";
if ($style eq "slash") {
$name = "bad/slash";
} elsif ($style eq "empty") {
$name = "";
}
my $valid = $name =~ /^[A-Za-z0-9_]+$/ ? 1 : 0;
my $status = $valid ? "accepted" : "rejected";
print "style=$style\n";
print "name=$name\n";
print "status=$status\n";
use strict;
use warnings;
my $style = ;
my $name = "report";
if ($style eq "slash") {
$name = "bad/slash";
} elsif ($style eq "empty") {
$name = "";
}
my $valid = $name =~ /^[A-Za-z0-9_]+$/ ? 1 : 0;
my $status = $valid ? "accepted" : "rejected";
print "style=$style\n";
print "name=$name\n";
print "status=$status\n";
argument-validation
Validation checks whether input follows the shape a command expects. The result can be a plain status string instead of an exception.