I like this feature, actually it does make me feel that Perl's OO feature is one step more mature. But ...

One of the functionality, which this 'use fields' feature is supposed to deliver is, to do "a compile time class fields verify". It does delivered this as I will demo, but after a little bit study, I quickly came up a way to demo how this thing is hacked, and how you can cause some inconsistancy.

I have a class defined in Package:
package Package1; use fields qw(attr1 attr2 attr3); sub new { my Package1 $self = shift; $self = fields::new($self) unless ref $self; $self->{attr1} = "init_1"; $self->{attr2} = "init_2"; $self->{attr3} = "init_3"; return $self; } 1;
Then I made this little test1.pl:
use Package1; use Data::Dumper; my Package1 $p = Package1->new; $p->{attr1} = "set_1"; print $p->{attr1}, "\n"; $p->{attr4} = "set_4"; #this should fail, and did fail
This script failed, complaining there is no field attr4 in the pseudo hash, which is good. Now the next test ...test2.pl:
use Package1; use Data::Dumper; my Package1 $p = Package1->new; $p->[0]->{attr4} = 4; $p->[$p->[0]->{attr4}] = "set_4"; print $p->[0]->{attr4};#the above two steps sunccessfully modified the + internal pseudo hash print Dumper($p);#again proves that I successfully modified the intern +al pseudo hash #print $p->{attr4};#this fails
For this second test case, if I uncomment the last print statement, if complains that there is no attr4 in the pseudo hash, although I actually added attr4 already, as you can see in the Data Dumper. On one hand this is good, the checking is still working under ONE definition, (the checking is based on the list I provided in the use field statement). On the other hand, this is really bad, as the data consistancy is now broken. To me, it is crystal clear that, you either make sure that I can not touch the pseudo hash at all, or if you let me touch it, then better make it looks the same from both inside and outside.

I know this is the best they can do on top of the current foundation, but this obviously is something must be improved in the future, maybe in Perl 6.0, after the foundation is re-established.

Hope we will have less HACKED feature in the coming days.

In reply to Re: (When) Should I 'use fields;'? by pg
in thread (When) Should I 'use fields;'? by traveler

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.