Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
A question was posed to me about using substitution to perform concatenation -- more as a question as to whether substitution is flexible enough to do so rather than is this the best way to use the tools Perl provides.
So, I wrote the following as an innocuous brute force attempt:
However, the output is:die 'insufficient arguments' unless @ARGV == 1; open(IN, $ARGV[0]) or die "unable to open '$ARGV[0]'"; while (my $line = <IN>) { my $prefix = sprintf '%4d', $.; $line =~ s/$line/$prefix: $line/; print $line; } close IN;
Adding use strict; modifies the output to:$ perl test.pl test.pl 1: die 'insufficient arguments' unless @ARGV == 1; open(IN, $ARGV[0]) or die "unable to open '$ARGV[0]'"; while (my $line = <IN>) { my $prefix = sprintf '%4d', $.; $line =~ s/$line/$prefix: $line/; print $line; 7: } 8: close IN; $
Can you help elucidate why these results are what is shown above? I see the same behavior using ActiveState 5.8.4 and 5.6 on FreeBSD 4.11.$ perl test.pl test.pl 1: use strict; 2: die 'insufficient arguments' unless @ARGV == 1; open(IN, $ARGV[0]) or die "unable to open '$ARGV[0]'"; while (my $line = <IN>) { my $prefix = sprintf '%4d', $.; $line =~ s/$line/$prefix: $line/; print $line; 8: } 9: close IN; $
Thanks for your candor.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: concatenation through substitution?
by merlyn (Sage) on Dec 30, 2004 at 19:37 UTC | |
by Anonymous Monk on Dec 31, 2004 at 14:57 UTC | |
|
Re: concatenation through substitution?
by waswas-fng (Curate) on Dec 30, 2004 at 21:22 UTC | |
by revdiablo (Prior) on Dec 30, 2004 at 21:47 UTC | |
by trammell (Priest) on Dec 31, 2004 at 00:33 UTC | |
by Anonymous Monk on Dec 31, 2004 at 14:58 UTC |