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
|
|---|