S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

I want to provide a upper and lower case replace capability to my program. The line that does the standard regex works but I am not sure if the LC and UC calls will work. I have not been able to check this but figured I'd post in case there is a different way to tackle this.

Here is what I am doing (%input is CGI)

for ( keys %input ) { $report =~ s/$input->param('wrapper')$_$input->param('wrapper')/$i +nput->param($_)/g; $report =~ s/$input->param('wrapper')lc_$_$input->param('wrapper') +/lc $input->param($_)/g; $report =~ s/$input->param('wrapper')uc_$_$input->param('wrapper') +/uc $input->param($_)/g; }

It's lines 3 and 4 I am curious about. Will that work? From the looks of it, I would assume not (meaning it won't do what I'm thinking I want it to do).

TIA

Sean Shrum
sean@shrum.net

Replies are listed 'Best First'.
Re: REGEX question
by japhy (Canon) on Feb 06, 2002 at 02:36 UTC
    Method calls do not expand in strings nor regexes. Store $input->param('wrapper') in a variable ahead of time.
    my $w = $input->param('wrapper'); for (keys %input) { $report =~ s/\Q$w$_$w/$input->param($_)/eg; $report =~ s/\Q$w\L$_\E$w/$input->param(lc)/eg; $report =~ s/\Q$w\U$_\E$w/$input->param(uc)/eg; }
    japhy learned something today -- \Q, \L, and \U nest with their respective \E as shown above.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Japhy! Good 2CU again (RIP perlguru.com)

      Thanx for the code...I figured I couldn't start off with the UC and LC (as these would have just been looked at as part of the string replace.

      I'll assume (for now) that this is good (all your other stuff has been). I am in the middle of debugging a ton of other stuff. but should get to this later 2nite.

      Cheers

      Sean Shrum
      sean@shrum.net

        Nix that last post from me...I had some stray test code in the script that was doing bad things.

        I tried the code you provided and nothing seems to be happening to the tokens on the template. No errors are being generated either ;-).

        Any other ideas???

        Thanx for all your help to date.

        ======================
        Sean Shrum
        http://www.shrum.net

      Okay...I'm finally at the point where I am seeing results. I tried the code above and the search is working but the replace is placing a reference like: 'CGI=HASH(0x1522d58)->param('someparam')' TIA

      ======================
      Sean Shrum
      http://www.shrum.net

        I have a feeling you're leaving out the /e modifier, then.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who could use a job
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: REGEX question
by jonjacobmoon (Pilgrim) on Feb 06, 2002 at 02:43 UTC
    This sounds suspiciously like homework only because you say that you have not been able to try it. Maybe you should have tried it before submitting it.

    That being said, I will give you the benefit of the doubt. My guess is that it will not work because it should be lc($string) not lc $string or lc_$string.

    Also, for readability I would do:

    for ( keys %input ) { my $string = $_ . $input->param('wrapper'); my $lcstring = $input->param('wrapper') . lc($string); my $ucstring = $input->param('wrapper') . uc($string); $report =~ s/$input->param('wrapper')$string/$input->param($_)/g; $report =~ s/$input->param('wrapper')$lcstring/$input->param($_)/g +; $report =~ s/$input->param('wrapper')$ucstring/$input->param($_)/g +; }
    At least it is a little clearer.

    What is not clear, is why you want to do this. It looks very strange.

    NOTE: This is untested, but I think it should work.


    I admit it, I am Paco.

      Nope...not homework.

      I'm looking to expand the functionality of a database program that I wrote and want to be able to REGEX tokens in a template and be able to allow for forced case.

      The main issue that started this was sometimes a parameter in the $input may double as a filename. Under Unix systems, case-sensitivity must match.

      Ergo: http://...?&term=Thisandthat

      ...can only be used to call a filename that matches the same case. This way, the template creator can say ahead of time, "Since I don't know how the user is inputting (in regards to case), I can force one way or another.".

      Thanx for the help (and the benefit of the doubt!).

      Sean Shrum
      sean@shrum.net