Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello all,

Whenever possible I use a subset of a project that I am working on to learn some new element of perl. This winds up being a really great way to improve my perl-Foo but can on occaision lead to a massive time sink with no actual result.

Currently I have hit an impasse on my latest 'I wonder if I can do that' and I would like some feedback if possible. I'm trying to open STDOUT to a subroutine reference and I'm intrigued by the fact that it doesn't die but it also doesn't behave the way I had hoped. I also wonder if it's significant that the variable is still a CODE ref even after I use it in the open statement.

use Modern::Perl; print "Hello World 1\n"; my $sub_ref = sub{ warn join( ' ', @_ ) . " - this is really bad" }; open( STDOUT, ">>", $sub_ref ) or die "open failed: " . $!; print "Hello World 2"; warn ref $sub_ref;

results in the output

Hello World 1 CODE at line 8.

I would like to get

Hello World 1 Hello World 2 - this is really bad at line 3 CODE at line 8.

Any help or even don't go there's with an explanation would be helpful. I am aware of tee and pipe and although tie seemed intriguing I couldn't get it to tie \*STDOUT to a CODE ref at all.

In reply to open STDOUT to a subroutine reference? by jandrew

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (3)
    As of 2018-05-21 02:21 GMT
    Find Nodes?
      Voting Booth?