http://www.perlmonks.org?node_id=525966

Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

Honoured bretheren in Perl

I am trying to create a constrained variable using syntax like this ...

use threads; use threads::shared; my $var : shared;
To keep things simple I have created a package called Me providing a 'Me' object. Now I want to be able to do something like this.
use Me; my $user : Me; $user = "Random"; # this is OK $user = "Deterministic"; # croaks

Here is the Me.pm
#!/usr/bin/perl use strict; use warnings; package Me; use Carp; use overload '""' => \&str; sub new { my ($class, $value) = @_; validate ($value) if defined $value; bless \$value, $class; } sub validate { my $value = shift; my @names = qw (Random_Walk Random Walk); croak "$value is not a good name\n" unless grep {$value eq $_} @na +mes; } sub str {"Not telling you"}; 1;
It works fine like this
> perl -Mlib=. -MMe -le'my $m = new Me (Random);print $m' Not telling you > perl -Mlib=. -MMe -le'my $m = new Me (Determined);print $m' Determined is not a good name at -e line 1 # But I can not suss out how to get his to work > perl -Mlib=. -MMe -le'my $m : Me; $m = "Random"; print $m' Invalid SCALAR attribute: Me at -e line 1

I have had a look into attributes and found this Invalid SCALAR attribute? but I remain completely unenlighted how to use this to get the threads::shared syntax working or even if it is possible without delving into XS

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!