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.
In reply to concatenation through substitution? by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |