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

Converting Hashes to Objects

by haukex (Archbishop)
on May 17, 2020 at 13:13 UTC ( [id://11116851]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    use Util::H2O;
    my $hash = h2o { foo => "bar", x => "y" }, qw/ more keys /;
    print $hash->foo, "\n";  # prints "bar"
    $hash->x("z");  # setter
    
  2. or download this
    my $struct = { hello => { perl => { world => "yay" } } };
    h2o -recurse, $struct;
    print $struct->hello->perl->world, "\n";  # prints "yay"
    
  3. or download this
    my $obj = h2o -meth, {
        one => 123, two => 456,
    ...
            return $self->one + $self->two;
        } };
    print $obj->sum, "\n";  # prints "579"
    
  4. or download this
    h2o -class=>'Point', -new, -meth, {
            angle => sub { my $self = shift; atan2($self->y, $self->x) },
    ...
    my $two = Point->new(x=>3, y=>4);
    $two->y(5);
    printf "%.4f\n", $two->angle;  # prints 1.0304
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11116851]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found