in reply to code file refactoring to use English module as per PBP
Your specific problem (things being printed in the wrong order) appears to be due to the combination of double quotes and the x modifier. The double quotes in my $searchPattern = "(^\#!/usr/bin/perl.*?\n)"; causes "\#" to be inserted in the string as plain "#". The x modifier causes Perl to read "#" as the start of a comment. Thus the regex ends up being ^ alone followed by a comment whose text is "!/usr/bin/perl". The net effect is that $1 evaluates to the empty string. The regular expression matches the position at the start of the string and so inserts $insertion at the start of the string. /usr/bin/perl is never matched.
Now for how to avoid such problems in the future:
And a few comments on best practice:
The following code illustrates these points and outputs the insertion code in the correct position:
#!/usr/bin/perl -w ###-DDEBUGGING -d -Dslt` use strict; use warnings; use Env; use English qw( no_match_vars ); my $searchPattern='(^\#!/usr/bin/perl[^\n]*\n)'; my $insertion = "\nuse English \( no_match_vars \);\n"; my $content = "#!/usr/bin/perl \n\nyaba-dubba-doo\n"; print "content = <$content>\n"; print "insertion = <$insertion>\n"; $content =~ s?$searchPattern?${1}${insertion}?msx; print "content now = <$content>"; exit 0;
which outputs
content = <#!/usr/bin/perl yaba-dubba-doo > insertion = < use English ( no_match_vars ); > content now =<#!/usr/bin/perl use English ( no_match_vars ); yaba-dubba-doo >
Best, beth
Updated: explain specific cause of error and added code sample.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: junior application developer
by lloder174 (Novice) on Jun 03, 2009 at 12:56 UTC |