Return to Snippet

Revision: 283
at July 9, 2006 21:11 by kkindoh


Updated Code
## Define three types printers: 1.Pipe to less, 2.Output to a file 3.Just print
##
##

   sub getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  return $pout->print( $line ) if defined $line;
                  $pout->close(); # undef to close
            } # closure     
   } # getLessPrinter

   sub getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> $file" ) or croak "Failed to create file: $file: $!\n";

     return sub { my $line= shift;
                  return $fout->print( $line ) if defined $line;
                  $fout->close(); # undef to close
            } # closure     
   } # getFilePrinter

  sub getNoLessPrinter {

     return sub { my $line= shift;
                  print $line if defined $line;
            } # closure     
  } # getNoLessPrinter


  ## Then select one of the printer
  ##
  ## my $printer= getFilePrinter( "output.txt");
  ##    or
  ## my $printer= getNoLessPrinter();
  ##    or
  my $printer= getLessPrinter();

  $printer->( "Something to print.\n");
  $printer->( undef ); # to close the stream if needed.

Revision: 282
at July 9, 2006 21:10 by kkindoh


Updated Code
sub getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  return $pout->print( $line ) if defined $line;
                  $pout->close(); # undef to close
            } # closure     
   } # getLessPrinter

   sub getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> $file" ) or croak "Failed to create file: $file: $!\n";

     return sub { my $line= shift;
                  return $fout->print( $line ) if defined $line;
                  $fout->close(); # undef to close
            } # closure     
   } # getFilePrinter

  sub getNoLessPrinter {

     return sub { my $line= shift;
                  print $line if defined $line;
            } # closure     
  } # getNoLessPrinter


  ## Then select one of the printer
  ##
  ## my $printer= getFilePrinter( "output.txt");
  ##    or
  ## my $printer= getNoLessPrinter();
  ##    or
  my $printer= getLessPrinter();

  $printer->( "Something to print.\n");
  $printer->( undef ); # to close the stream if needed.

Revision: 281
at July 9, 2006 21:09 by kkindoh


Updated Code
sub getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  return $pout->print( $line ) if defined $line;
                  $pout->close(); # undef to close
            } # closure     
   } # getLessPrinter

   sub getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> $file" ) or croak "Failed to create file: $file: $!\n";

     return sub { my $line= shift;
                  return $fout->print( $line ) if defined $line;
                  $fout->close(); # undef to close
            } # closure     
   } # getFilePrinter

  sub getNoLessPrinter {

     return sub { my $line= shift;
                  print $line if defined $line;
            } # closure     
  } # getNoLessPrinter


  ## Then select one of the printer
  ##
  ## my $printer= getFilePrinter( "output.txt");
  ##    or
  ## my $printer= getNoLessPrinter();
  ##    or
  my $printer= getLessPrinter();

  $printer->( "Something to print.\n");
  $printer->( undef ); # to close the stream if needed.

Revision: 280
at July 9, 2006 21:09 by kkindoh


Updated Code
sub getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  return $pout->print( $line ) if defined $line;
                  $pout->close(); # undef to close
            } # closure     
   } # getLessPrinter

   sub getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> $file" ) or croak "Failed to create file: $file: $!\n";

     return sub { my $line= shift;
                  return $fout->print( $line ) if defined $line;
                  $fout->close(); # undef to close
            } # closure     
   } # getFilePrinter

  sub getNoLessPrinter {

     return sub { my $line= shift;
                  print $line if defined $line;
            } # closure     
  } # getNoLessPrinter


  ## Then select one of the printer
  ##
  ## my $printer= getFilePrinter( "output.txt");
  ##    or
  ## my $printer= getNoLessPrinter();
  ##    or
  my $printer= getLessPrinter();

  $printer->( "Something to print.\n");
  $printer->( undef ); # to close the stream if needed.

Revision: 279
at July 9, 2006 12:54 by kkindoh


Updated Code
sub Line::getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  return $pout->print( $line ) if defined $line;
                  $pout->close(); # undef to close
            } # closure     
   } # Line::getLessPrinter

   sub Line::getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> $file" ) or croak "Failed to create file: $file: $!\n";

     return sub { my $line= shift;
                  return $fout->print( $line ) if defined $line;
                  $fout->close(); # undef to close
            } # closure     
   } # Line::getFilePrinter

   sub Line::getNoLessPrinter {

     return sub { my $line= shift;
                  print $line if defined $line;
            } # closure     
   } # Line::getNoLessPrinter


   ## Then select one of the printer
   ##
   ## my $printer= getFilePrinter( "output.txt");
   ##    or
   ## my $printer= getNoLessPrinter();
   ##    or
   my $printer= getLessPrinter();

   $printer->( "Something to print.\n");
   $printer->( undef ); # to close the stream if needed.

Revision: 278
at July 8, 2006 13:28 by kkindoh


Updated Code
sub getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  $pout->print( $line );
                  $pout->close() if $line =~ / BOTTOM /;
            } # closure     
   } # getLessPrinter

   sub getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> $file" ) or croak "Failed to create file: $file: $!\n";

     return sub { my $line= shift;
                  $fout->print( $line );
                  $fout->close() if $line =~ / BOTTOM /;
            } # closure     
   } # getFilePrinter

   sub getNoLessPrinter {

     return sub { my $line= shift;
                  print $line;
            } # closure     
   } # getNoLessPrinter

   ## Then select one of the printer
   ##
   my $printer= getLessPrinter();

   $printer->( "Something to print.\n");
   $printer->( "BOTTOM\n"); # print it and close it.

Revision: 277
at July 8, 2006 12:59 by kkindoh


Initial Code
sub getLessPrinter {
     my $pout= IO::File->new( "| less" ) or croak "Failed to create less pipe\n";

     return sub { my $line= shift;
                  $pout->print( $line );
                  $pout->close() if $line =~ / BOTTOM /;
            } # closure     
   } # getLessPrinter

   sub getFilePrinter {
     my $file= shift;
     my $fout= IO::File->new( "> ". Line::output() ) or croak "Failed to create file:". Line::output() .": $!\n";

     return sub { my $line= shift;
                  $fout->print( $line );
                  $fout->close() if $line =~ / BOTTOM /;
            } # closure     
   } # getFilePrinter

   sub getNoLessPrinter {

     return sub { my $line= shift;
                  print $line;
            } # closure     
   } # getNoLessPrinter

   ## Then select one of the printer
   ##
   my $printer= getLessPrinter();

   $printer->( "Something to print.\n");
   $printer->( "BOTTOM\n"); # print it and close it.

Initial URL

                                

Initial Description

                                

Initial Title
Closure to change output

Initial Tags

                                

Initial Language
Perl