Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Converting Hashes to Objects

by tobyink (Canon)
on May 18, 2020 at 07:55 UTC ( [id://11116886]=note: print w/replies, xml ) Need Help??


in reply to Converting Hashes to Objects

I wrote Object::Adhoc recently which is pretty similar.

my $obj = object { foo => 1, bar => 2 };

Main differences are that mine doesn't work recursively, and mine also generates has_foo methods.

If you need nested objects, my solution would be to just be explicit:

my $connection = object { client => object { address => '10.0.0.1', port => 20001 }, server => object { address => '10.0.0.2', port => 25 }, }; say $connection->server->address;

As far as adding things like constructors and methods, I consider that beyond the scope of Object::Adhoc; use a proper class builder for that. Object::Adhoc is just for those cases where you want a sub to return something dict/struct-like.

object { ... } is conceptually a quote-like operator.

Replies are listed 'Best First'.
Re^2: Converting Hashes to Objects
by haukex (Archbishop) on May 18, 2020 at 08:05 UTC

    Nice, thanks! I've added references to the docs.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found