Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

use fields;

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

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

Background

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;

Problem

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?

TIA

---
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 (Patriarch) 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=;http://www.manning.com/Conway/index.html'>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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found