Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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; }

Dean
The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

In reply to Streaming to Handles by crabbdean

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (5)
    As of 2014-09-22 10:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (188 votes), past polls