Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC PATCH fields.pm 0.02] _private not very private #194

Closed
p5pRT opened this issue Jul 14, 1999 · 1 comment
Closed

[DOC PATCH fields.pm 0.02] _private not very private #194

p5pRT opened this issue Jul 14, 1999 · 1 comment

Comments

@p5pRT
Copy link

p5pRT commented Jul 14, 1999

Migrated from rt.perl.org#1000 (status was 'resolved')

Searchable as RT1000$

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 1999

From @schwern

package Foo;

use fields qw(foo bar _private);

sub new {
  my $proto = shift;
  my $class = ref $proto || $proto;
  bless [\%{"$class\​::FIELDS"}];
}

package Bar;
use base qw(Foo);

sub bar { print shift->{_private} }

package main;

my Foo $obj = Foo->new;

$obj->{foo} = 42;
print $obj->{foo};

$obj->{_private} = 99;
print $obj->{_private};

The result is 42 and 99. The result -should- be 42 and "You tried to
use a private data member from outside Foo, now you die, sucker!" at
least if fields' idea of "Private" is anything like every else's.

Looking at the code of fields.pm, however, I see no easy way to fix
this without making $obj a tied hash (ick, slow) or digging into the
perl source (no thanks). :(

The band-aid solution would be a patch to fields' docs, which I have, below.

Also, it was pointed out to me that Protected data members are a
hellofalot more useful than Private (to use the C++ terms) and it
would be nice if fields implemented Protected as well. Unfortunately,
Protected is the same as Public as long as the above bug is alive, so
there's no point, but I mentioned it as a bug in the doc patch.

*** lib/fields.pm 1999/07/15 00​:03​:04
--- lib/fields.pm 1999/07/15 00​:14​:10
***************
*** 41,49 ****
  and the 'base' pragma modules. The 'base' pragma will copy fields
  from base classes and the 'fields' pragma adds new fields. Field
  names that start with an underscore character are made private to a
! class and are not visible to subclasses. Inherited fields can be
! overridden but will generate a warning if used together with the C<-w>
! switch.
 
  The effect of all this is that you can have objects with named fields
  which are as compact and as fast arrays to access. This only works
--- 41,49 ----
  and the 'base' pragma modules. The 'base' pragma will copy fields
  from base classes and the 'fields' pragma adds new fields. Field
  names that start with an underscore character are made private to a
! class and are not visible to subclasses (however, they are visable by
! users of the class, see L</BUGS>). Inherited fields can be overridden
! but will generate a warning if used together with the C<-w> switch.
 
  The effect of all this is that you can have objects with named fields
  which are as compact and as fast arrays to access. This only works
***************
*** 60,65 ****
--- 60,96 ----
  my $self = bless [\%{"$class\​::FIELDS"}], $class;
  $self;
  }
+
+
+ =head1 BUGS
+
+ =over 4
+
+ =item *
+
+ Private data members aren't very private.
+
+ Unfortunately, this doesn't cause an error.
+
+ my Foo $obj = Foo->new;
+ $obj->{_private} = 42;
+
+ Strictly speaking, no one should be able to access $obj->{_private}
+ except methods in the Foo class. Unfortunately this is not currently
+ enforcable. This is a design flaw and will hopefully be resolved in
+ future versions.
+
+
+ =item *
+
+ Needs Protected data members.
+
+ A Protected data member is that which is inherited, yet not accessable
+ from anywhere outside the inheritence tree. Protected cannot be
+ implemented until the above "Private data members aren't very private"
+ bug is fixed (otherwise they're indistinguishable from Public.)
+
+ =back
 
 
  =head1 SEE ALSO

--

Michael G Schwern schwern@​pobox.com
  http​://www.pobox.com/~schwern
  /(?​:(?​:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant