I'm writing code that attempts to output data to a filehandle, either STDOUT or whatever is specified. I want it to stream the output so that in your calling script you can work with each line of output as it comes out. The below code gives the guts of everything (more a watered-down version so you get an idea of my problem.)
My problem is that my module that does all the work prints the output to the specified HANDLE, but you can't work with the output. eg. If you output to LOGFILE in my calling the normal structure of ...
while (<LOGFILE>) {
## do something with $_
}
OR
foreach ($f->list) {
## do something with $_
}
... doesn't work. The reason being is that my module outputs straight to the output file handle insteading of returing it to my main calling script.
In the example given you can see the section I've marked as "##problem??" doesn't output each entry with a newline character. The output is being directly spat out from the module.
How do I capture the output as an input stream to my calling program? What am I doing wrong? Is this making sense? (see code in the readmore section)
My calling program is this:
#!perl
use strict;
use warnings;
push (@INC,'F:/dean/scr/pl/pms/dirlist');
require List;
my $f = List->new;
$f->stream_to(\*STDOUT);
$f->look_in('c:/');
foreach ($f->list) { ##problem??
print "$_\n";
}
The module is this:
package List;
use strict;
use warnings;
use vars qw(@ISA @EXPORT_OK $VERSION);
use File::Spec qw(catfile);
use Carp;
use Cwd qw(cwd abs_path);
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(list);
$VERSION = '0.99';
######################################################################
# object interface
sub new {
my ($class, $stream, $path, ) = @_;
$stream = \*STDOUT unless @_ > 1;
$path = cwd unless @_ > 2;
my $self = {
stream => $stream,
path => File::Spec->canonpath($path),
};
bless $self, $class;
return $self;
}
sub list {
my ($self, $dir, $level) = @_;
$dir = $self->{path} unless @_ > 1;
$level = 0 unless @_ > 2;
if ( opendir( DIR, $dir)) {
$level++;
map {
my $fullfile = File::Spec->catfile( $dir, $_ );
if (-d $fullfile) {
$self->list ( $fullfile, $level );
}
my $str = $self->{stream};# ref to the stream
print $str $fullfile; ## problem??
} File::Spec->no_upwards( readdir( DIR ) );
} else {
warn "$dir : opendir failed: $!\n";
}
close DIR;
}
sub stream_to{
my ($self, $strm) = @_;
$strm = \*STDOUT unless @_ > 1;
$self->{stream} = $strm;
}
sub look_in {
my ($self, $path) = @_;
$path = cwd unless @_ > 1;
$self->{path} = $path;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.