in reply to another string replacement with uppercase :: converting C macros to C++

Hello f77coder,

Your first try is close, but has two problems:

  1. You need to capture the text between the double underlines
  2. To run a perl function in the replacement part of a substitution, you need to add an /e modifier.
#! perl use strict; use warnings; while (<DATA>) { s/__(\S+?)__/uc($1)/eg; print; } __DATA__ #ifdef __some_Long_name__ do stuff #endif

Output:

18:25 >perl 1523_SoPW.pl #ifdef SOME_LONG_NAME do stuff #endif 18:27 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: another string replacement with uppercase :: converting C macros to C++
by martin (Friar) on Jan 23, 2016 at 09:06 UTC
    For uppercasing, of course, there is also the builtin \U.
    s/\b__(\w+)__\b/\U$1/;

      Good point! I’d forgotten about these (if I ever knew them1) — except for \Q, which I often have to use. For the record, they’re documented here:

      19:11 >perl -wE "my $s = qq[abc\Udef\Eghijk]; say $s;" abcDEFghijk 19:13 >

      Good to know, thanks!

      Update: 1I must have read about them any number of times. But unfortunately it seems that reading about ≠ knowing, unless it’s combined with using. ;-)

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^2: another string replacement with uppercase :: converting C macros to C++
by choroba (Cardinal) on Jan 23, 2016 at 09:18 UTC
    The second is not farther:
    s/__(\S+)__/\U$1/g
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^2: another string replacement with uppercase :: converting C macros to C++
by f77coder (Beadle) on Jan 23, 2016 at 08:36 UTC

    Excellent! Works like a charm

    Thanks you very much.