J
John
I need an alternative for 'use constant', but that allows the definition of
unique IDs not associated with scalar values.
I propose the UniqueID class, summarized below. I'm asking this list:
- Is this module necessary, or is there a better way to do this?
- Is it aptly named?
- Other comments before I post to CPAN?
Synopsis:
use UniqueID qw (red yellow blue);
my $color = red;
print "Just as we thought!\n" if $color == red;
print "This will never happen!\n" if $color == blue;
print "Nor will this!\n" if $color == 'red';
print "Color is ".$color->name.".\n";
$color + 1; #Raises an exception. Addition isn't defined for UniqueID
objects.
Output:
Just as we thought!
Color is red.
The '+' operation isn't defined for UniqueID objects at C:\test5.pl line 11
Rationale:
UniqueID lets you define unique identifiers that are not associated with
scalar values. Contrast this with the "constant" package, which simply
aliases names to scalar values.
UIDs are safer than constants, because they can not accidently be confused
with un-related scalars. For example, the following accident might happen
using aliased constants:
use constant red => 0;
my $color;
my $color = red if not 1; #*Don't* set $color to red.
#Comparing $color to red is the same as comparing it to 0.
if($color == red) {
print "Color is red!\n";
} else {
print "Color is NOT red!\n";
}
Output: Color is red!
Whereas colors can never be confused with numbers if you use UIDs:
use UniqueID qw (red);
my $color = red if 0; #*Don't* set $color to red.
###Red doesn't equal 0 or undef.
if($color == red) {
print "Color is red!\n";
} else {
print "Color is NOT red!\n";
}
Output: Color is NOT red!
unique IDs not associated with scalar values.
I propose the UniqueID class, summarized below. I'm asking this list:
- Is this module necessary, or is there a better way to do this?
- Is it aptly named?
- Other comments before I post to CPAN?
Synopsis:
use UniqueID qw (red yellow blue);
my $color = red;
print "Just as we thought!\n" if $color == red;
print "This will never happen!\n" if $color == blue;
print "Nor will this!\n" if $color == 'red';
print "Color is ".$color->name.".\n";
$color + 1; #Raises an exception. Addition isn't defined for UniqueID
objects.
Output:
Just as we thought!
Color is red.
The '+' operation isn't defined for UniqueID objects at C:\test5.pl line 11
Rationale:
UniqueID lets you define unique identifiers that are not associated with
scalar values. Contrast this with the "constant" package, which simply
aliases names to scalar values.
UIDs are safer than constants, because they can not accidently be confused
with un-related scalars. For example, the following accident might happen
using aliased constants:
use constant red => 0;
my $color;
my $color = red if not 1; #*Don't* set $color to red.
#Comparing $color to red is the same as comparing it to 0.
if($color == red) {
print "Color is red!\n";
} else {
print "Color is NOT red!\n";
}
Output: Color is red!
Whereas colors can never be confused with numbers if you use UIDs:
use UniqueID qw (red);
my $color = red if 0; #*Don't* set $color to red.
###Red doesn't equal 0 or undef.
if($color == red) {
print "Color is red!\n";
} else {
print "Color is NOT red!\n";
}
Output: Color is NOT red!