Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > RFC: UniqueID Module

Reply
Thread Tools

RFC: UniqueID Module

 
 
John
Guest
Posts: n/a
 
      04-03-2004
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!





 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-03-2004
John <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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';

^^
Wrong comparison operator. "==" is for numbers, you need "eq".

> 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


You can have unique identifiers in Perl by just taking a reference to
anything. So you get most of the behavior you describe after just saying:

use constant red => \ '';
use constant yellow => \ '';
use constant blue => \ '';

instead of

use UniqueID qw (red yellow blue);

The difference is that the arithmetic operation is allowed and that
there is no ->name method.

> 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!


....plus a warning, if warnings are on as they should. While an undef
*can* be mistaken for a 0 (or a ""), there is a difference, and a
program can tell which is which.

[...]

I have as yet not missed the functionality of UniqueID.

Anno
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      04-03-2004
One more point in the case for UniqueID that I didn't mention: a UniqueID
would maintain it's identity through serialization across processes. It
would be implemented as a blessed reference to a scalar containing the
symbol name.

Also, below is another perhaps beter example of UniqueID's use, although it
is still contrived:

use UniqueID 'error';

sub bar {
my($arg) = @_;
return not(1) ? $arg : error;
}

my $foo = bar('baz');
print $foo eq error ? "Foo is no good." : "Foo is $foo.";

The actual situation where I want to use UniqueID is too complicated for
this post, but I need a variable to be able to contain *any* value that the
user can throw at it, *or* some flag value that is above all the other
values

Also, are you sure that == and != wouldn't bet appropriate comparators,
since UniqueID objects aren't strings any more than they are numbers, and
the == operators implies "mathematical identity", which is really what I'm
aiming for. So you'd have

use UniqueID 'infinity';
my $variable = 5;
print "5 is infinite!\n" if $variable == infinity;



"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:c4llrr$41q$(E-Mail Removed)-Berlin.DE...
> John <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > 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';

> ^^
> Wrong comparison operator. "==" is for numbers, you need "eq".
>
> > 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
>
> You can have unique identifiers in Perl by just taking a reference to
> anything. So you get most of the behavior you describe after just saying:
>
> use constant red => \ '';
> use constant yellow => \ '';
> use constant blue => \ '';
>
> instead of
>
> use UniqueID qw (red yellow blue);
>
> The difference is that the arithmetic operation is allowed and that
> there is no ->name method.
>
> > 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!

>
> ...plus a warning, if warnings are on as they should. While an undef
> *can* be mistaken for a 0 (or a ""), there is a difference, and a
> program can tell which is which.
>
> [...]
>
> I have as yet not missed the functionality of UniqueID.
>
> Anno



 
Reply With Quote
 
John
Guest
Posts: n/a
 
      04-03-2004
Here is a proposed implementation for the package:

use strict; use warnings;
package UniqueID;
our $VERSION = '1.01';

use Carp;

use overload
'==' => sub {my $class = ref(shift); croak "'==' operator isn't defined
for $class objects. Did you mean 'eq'?"},
'eq' => 'equals',
'!=' => sub {my $class = ref(shift); croak "'!=' operator isn't defined
for $class objects. Did you mean 'ne'?"},
'ne' => 'notequals',
nomethod => sub {
my($a, $b, $c, $operator) = @_;
my $class = ref($a);
croak "The '$operator' operation isn't defined for $class objects";
},
'""' => 'stringify'
;

sub stringify {
my($self) = @_;
return overload::StrVal($self).'='.$$self;
}
sub equals {
ref($_[1]) eq ref($_[0]) and ${$_[0]} eq ${$_[1]}
};
sub notequals {
not (ref($_[1]) eq ref($_[0]) and ${$_[0]} eq ${$_[1]})
};

sub name {
my($self) = @_;
my @parts = split /\:\:/, $$self;
return $parts[-1];
}

sub fullname {
my($self) = @_;
return $$self;
}

sub new {
my($pkg, $client_package, $name) = @_;

my $string = $client_package."::".$name;
my $self = bless \$string, $pkg;
return $self;
}

sub make_identifier {
my($pkg, $client_package, $name) = @_;

my $id = $pkg->new($client_package, $name);

no strict 'refs';
*{$client_package."::".$name} = sub {
$id;
};
}

sub import {
my($pkg, @names) = @_;
my $client_package = caller(0);
for(@names) {
$pkg->make_identifier($client_package, $_);
}
}
"John" <(E-Mail Removed)> wrote in message
news:c4l9gc$2iqd$(E-Mail Removed)...
> 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!
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UniqueID vs. ClientID Why are they Differenct? Ken Baltrinic ASP .Net 2 03-04-2005 06:36 PM
page.UniqueID =?Utf-8?B?QXJuZQ==?= ASP .Net 0 12-07-2004 08:39 PM
EnsureChildControls causes UniqueID to be incorrect inside UserControl Kepler ASP .Net 1 07-07-2004 12:16 PM
Difference between ID, ClientID and UniqueID? Henri ASP .Net 2 05-18-2004 12:18 AM
Problem using .NET UniqueID to reference RadioButton in Javascript? David Whitney ASP .Net 1 07-21-2003 04:17 AM



Advertisments