Re: [Boston.pm] function that takes two arguments and passes them into s///

"Greg London" <email-2Ro/Dj86MvDSUeElwK9/[email protected]> Fri, 1 Feb 2019 10:57:22 -0500
Newsgroups gmane.comp.lang.perl.perl-mongers.boston
Message-ID <[email protected]>
OK, this is weird, and I can't seem to simplify this anymore than it
already is below, but this seems to work:


use warnings;
use strict;

my $content = "first prefix_55 second prefix_66 third";

sub cleanup{
        my($oldtext, $newtext)=@_;
        my $original=$content;
        my $modified=$content;
        my $changed_flag=0;

        if($modified =~ s/$oldtext/"\"$newtext\""/gee  ){
                $changed_flag=1;
        }

        print "\n\n################################################\n";
        print "oldtext is '$oldtext'\nnewtext is
'$newtext'\noriginal:\n$original\n";
        print "modified: \n$modified\nchange_flag is $changed_flag\n";
}

cleanup('prefix_(\w+)', 'PREFIX_$1');


When I run it, it prints out:
original:
first prefix_55 second prefix_66 third
modified:
first PREFIX_55 second PREFIX_66 third


I guess I still haven't quite wrapped my head around string interpolation
in perl. But the code seems to be doing what I want.

I think the replacement part of s/// does a string interpolation
on top of all the other string interpolations, so I have to
double-double-quote that part.

after all these years, perl is still surprising me.
Greg

On Wed, January 30, 2019 10:14 pm, Bill Ricker via Boston-pm wrote:
> Maybe try
> cleanup('prefix_(\w+)', 'PREFIX_$1');  as
>    cleanup(  qr{prefix_(\w+)},   'PREFIX_$1'  );
> ?
>
>
> You do NOT need the /e flag(s) for the $1 substitution to work.
> I don't know if it works if it is in a substituted string or if it has
> to be top level of the parse. The capturing parens need to be seen as RegEx
> special chars when parsed (as does the \w+),
>
>
> (the alternative is to build the whole s/// statement in a string and
> eval _that_.)
>
> it will have a better chance of working On 01/30/2019 01:36 PM, Greg
> London wrote:
>
>> Tried to boil this down to the smallest testcase i could come up with.
>>
>>
>> I have a function that wraps a s/// substitution.
>> Callers pass in the pattern and the replacement string.
>> There's a bunch of other stuff going on that is why the
>> function exists, but isn't why it isn't working.
>>
>> It's not working when I try pass in $1 as a substitution string.
>> Cant get it to eval 'PREFIX_$1' inside the s/// piece.
>>
>>
>> i've tried s///e and s///ee and neither work. I've tried qq()'ing the
>> text, tryign to turn it into an expression, etc. Everything I've tried,
>> I get one of two things in the output:
>> Either the output has literally PREFIX_$1 in the result
>> or it completely removes teh previx_nn from the result.
>>
>> I've used eval() on '$var' in other settings and it works.
>> But cant seem to get it to work in the case of the replacement
>> string in a s/// substitution.
>>
>> Thoughts?
>>
>>
>> I'm using perl 5.8, if it matters.
>>
>>
>> Here's the test code:
>>
>>
>>
>> #!/usr/bin/perl
>>
>>
>> use warnings; use strict;
>>
>> #---------------------------------------
>> # modules and other library code
>> #---------------------------------------
>>
>>
>> my $content =<<'CONTENT'; some prefix string HERE: prefix_01
>> post prefix stuff CONTENT
>>
>>
>>
>> sub cleanup{ my($oldtext, $newtext)=@_; my $original=$content; my
>> $modified=$content;
>> my $changed_flag=0;
>>
>> if($modified =~ s/$oldtext/$newtext/ge  ){ $changed_flag=1;
>> }
>>
>>
>> print "\n\n################################################\n"; print
>> "oldtext is '$oldtext'\nnewtext is '$newtext'\noriginal:
>> \n$original\n";
>> print "modified: \n$modified\nchange_flag is $changed_flag\n";
>>
>> }
>>
>>
>> #---------------------------------------
>> # user code
>> #---------------------------------------
>>
>>
>> # straightforward pattern with no capture: works
>> cleanup('prefix','PREFIX');
>>
>> # using capture parens and $1: does not work
>> cleanup('prefix_(\w+)', 'PREFIX_$1');
>>
>> _______________________________________________
>> Boston-pm mailing list
>> [email protected]
>> https://mail.pm.org/mailman/listinfo/boston-pm
>>
>>
>
> --
> Bill Ricker
> independent contractor for The Perl Shop
>
>
> _______________________________________________
> Boston-pm mailing list
> [email protected]
> https://mail.pm.org/mailman/listinfo/boston-pm
>
>


-- 

_______________________________________________
Boston-pm mailing list
[email protected]
https://mail.pm.org/mailman/listinfo/boston-pm