Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 829237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:51:02+00:00 2026-05-15T03:51:02+00:00

I’m trying to understand the behavior of the fields pragma, which I find poorly

  • 0

I’m trying to understand the behavior of the fields pragma, which I find poorly documented, regarding fields prefixed with underscores. This is what the documentation has to say about it:

Field names that start with an underscore character are made private to the class and are not visible to subclasses. Inherited fields can be overridden but will generate a warning if used together with the -w switch.

This is not consistent with its actual behavior, according to my test, below. Not only are _-prefixed fields visible within a subclass, they are visible within foreign classes as well (unless I don’t get what ‘visible’ means). Also, directly accessing the restricted hash works fine.

Where can I find more about the behavior of the fields pragma, short of going at the source code?

{
    package Foo;
    use strict;
    use warnings;
    use fields qw/a _b __c/;

    sub new {
        my ( $class ) = @_;
        my Foo $self = fields::new($class);
        $self->a = 1; $self->b = 2; $self->c = 3;
        return $self;
    }

    sub a : lvalue { shift->{a}   }
    sub b : lvalue { shift->{_b}  }
    sub c : lvalue { shift->{__c} }
}
{
    package Bar;
    use base 'Foo';
    use strict;
    use warnings;
    use Data::Dumper;

    my $o = Bar->new;
    print Dumper $o; ##$VAR1 = bless({'_b' => 2, '__c' => 3, 'a' => 1}, 'Foo');

    $o->a = 4; $o->b = 5; $o->c = 6;
    print Dumper $o; ##$VAR1 = bless({'_b' => 5, '__c' => 6, 'a' => 4}, 'Foo');

    $o->{a} = 7; $o->{_b} = 8; $o->{__c} = 9;
    print Dumper $o; ##$VAR1 = bless({'_b' => 8, '__c' => 9, 'a' => 7}, 'Foo');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:51:02+00:00Added an answer on May 15, 2026 at 3:51 am

    Coincidentally enough, I happen to have a test script in ~/codescraps/fields/test.pl dated from two years ago when I experimented with answering exactly this same question. 🙂

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    {
       package Foo;
       use fields qw(foo bar _Foo_private);
       use private qw(_really_private);
       sub new {
           my Foo $self = shift;
           unless (ref $self) {
               $self = fields::new($self);
               $self->{_Foo_private} = "this is Foo's secret";
           }
           $self->{foo} = 10;
           $self->{bar} = 20;
           return $self;
       }
    }
    
    my $foo = Foo->new;
    $foo->{foo} = 42;
    
    # this will generate an error: field does not exist
    #$foo->{zap} = 42;
    
    print "_Foo_private: " . $foo->{_Foo_private} . "\n";
    $foo->{_Foo_private} = 1;
    print "_Foo_private: " . $foo->{_Foo_private} . "\n";
    
    print "_really_private: " . $foo->{_really_private} . "\n";
    $foo->{_really_private} = 1;
    print "_really_private: " . $foo->{_really_private} . "\n";
    
    print Dumper($foo);
    
    # subclassing
    {
       package Bar;
       use base 'Foo';
       use fields qw(baz _Bar_private);        # these fields 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;
       }
    }
    
    my $bar = Bar->new;
    # these work fine
    $bar->{foo} = 1;
    $bar->{bar} = 1;
    $bar->{_Bar_private} = 1;
    
    # this will not work - underscored fields are not visible to children
    $bar->{_Foo_private} = 1;
    

    And when I run your code, I get the error:

    No such pseudo-hash field "_b" at test2.pl line 16.
    

    (line 16 is the definition for sub b.) What architecture are you running this on? Objects using the fields pragma aren’t simple blessed hashrefs — they are blessed arrayrefs, e.g. when I modify your constructor to look like this:

    sub new {
        my ( $class ) = @_;
        my Foo $self = fields::new($class);
        $self->{a} = 1; $self->{_b} = 2; $self->{__c} = 3;
        print "I look like: ", Data::Dumper::Dumper($self);
        return $self;
    }
    

    I see:

    I look like: $VAR1 = bless( [
                     bless( {
                              'a' => 1
                            }, 'pseudohash' ),
                     1,
                     2,
                     3
                   ], 'Bar' );
    

    As a postscript, I feel obliged to point out that the fields pragma, and the base pragma that goes with it, are both deprecated and one is strongly urged to avoid using them. Nowadays, if you’re looking to construct a nice OO module with accessors, one would either use Class::Accessor or go directly to Moose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.