On Saturday, August 31, 2002, at 04:31 AM, Bruce Van Allen wrote:

> At 1:20 PM -0400 2002-08-30, Warren Pollans wrote:
>> Hi Folks,
>>
>> This is not OSX-related, but I'm hoping that some OSXer could 
>> point me in the right direction.  This has to have a RTFM 
>> answer but I haven't been able to find the FM to R.
>
> With Perl, there is always its own internal documentation, in 
> this case the POD of constant.pm.
>
> However, :-), a quick scan didn't confirm that this was where I 
> learned the answer to your question, which is to de-reference a 
> reference to the constant (!!).
>
> #!/usr/bin/perl -w
> use strict;
> use constant FS => '##';
>
> my $x = 'sadfjsfh##fskdjfa;s##dfjhasfjh';
>
> $x =~ s/${ \FS }/ZZ/g;  # or ${ \FS() }
> #       ^^ ^   ^
> print "1:$x\n";
>
> __END__


That's probably not the best solution.  The reason is that perl 
can't compile your regular expression statically, because now 
there's a variable in it, and perl will have to find this 
"constant" value every time it searches through the target 
string.  This negates the presumed performance benefits of using 
the constant.

One solution would be to use the /o flag to make it static:

  $x =~ s/${ \FS }/ZZ/og;

My preferred solution would be to pre-compile the regex, though:

  #!/usr/bin/perl -w
  use strict;
  use constant FS => '##';
  my $x = 'sadfjsfh##fskdjfa;s##dfjhasfjh';
  my $rx = qr{##};  # or my $rx = qr{${\FS}}; if you want to use FS
  $x =~ s/$rx/ZZ/g;
  print "1:$x\n";


  -Ken

Reply via email to