Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
my %vars  = %$rvars;

A note on this: When you do this, you're creating a (shallow) copy of the hash, and if $rvars happens to be a Util::H2O object, you'd lose its methods. If that's not what you want, you'll have to work with the hash reference directly. In newer versions of Perl (>=5.22), there is an experimental feature that allows aliasing:

use experimental 'refaliasing'; my $rvars = { foo=>"bar" }; \my %vars = $rvars; # alias print $vars{foo}, "\n"; # prints "bar" $vars{abc} = "xyz"; # modifies $hashref's contents

But since it's experimental, it's probably better to stick with $rvars->{foo} instead.

Q1) How is a "setter" to be implemented?

In the context of Util::H2O, getters/setters are created for every key that exists in the hash or is given as an "additional key" at the time of the h2o call. So for your $hash->x("z") to work, there'd need to be a key x in the hash or you need to specify it to the h2o function; you don't need to write your own sub x.

Q2) Do I still have to pass this around as clumsily as I am with these hash references now.

Q3) Is there now a better way to do this?

TIMTOWTDI, here's how I might have written it without the module:

use Path::Tiny; use Time::Piece; my $ref_var = { abs => path(__FILE__)->absolute, cwd => Path::Tiny->cwd, }; init_vars($ref_var); print $ref_var->{save_file}, "\n"; sub init_vars { my $rvars = shift; # ... $rvars->{save_file} = path( $rvars->{cwd}, "games", localtime->strftime("%d-%m-%Y-%H-%M-%S.txt") )->touchpath; # ... }

And with the module, one can write:

use Path::Tiny; use Time::Piece; use Util::H2O; my $ref_var = h2o { abs => path(__FILE__)->absolute, cwd => Path::Tiny->cwd, }, qw/ save_file ... /; init_vars($ref_var); print $ref_var->save_file, "\n"; sub init_vars { my $rvars = shift; # ... $rvars->save_file( path( $rvars->cwd, "games", localtime->strftime("%d-%m-%Y-%H-%M-%S.txt") )->touchpath ); # ... }

In reply to Re^2: Converting Hashes to Objects by haukex
in thread Converting Hashes to Objects by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found