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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:54:39+00:00 2026-05-16T04:54:39+00:00

My goal is to be able to use $obj like this: print $obj->hello() .

  • 0

My goal is to be able to use $obj like this:

print $obj->hello() . $obj->{foo};

And I would like to create an object inline, maybe using something like this:

my $obj = (
    foo => 1,
    hello => sub { return 'world' }
);

but when I try to use $obj as an object, I get an error saying that $obj has not been blessed. Is there some base class (like stdClass in PHP) I can use to bless the hash so that I can use it as an object?


For those that know JavaScript, I am trying to do the following, but in Perl:

# JS CODE BELOW
var obj = { foo: 1, hello: function () { return 'world' } };
echo obj.hello() + obj.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-16T04:54:40+00:00Added an answer on May 16, 2026 at 4:54 am

    Perl would require a little help to do this. Because it doesn’t consider code references stored in hashes as “methods”. Methods are implemented as entries into a package symbol table. Perl is more class-oriented than JavaScript, which proudly proclaims that it is more object-oriented (on individual objects).

    In order to do that functionality, you would have to create a class that mapped references in this way. The way to get around methods in the symbol table is the AUTOLOAD method. If a package contains an AUTOLOAD subroutine, when a call is made to a blessed object that Perl cannot find in the inheritance chain, it will call AUTOLOAD and set the package-scoped (our) variable $AUTOLOAD will contain the full name of the function.

    We get the name of the method called, by getting the last node (after the last ‘::’) of the fully-qualified sub name. We look to see if there is a coderef at that location, and if there is, we can return it.

    package AutoObject;
    
    use strict;
    use warnings;
    use Carp;
    use Params::Util qw<_CODE>;
    our $AUTOLOAD;
    
    sub AUTOLOAD {
        my $method_name = substr( $AUTOLOAD, index( $AUTOLOAD, '::' ) + 2 );
        my ( $self )    = @_;
        my $meth        = _CODE( $self->{$method_name} );
        unless ( $meth ) { 
            Carp::croak( "object does not support method='$method_name'!" );
        }
        goto &$meth;
    }
    
    
    1;
    

    Then you would bless the object into that class:

    package main;
    
    my $obj 
        = bless { foo => 1
          , hello => sub { return 'world' }
          }, 'AutoObject';
    
    print $obj->hello();
    

    Normally, in an AUTOLOAD sub I “cement” behavior. That is, I create entries into the package symbol table to avoid AUTOLOAD the next time. But that’s usually for a reasonably defined class behavior.

    I also designed a QuickClass which creates a package for each object declared, but that contains a lot of symbol table wrangling that now days is probably better done with Class::MOP.


    Given the suggestion by Eric Strom, you could add the following code into the AutoObject package. The import sub would be called anytime somebody use-d AutoObject (with the parameter 'object').

    # Definition:
    sub object ($) { return bless $_[0], __PACKAGE__; };
    
    sub import { # gets called when Perl reads 'use AutoObject;'
        shift; # my name
        return unless $_[0] eq 'object'; # object is it's only export
        use Symbol;
        *{ Symbol::qualify_to_reference( 'object', scalar caller()) }
            = \&object
            ;
    }
    

    And then, when you wanted to create an “object literal”, you could just do:

    use AutoObject qw<object>;
    

    And the expression would be:

    object { foo => 1, hello => sub { return 'world' } };
    

    You could even do:

    object { name  => 'World'
           , hello => sub { return "Hello, $_[0]->{name}"; } 
           }->hello()
           ;
    

    And you have an “object literal” expression. Perhaps the module would be better called Object::Literal.

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

Sidebar

Related Questions

Hello so far been trying to use this line of code my goal is
The end goal of this part of my project is to be able to
Goal I am building an Eclipse plugin targeting the 3.7 environment and would like
My ultimate goal is to be able to make use of additional mouse buttons
My goal is to be able to use HttpUnit to crawl around my webpage
My goal is to be able to create a soap request that can contain
My overall goal is to be able to create a pixel shader that takes
I'd like to be able to use php search an array (or better yet,
My end goal is for my site to be able to use Facebook API
I would like to use an objects property as the key for a dictionary.

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.