Does anyone have any views on whether this (a) will work correctly and ?

Sure, why not :)

Does anyone have any views on whether this (b) will be useful?

Um, given the many many many things that already exist, maybe

Can't be faster than restricted hashes ie fields/Hash::Util::lock_keys ... but yeah, fields doesn't force accessors ...

It goes back to freedom, if the user wants to shoot his own foot with direct access, let him, less work for you :)

A test :)

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { eval { my $vmio = Steeve->new( 'ro', 'sham', 'bo', 5 ); dd( { -ro => $vmio->ro, -bo => $vmio->bo } ); dd( $vmio->dump ); $vmio->{ohno} = 4; 1; } or warn $@; eval { my $vmio = Steeve->new( ); dd( $vmio->dump ); $vmio->ro('sham'); $vmio->bo(5); dd( $vmio->dump ); print $vmio->{bo}; 1; } or warn $@; } BEGIN { package VMIOH; $INC{'VMIOH.pm'} = __FILE__; use Variable::Magic qw< wizard cast getdata >; use Carp qw/ croak /; use strict; use warnings; require Exporter; our @ISA = qw/ Exporter /; our @EXPORT = qw/ has dump new /; # exported by default sub has { my $class = caller; for my $attr( @_ ){ croak qq{illegal "$attr"} if not $attr =~ m/^[a-zA-Z][a-zA +-Z0-9]+$/; no strict 'refs'; *{ $class .'::'. $attr } = sub { $#_ ? $_[0]{$attr} = $_[1 +] : $_[0]{$attr} }; } return; } my $protected = sub { croak qq{Attempt to access protected data "$_[2]"} unless caller()->isa( __PACKAGE__ ); }; my $wiz = wizard( store => $protected, fetch => $protected, exists => $protected, delete => $protected, ); sub new { my $class = shift; my %params = @_; my %self; cast %self, $wiz; my $self = \%self; bless $self, $class; $self->$_($params{$_}) foreach keys %params; return $self; } sub dump { return scalar { %{ shift() } }; } 1; } BEGIN { package Steeve; $INC{'Steeve.pm'} = __FILE__; use VMIOH qw/ has new dump /; has 'ro'; has 'bo'; no VMIOH qw/ has /; 1; } __END__ { -bo => 5, -ro => "sham" } { bo => 5, ro => "sham" } Attempt to access protected data "ohno" at - line 12. {} { bo => 5, ro => "sham" } Attempt to access protected data "bo" at - line 21.

In reply to Re: Private & Protected Objects (Variable::Magic) by Anonymous Monk
in thread Private & Protected Objects by Sixes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.