McSvenster:

Since your object is basically the hash you want with a footnote (the class it's blessed into), you can send the data to elasticsearch just like you'd do with a standard hash. There are a couple of caveats, though:

First: if the elasticsearch API wants a hash and tries to verify that it's a hash with ref, then it may complain because your object isn't just a hash. Second, if the elasticsearch function(s) twiddle the hash, your object will be similarly twiddled. Finally, if the elasticsearch hangs on to the reference, it may bonk your object at some point in the future (alternatively, if you change your object, then elasticsearch could get confused by the change). So if the API is going to play nice, you can simply pass it your object, but if you have any doubts, you can just put in a copy. Here's a quickie example to show what I mean:

$ cat flog.pl #!/usr/bin/env perl use strict; use warnings; my $o = bless {a=>1,b=>2,c=>3}, 'Flog'; my $j = { J => { %$o }, # Make a copy of the object K => $o # or just pass it in }; print "Before:\n"; dumpit($j); $j->{J}{a}=7; $j->{K}{b}=8; $o->{c}=9; print "After:\n"; dumpit($j); sub dumpit { my $t = shift; for my $k ('a' .. 'c') { print "$k : $t->{J}{$k} : $t->{K}{$k} : $o->{$k}\n"; } } Roboticus@Waubli ~ $ perl flog.pl Before: a : 1 : 1 : 1 b : 2 : 2 : 2 c : 3 : 3 : 3 After: a : 7 : 1 : 1 b : 2 : 8 : 8 c : 3 : 9 : 9

In general, I'd suggest sending in a copy unless you *want* the object to be passed around. I mention elasticsearch only because you did. I don't know a thing about it, so I don't know how its API is going to treat your object.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: beginner's question regarding objects by roboticus
in thread beginner's question regarding objects by McSvenster

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.