Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Line Numbers

by wnodom (Initiate)
on Jul 01, 2000 at 01:59 UTC ( [id://20710]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Line Numbers

Instead of overloading print, how about redirecting STDOUT with a tied filehandle? Try this:

#! /usr/bin/perl -w

package NumberedPrint;

use strict;

sub new
  {
    my $self = bless {}, shift;
    $self;
  }

sub TIEHANDLE
  {
    shift->new(@_);
  }

{
  my $printed_to_stdout = 0;

  sub PRINT
    {
      shift;
      print ORIGINAL_STDOUT ++$printed_to_stdout, ': ', @_;
    }

  sub PRINTF
    {
      shift;
      print ORIGINAL_STDOUT ++$printed_to_stdout, ': ', sprintf(@_);
    }
}

*ORIGINAL_STDOUT = *main::STDOUT;

tie *main::STDOUT, 'NumberedPrint';

package main;

print "Hello.\n";
print "Hello again.\n";
print "Hello a third time.\n";

print NumberedPrint::ORIGINAL_STDOUT "\nA line without a number.\n";

select NumberedPrint::ORIGINAL_STDOUT;

print "A lot of\n";
print "lines without\n";
print "any numbers.\n";
print "\n";

select STDOUT;

print "The final line.\n";

__END__

This is probably pretty fragile, but may do what you need. I found a similar technique in Lennart Borgman's Win32::ASP::Cgi module, and it seems to work pretty well there (albeit for a totally different purpose).

--Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://20710]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.