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

Hi monks, I am working on converting a plain text file to html file.Now I have a scenario like below
#Assume the file is read to $str which contains $str="iteration 1 some information goes here machine name:[some meta-info] some multi-line info machine name:[some meta-info] some multi-line info iteration 2 some information goes here machine name:[some meta-info] some multi-line info machine name:[some meta-info] some multi-line info"; $str=~s/(iteration)(.*)/<B> uc($1)$2 <\/B>/; print $str;
I expected the "iteration 1" and "iteration 2" to be "ITERATION 1" and "ITERATION 2", but its instead "uc(iteration 1)" and "uc(iteration 2)".
Please help me out to convert it into upper-case.Also any modules to convert plain text file to html would be helpful.TIA

ps: I know this can be done in passes by converting to uppercase in pass1 and adding tags in pass 2, but is it possible to achieve in a single substitution

The world is so big for any individual to conquer

Replies are listed 'Best First'.
Re: Calling a function in regex substitution
by bart (Canon) on Sep 11, 2007 at 09:31 UTC
    For this particular example, you don't need a function call: just use \U to convert the rest of the string (up to \E or an overriding modifier) to uppercase.

    If you want to replace all matches, you'll have to specify the /g flag too.

    $str=~s/(iteration)(.*)/<B> \U$1\E$2 <\/B>/g;

    You can run code in the substitution part by specifying the /e flag, but you'll have to make sure the whole string on the right is valid Perl code. You can't mix and match literal strings and executable code.

    $str=~s/(iteration)(.*)/"<B> ".uc($1).$2." <\/B>"/ge;

    The next works too, it's one of the few tricks available to embed executable code in a double-quoted string.

    $str=~s/(iteration)(.*)/<B> @{[uc($1)]}$2 <\/B>/g;
Re: Calling a function in regex substitution
by almut (Canon) on Sep 11, 2007 at 09:29 UTC
    $str=~s/(iteration)(.*)/"<B>".uc($1)."$2 <\/B>"/eg;

    (Option 'e' makes the difference; it evaluates the substitution part as Perl code.)

Re: Calling a function in regex substitution
by tcf03 (Deacon) on Sep 11, 2007 at 10:14 UTC
    Just curious but is this something that could be handled by an ini style config file and a module like Config::Tiny ?

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: Calling a function in regex substitution
by b4swine (Pilgrim) on Sep 11, 2007 at 09:30 UTC
    Using functions can be done in regexps, but easier in this case is
    $str=~s/(iteration)(.*)/<B> /U$1/E$2 <\/B>/g;
    See documentation on regexps.
      $str=~s/(iteration)(.*)/<B> /U$1/E$2 <\/B>/g;
      It is recommendable to test your code before posting it here. Your snippet produces a syntax error, and you need to use backslashes, not forward slashes.