First off, I want to limit myself to STDOUT, in this case, because I want the impact on the original script to be as small as possible - ie, I'd like it to just be an extra use line that I can insert into each script. But anyways, here is how it all worked out for me:
I could, after some tinkering, accomplish the same thing with Tie::Handle::Scalar, as I do now. I'll just throw up the code right away:
I put this in a module, and used it instead of the original example file - the sub routine is the exact same, so I could compare results.use Tie::Handle::Scalar; tie *STDOUT, 'Tie::Handle::Scalar'; END { my $output; while(<STDOUT>) { $output .= $_; } &on_finish(\$output); untie *STDOUT; print $output; } sub on_finish { my $stdout_ref = shift; # Insert a stylesheet link last in the head section $$stdout_ref =~ s{(</head>)}{<link rel="stylesheet" type="text/css +" href="/css/style.css" />\n$1}i; }
The above code does work, and comparatively, it uses about one third of the code in my module to do the same thing. But I do have some issues with it - you be the judge if I am just defending my code, or if I have any substance in these "complaints". :)
Not big things, I guess, but it does a lot I don't need, in probably too general ways, plus I'd want that wrapper anyways. It could have saved me some POD writing though, if I wanted a one-time solution. It would most certainly work.
IO::String and IO::Stringy I am less certain about. I couldn't get either to do this at all. Possibly because I don't understand the terse docs, but I tried most possible combinations of what was there at least. It is possible that it isn't meant to do this exact thing too, and equally possible I am doing it wrong.
Most things still apply though, it would take an extra wrapper to get it where I want, etc.
Well, I guess my petty "defence" is that I wanted something that did something, that something only, and did it good while being easy to use. If I succeeded is another matter entirely.
I did lots of searches for something that could do this, and I *still* missed those modules, that is almost unbelievable. Especially since I searched the Tie::* modules pretty good. Guess I didn't look closely enough at exactly what modules did.
Again, thanks for pointing these out to me. I'd still stick with what I wrote, now that it is written, but I might have reconsidered had I found this. Not that it matters, writing code is what is fun after all. :)
In reply to Re(2): STDOUT::Capture - manipulate STDOUT
by Dog and Pony
in thread STDOUT::Capture - manipulate STDOUT
by Dog and Pony
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |