> On Aug 13, 2017, at 6:02 PM, Harry Putnam <[email protected]> wrote:
>
> My aim:
>
> Run certain kinds of log file lines thru a perl script that will:
>
> 1) Identify each line by regex that finds pattern at start of line
> 2) When such a line is found, print newline first then
> 3) wrap any lines longer than specified number of columns.
>
>
> I was not able to divine from `perldoc Text::Wrap' how to really use
> it to do what I want.
>
> my non-working effort stripped to the bare bones:
>
> use strict;
> use warnings;
> use Text::Wrap;
>
> my $rgx = qr/@{[shift]}/;
>
> $Text::Wrap::columns = 68;
>
> my @text;
>
> while (<>) {
> if (/$rgx/) {
> print "\n";
> print wrap(",", @text);
> }
> }
>
> It wasn't at all clear from perldoc Text::Wrap how @text is supposed
> to be populated.
@text is a list of scalar strings passed to the wrap subroutine. You can pass a
single string also. Try this loop instead:
while (<>) {
if (/$rgx/) {
print "\n";
print wrap(",", $_);
}
}
It is usually better to use explicit variables:
while ( my $line = <> ) {
if (/$rgx/) {
print "\n";
print wrap(",", $line);
}
}
Jim Gibson
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/