in reply to Re: Re: Re: Using $'
in thread appending to end of string
PLEASE DON'T USE $&! Take a look at perldoc perlvar for $&. For why $& is generally consider to be a Bad Thing™, check out the documentation for Devel::SawAmpersand. The important parts:
There's a global variable in the perl source, called sawampersand. It gets set to true [..whenever..] the parser sees one of $`, $', and $&. It never can be set to false again.
If the global variable sawampersand is set to true, all subsequent RE operations will be accompanied by massive in-memory copying, because there is nobody in the perl source who could predict, when the (necessary) copy for the ampersand family will be needed. So all subsequent REs (even in places outside of scope) considerably slower than necessary.
That page even offers helpful ways that you can avoid the $&, $`, and $' variables.
Update: Proof of concept since my buddy didn't believe me:
Introducing Test1.pm
package Test1; use Devel::SawAmpersand qw(sawampersand); use Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(testbelow); sub testbelow { print "Inside Test1::testbelow"; print "sawampersand? ", (sawampersand() ? "yes":"no"); print "Outside Test1::testbelow" } 1;
Introducing Test2.pm
package Test2; use Exporter; our @ISA = qw(Exporter); # notice that we don't even export anything sub showdollaramp { $&; } 1;
And test.pl
use Test1; use Devel::SawAmpersand qw(sawampersand); $\ = "\n"; # I'm lazy; puts \n at end of every print print "sawampersand? ", (sawampersand() ? "yes":"no"); testbelow(); require Test2; print "Loaded Test2"; print "sawampersand? ", (sawampersand() ? "yes":"no"); testbelow(); __DATA__ output: sawampersand? no Inside Test1::testbelow sawampersand? no Outside Test1::testbelow Loaded Test2 sawampersand? yes Inside Test1::testbelow sawampersand? yes Outside Test1::testbelow
So will this affect anything anywhere else including within modules that loaded prior to the $& showing up? YES!
antirice
The first rule of Perl club is - use Perl
The ith rule of Perl club is - follow rule i - 1 for i > 1
|
|---|