Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

use fields;

by nite_man (Deacon)
on Nov 23, 2004 at 07:15 UTC ( #409825=perlquestion: print w/replies, xml ) Need Help??

nite_man has asked for the wisdom of the Perl Monks concerning the following question:


I've developed a couple of Perl classes using paradigma 'fields'. I've needed it because it's so easy to inherit, add new properties and it doesn't need to rewrite object constructor and inizializator.
I create object like that:

no strict "refs"; my $self = bless [\%{"${caller}::FIELDS"}], $class;


All worked perfectly till Perl 5.8.0 was released. I have warning "Pseudo-hashes are deprecated at ..." when I try to run my script which uses developed classes under Perl 5.8.0. I know that using fields is going to be deprecated.

So, my question is how can use fields now?


Michael Stepanov aka nite_man

It's only my opinion and it doesn't have pretensions of absoluteness!

Replies are listed 'Best First'.
Re: use fields;
by Corion (Pope) on Nov 23, 2004 at 07:19 UTC

    Is there a reason why you are using that weird syntax instead of the way proposed in the SYNOPSIS section of the documentation ?

    package Bar; use base 'Foo'; use fields qw(baz _Bar_private); # not shared with Foo sub new { my $class = shift; my $self = fields::new($class); $self->SUPER::new(); # init base fields $self->{baz} = 10; # init own fields $self->{_Bar_private} = "this is Bar's secret"; return $self; }

    Of course, doing it that way means you need to chain to the previous constructor manually, but I didn't see any chaining in your (more generic?) constructor.

      Thanks, Corion, you are right, there is not any reason to use the weird syntax. I found it two years ago in the book <a href=;'>Object Oriented Perl by Damian Conway (in my opinion, that book is one of the best Perl books). Probably, that syntax became old. Unfortunately, using
      my $self = fields::new($class);
      intead of
      no strict "refs"; my $self = bless [\%{"${caller}::FIELDS"}], $class;
      doen't resolve problem with pseudo-hashes.

      Michael Stepanov aka nite_man

      It's only my opinion and it doesn't have pretensions of absoluteness!

        Re-reading the documentation, Perl 5.8.x still uses the pseudo-hash implementation, so I guess you will have to temporarily switch off the warnings for that section of code:

        my $self = do { no warnings 'deprecated'; fields::new($class); };

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2021-06-18 03:00 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (87 votes). Check out past polls.