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

Re: Converting Hashes to Objects

by tobyink (Canon)
on May 18, 2020 at 07:55 UTC ( #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 => '', port => 20001 }, server => object { address => '', 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 (Bishop) on May 18, 2020 at 08:05 UTC

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-09-26 12:24 GMT
Find Nodes?
    Voting Booth?
    If at first I don’t succeed, I …

    Results (141 votes). Check out past polls.