<?xml version="1.0" encoding="windows-1252"?>
<node id="349582" title="Streaming to Handles" created="2004-05-01 03:40:50" updated="2005-05-31 06:54:07">
<type id="115">
perlquestion</type>
<author id="319416">
crabbdean</author>
<data>
<field name="doctext">
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.)&lt;br&gt;&lt;br&gt;

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 ...
&lt;CODE&gt;
while (&lt;LOGFILE&gt;) {
            ## do something with $_
}
&lt;/CODE&gt;
OR
&lt;CODE&gt;
foreach ($f-&gt;list) {
            ## do something with $_
}
&lt;/CODE&gt;
... 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.&lt;br&gt;&lt;br&gt;

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.&lt;br&gt;&lt;br&gt;

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)&lt;br&gt;&lt;br&gt; 
&lt;readmore&gt;
My calling program is this:
&lt;CODE&gt;
#!perl

use strict;
use warnings;

push (@INC,'F:/dean/scr/pl/pms/dirlist');
require List;

my $f = List-&gt;new;
$f-&gt;stream_to(\*STDOUT);
$f-&gt;look_in('c:/');

foreach ($f-&gt;list) {  ##problem??
	print "$_\n";
}
&lt;/CODE&gt;
The module is this:
&lt;CODE&gt;
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 @_ &gt; 1;
	$path = cwd unless @_ &gt; 2;
	my $self = {
			stream   =&gt; $stream,
			path    =&gt; File::Spec-&gt;canonpath($path),
			};
	bless $self, $class;
	return $self;
}

sub list {
	my ($self, $dir, $level) = @_;
	$dir = $self-&gt;{path} unless @_ &gt; 1;
	$level = 0 unless @_ &gt; 2;
	if ( opendir( DIR, $dir)) {
		$level++;				
		map {
			my $fullfile = File::Spec-&gt;catfile( $dir, $_ );
			if (-d $fullfile) {
				$self-&gt;list ( $fullfile, $level );
			}
			
			my $str = $self-&gt;{stream};# ref to the stream
			print $str $fullfile;	## problem??
		} File::Spec-&gt;no_upwards( readdir( DIR ) );
	} else {
		warn "$dir : opendir failed: $!\n";
	}
	close DIR;
}

sub stream_to{
	my ($self, $strm) = @_;
	$strm = \*STDOUT unless @_ &gt; 1;
	$self-&gt;{stream} = $strm;
}

sub look_in {
	my ($self, $path) = @_;
	$path = cwd unless @_ &gt; 1;
	$self-&gt;{path} = $path;
}
&lt;/CODE&gt;
&lt;/readmore&gt;
&lt;div class="pmsig"&gt;
&lt;div class="pmsig-319416"&gt;
&lt;br&gt;
[id://319416|Dean]&lt;br&gt;
The Funkster of Mirth&lt;br&gt;
&lt;i&gt;&lt;small&gt;Programming these days takes more than a lone avenger with a compiler. - sam&lt;br&gt;
[http://www.ietf.org/rfc/rfc1149.txt|RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
]&lt;/i&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
