Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Restricting a scalar to certain values

by tobyink (Abbot)
on Jun 20, 2013 at 08:56 UTC ( #1039921=note: print w/replies, xml ) Need Help??


in reply to Restricting a scalar to certain values

Type::Tie can be used to tie a scalar, array or hash variable to a type constraint. It supports Type::Tiny, Moose::Meta::TypeConstraint and MooseX::Types type constraints (and probably Mouse::Meta::TypeConstraint and MouseX::Types, though these are untested).

#!/usr/bin/env perl use strict; use warnings; use Type::Utils qw(enum); use Type::Tie; my $scalar; ttie $scalar, enum ["foo", "bar"]; $scalar = "foo"; # ok $scalar = "bar"; # ok $scalar = "baz"; # dies

Update: note though that tied scalars are significantly slower than untied. So an idea might be to tie your scalars in your development environment, and keep them untied in production. This can be achieved using something like:

use constant DEBUG => !!$ENV{DEVELOPMENT_SERVER}; my $scalar; ttie $scalar, enum ["foo", "bar"] if DEBUG;
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039921]
help
Chatterbox?
[Veltro]: marto depends on how you define app. Doing the processing somewhere else, so it is not the same app? I don't think like that anymore.
[Corion]: Yeah, but calling some random cloud service is not really implementing that stuff yourself :) Also, I don't like to litter the world with my personal data, or random bits of recorded audio... Ideally, my computations never leave my network :)
[Veltro]: ok, but nm. I meant the solution as a whole actually. Not sincerely the apps that use it
[marto]: and not long ago it wasn't feasible for anyone really to create a finger print of all known music to compare samples against...
[marto]: if you're living in the cloud, you're going to get rained on :P

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2018-05-24 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?