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 919327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:31:33+00:00 2026-05-15T18:31:33+00:00

I have seen code like this package xyz; # putting the module inside its

  • 0

I have seen code like this

package xyz;

# putting the module inside its own pair of braces
{
    public _foo => my %_foo;
    public _bar => my %_bar;

    # some subroutines..

}
1;

My question is what exactly is the meaning of the declaration public _foo => my %_foo;
I mean how is the public keyword used, and why are we using the big arrow to assign another hash?

Any help would be greatly appreciated.

  • 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-15T18:31:34+00:00Added an answer on May 15, 2026 at 6:31 pm

    Oh, are you using Class::InsideOut?

    In this case, it’s not a keyword, it’s actually a subroutine. It sets up a storage variable in the package, to be used as a class attribute.

    Basically, an Inside-Out object is a way to get true encapsulation in Perl. Normally, Perl objects store their values inside a hash reference, which is then returned to the user, like this:

    package Foo;
    use strict;
    use warnings;
    
    sub new {
        my ($class, %args) = @_;
        return bless {
            foo => $args{foo},
            bar => $args{bar},
        }, $class;
    }
    
    sub get_foo {
        my $self = shift;
        return $self->{foo};
    }
    
    sub get_bar {
        my $self = shift;
        return $self->{bar};
    }
    
    1;
    

    As you can see, the details of what the class holds actually get returned to the user! They can muck about with it all they want without using accessors. Whups.

    Inside out objects use the fact that lexical variables inside a package (such as my variables) can not EVER be seen outside of their scope. It uses fact that every unique scalar reference has an address that is unique, giving us a unique key into the hash, allowing us to have a unique, restricted stash for class data. The basic pattern looks like this:

    package Foo;
    use strict;
    use warnings;
    use Scalar::Util qw/refaddr/;
    {
        my %_foo_of;
        my %_bar_of;
    
        sub new {
            my ($class, %args) = @_;
            # create a reference to an anonymous scalar.
            my $instance = \do { my $anon_scalar }; 
            $_foo_of{ refaddr $instance } = $args{foo};
            $_bar_of{ refaddr $instance } = $args{bar};
            return $instance;
        }
    
        # we have to manually delete these since they don't go out of scope
        # automatically like standard Perl classes do!
        sub DESTROY {
            my $self = shift;
            delete $_foo_of{ refaddr $self };
            delete $_bar_of{ refaddr $self };
        }
    
        sub get_foo {
            my $self = shift;
            return $_foo_of{ refaddr $self };
        }
    
        sub get_bar {
            my $self = shift;
            return $_bar_of{ refaddr $self };
        }
    }
    
    1;
    

    Ewwww. Lots of boilerplate. Lots more template work. Annoying.

    Now lets look at it with Class::InsideOut.

    package Foo;
    use strict;
    use warnings;
    use Class::InsideOut qw/new public/;
    
    public foo => my %foo;
    public bar => my %bar;
    
    1;
    

    DONE!

    You can make a more complex new subroutine if you want, but this automatically sets up a default new subroutine that initializes foo and bar from named arguments.

    Class::InsideOut makes it so you don’t have to do that boilerplate stuff. It makes your accessors, it makes your anonymous scalar for you, it makes your destructor, and gives it you the nice id keyword instead of refaddr. Other than that, everything is the same. It just gets rid of some of the boilerplate stuff you normally have to deal with, making inside out classes easy (and fun!) to use.

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

Sidebar

Related Questions

In our application, I have seen code written like this: User.java (User entity) public
We have several areas where code like this can be seen public Map extractData(ResultSet
Many times I have seen code where they write a Class like this: public
I have seen code like this (actually seeing another person type it up): catch
I have seen in some source code (by other developers) something like this: #import
I have seen a lot of code like this header.h @interface Foo : NSObject
I have seen code like this: struct failed_login_res { string errorMsg<>; unsigned int error;
I have seen example code like this before class C { C(); ~C(); foo(T1,
I have seen code in lua like this if (a==b or false) then what
I have seen this web beacon image here The code looks like this: header(

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.