Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

using an object as a variable type

by Random_Walk (Parson)
on Jan 27, 2006 at 13:43 UTC ( #525966=perlquestion: print w/ replies, xml ) Need Help??
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!

Comment on using an object as a variable type
Select or Download Code
Re: using an object as a variable type
by Anonymous Monk on Jan 27, 2006 at 14:00 UTC
    "using an object as a variable type" sounds like nonsense :)

    perldoc -f eval

      Not sure how the title is nonsense, I wish to constrain the behaviour of the variable to that defined in the 'Me' object so is the package not providing the type of the variable ? I am trying to do this using the syntax of threads::shared which I guess is instantiating a threads::shared object and assigning it to the given variable name. I can live with what I have now my $m = new Me (Random); but out of curiosity and to improve my Perl foo I want to discover and emulate how my $val : shared works.

      Not sure how eval can help me here, I don't want to trap the errors, I am intending to enforce limits on the varaiable and croak if an attempt is made to assign an illegal value to it.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
        I am trying to do this using the syntax of threads::shared which I guess is instantiating a threads::shared object and assigning it to the given variable name.

        That's not what it does. It ties the variable in such a way that multiple threads can access it. That's not what the attribute syntax does though; attributes just run a specific piece of code just after Perl compiles the statement. That code can do anything.

      perldoc attributes
      1. Code: package Canine; package Dog; my Canine $spot : Watchful ; Effect: use attributes (); attributes::->import(Canine => \$spot, "Watchful");
Re: using an object as a variable type
by phaylon (Curate) on Jan 27, 2006 at 14:57 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://525966]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2015-07-04 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls