I'm having a requirement to write a function, that can take a reference, and return me all given object references in that value. The function should do a deep search of the given reference (which could be a hash reference or an object), and return an array of objects. Before I go ahead, I would like to meditate on the Monastry Gates and seek the wisdom of monks; if a solution already exists, or a better solution is available.

Background: I have a requirement, that goes like this:


Object Log, when dumped, looks like this:
$VAR1 = bless({'options' => {'filehandle' => bless( \*Symbol::GEN2, 'FileHandle' ), 'filename' => 'a.log'},}, 'Log' );
Log object provides the methods filehandle() and filename() to set the above values (and ofcourse, a $log->print()). I have a large codebase, that I'm modifying to enable parallel processing of various parts, with minimum changes to the codebase. So the call:
$self->do_task()
becomes:
Process::spawn(\&do_task, $self);
The problem is, $self has references to the log object at various places. I want all the output of the spawned process to go to a new file. So I do something like this:
$new_log = new Log(filename => 'new.log'); $cur_log = $self->{log}; $self->{log} = $new_log; Process::spawn(\&do_task, $self); # This runs in background. $self->{log} = $cur_log; # Restore.
But alas !, $self also has various other references to log, embedded deep inside, like:
$self->{system}->{log}, $self->{filecache}->{system}->{log}
...etc. Since these log objects are created in the parent, at various points in the code, they all refer to the parent filehandle. I cannot change the parent code; So I need to do some gimmick to find out all these references to Log and replace them with $new_log.
So I plan to write something like Devel::ObjSearch, that can be used as:
@ref = Devel::ObjSearch($self, sub { return UNIVERSAL::isa($_[0], 'Log'); }); foreach (@ref) { $_->filename('new.log'); $_->filehandle($new_log); } .. spawn() ..
Anybody can provide a more enlightened solution ? Any hints to how I can develop Devel::ObjSearch ? (I plan to steal code from Data::Dumper, or implement a hook in dumper, if possible).
thanks in advance.

In reply to searching for an object reference by Anonymous Monk

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.