I use Log::Log4perl in a daemon for logging purposes, but that daemon runs other code via system's and qx//'s, so to capture output I redirect their output to a different file.

However, I thought, it would be good to get their STDxxx output into Log4perl too. Looking around it doesn't seem possible, but then I noticed in the 5.8.x open() perldoc you can open an in-memory IO handle on a scalar variable.

Great, I thought, I can tie a scalar to the log4perl warn and info functions and then use open on that scalar to reopen the STDxxx filehandles. Here is the code I have in my tie module:

my %function_for; my %scalar_ref_for; sub TIESCALAR { my ( $class, $function ) = @_; if ( ref $function ne 'CODE' ) { croak('Argument to tie must be a code reference'); } my $self = bless \do { my $anon_scalar }, $class; $function_for{ ident $self } = $function; $scalar_ref_for{ ident $self } = q{}; return $self; } sub STORE { my ( $self, @args ) = @_; $function_for{ ident $self }->(@args); } sub FETCH { my( $self ) = @_; return $scalar_ref_for{ ident $self }; }

Running some tests it works fine with:

my $tied_variable; tie $tied_variable, 'Tie::Scalar::Function' => sub { ok( defined $_[0], "got something + -> $_[0]" ) }; $tied_variable = "assignment";

However, using open against it doesn't work at all:

my $tied_variable; tie $tied_variable, 'Tie::Scalar::Function' => sub { ok( defined $_[0], "got something + -> $_[0]" ) }; warn 'here1:',$tied_variable; open(my $fh, '>', \$tied_variable); print {$fh} 'this is a test'; warn 'here2:',$tied_variable;

$tied_variable does get set by the print on the filehandle but STORE is not called via the tie - somehow its being circumvented.

What am I missing here? I am doing something wrong but I cannot see it...

Thanks

Duncs


In reply to Using a tied scalar as an in memory file by duncs

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.