Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I work a lot with complex data structures. When I need to access a value at some depth nested keys which not guaranteed to exist, I have to write,
sub somefunc { my($self, $context) = @_; return 1 # assumed just OK, but no further processing unless $self->{config}{key1} && $self->{config}{key1}{context} && $self->{config}{key1}{context}{$context} && $self->{config}{key1}{context}{$context}{form}; my $form = $self->{config}{key1}{context}{$context}{form}; # I can work with $form ... }
in order to avoid autovivification. But I'm getting tired to do that in all over the file, for every Perl file I work on, with distinct CDSes. Once I thought that I might need to restructure my data, but it wasn't possible to do so and it's still not possilbe to restructure now. So I come up with this little tool after doing some searching with Super Search and CPAN without satisfying result.
sub is_hash { my($hash, @keys) = @_; return unless defined $hash && ref($hash) eq 'HASH'; return $hash unless @keys; my $yes; for (@keys) { $yes = undef, last unless ref $hash eq 'HASH' && %{ $hash }; $yes = undef, last unless exists $hash->{$_} && defined $hash->{$_}; $yes = 1; $hash = $hash->{$_}; } return $yes ? $hash : $yes; }
Now I can write,
return 1 unless my $form = is_hash($self->{config}, 'key1', 'context', $conte +xt, 'form');
It's enough for my needs now. It can be extended further to also allow array references in the middle of the chain,
$somedata, $key1, $key2, $idx3, $key4 ....;

Or, to let us specify certain ref type (including non-ref) we want so it terminates if the ref type mismatches,

my $wanted = 'CODE'; # if key3 has a value but not a CODE ref, it croaks my $code = thefunc($somedata, $wanted, 'key1', 'key2', 'key3'); $code->(@someargs);

So, if you were on my shoes, was it worth for you?


Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Preventing autovivification while accessing value by naikonta

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 meditating upon the Monastery: (3)
As of 2024-04-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found