How do I get perl to replace strtolower($value) with strtolower(substr($value, 0, 2))












1














I have several hundred PHP scripts that expect a language field to contain an ISO 639-1 2-character identifier. for example "en", which I now want to modify to support language codes qualified by country code, for example "fr-CA". In each of these scripts there is the following code:



    case 'lang':
{ // language code
if (strlen($value) == 2)
$lang = strtolower($value);
break;
} // language code


which I want to modify to:



    case 'lang':
{ // language code
if (strlen($value) >= 2)
$lang = strtolower(substr($value,0,2));
break;
} // language code


So I wrote a perl script to run over the entire directory tree and modify all of the matching scripts. For testing I have set the script up to create all of the modified scripts in a new directory structure:



use strict;
use warnings;
use 5.010;

use File::Find;
use File::Slurp;

my @content;
find( &wanted, '/home/jcobban/public_html/');

exit;

sub wanted {
if (-f)
{
print "wanted: ", $File::Find::name, "n";
my $odir = '/home/jcobban/testlang' . substr($File::Find::dir, 25);
if ((substr $odir, -1) ne "/"){
$odir = "$odir/";
}
if (! -d $odir){
mkdir $odir;
}
print "odir '$odir'n";
my @lines = read_file($File::Find::name);
my $caselang = 0;
my $updated = 0;
foreach my $line (@lines){
if ($line =~ /bcaseb/)
{
$caselang = $line =~ /blangb/i;
}

if ($line =~ /bbreakb/)
{
$caselang = 0;
}

if ($caselang)
{
print "old $linen";
$line =~ s/ == 2/ >= 2/;
$line =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;
$updated = 1;
print "new $linen";
}
}

if ($updated)
{
# my $newfile = $File::Find::dir . "/" . $_;
my $newfile = $odir . $_;
print "alter $lang to support ll-CC $newfilen";
write_file($newfile, @lines);
}
else
{
print "did not find lang support in $_n";
}

}
return;
}


The first match replace works, to change the == to >=, but the second match replace does not modify any lines and I do not understand well. I thought maybe there was a problem with matching to "$" so I replaced it with "." but still no lines are changed. I applied the same command to other regex engines and they all worked. The output for a typical file is:



wanted: /home/jcobban/public_html/videoTutorials.php
odir '/home/jcobban/testlang/'
old case 'lang':
new case 'lang':
old {
new {
old if (strlen($value) == 2)
new if (strlen($value) >= 2)
old $lang = strtolower($value);
new $lang = strtolower($value);
alter $lang to support ll-CC /home/jcobban/testlang/videoTutorials.php









share|improve this question



























    1














    I have several hundred PHP scripts that expect a language field to contain an ISO 639-1 2-character identifier. for example "en", which I now want to modify to support language codes qualified by country code, for example "fr-CA". In each of these scripts there is the following code:



        case 'lang':
    { // language code
    if (strlen($value) == 2)
    $lang = strtolower($value);
    break;
    } // language code


    which I want to modify to:



        case 'lang':
    { // language code
    if (strlen($value) >= 2)
    $lang = strtolower(substr($value,0,2));
    break;
    } // language code


    So I wrote a perl script to run over the entire directory tree and modify all of the matching scripts. For testing I have set the script up to create all of the modified scripts in a new directory structure:



    use strict;
    use warnings;
    use 5.010;

    use File::Find;
    use File::Slurp;

    my @content;
    find( &wanted, '/home/jcobban/public_html/');

    exit;

    sub wanted {
    if (-f)
    {
    print "wanted: ", $File::Find::name, "n";
    my $odir = '/home/jcobban/testlang' . substr($File::Find::dir, 25);
    if ((substr $odir, -1) ne "/"){
    $odir = "$odir/";
    }
    if (! -d $odir){
    mkdir $odir;
    }
    print "odir '$odir'n";
    my @lines = read_file($File::Find::name);
    my $caselang = 0;
    my $updated = 0;
    foreach my $line (@lines){
    if ($line =~ /bcaseb/)
    {
    $caselang = $line =~ /blangb/i;
    }

    if ($line =~ /bbreakb/)
    {
    $caselang = 0;
    }

    if ($caselang)
    {
    print "old $linen";
    $line =~ s/ == 2/ >= 2/;
    $line =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;
    $updated = 1;
    print "new $linen";
    }
    }

    if ($updated)
    {
    # my $newfile = $File::Find::dir . "/" . $_;
    my $newfile = $odir . $_;
    print "alter $lang to support ll-CC $newfilen";
    write_file($newfile, @lines);
    }
    else
    {
    print "did not find lang support in $_n";
    }

    }
    return;
    }


    The first match replace works, to change the == to >=, but the second match replace does not modify any lines and I do not understand well. I thought maybe there was a problem with matching to "$" so I replaced it with "." but still no lines are changed. I applied the same command to other regex engines and they all worked. The output for a typical file is:



    wanted: /home/jcobban/public_html/videoTutorials.php
    odir '/home/jcobban/testlang/'
    old case 'lang':
    new case 'lang':
    old {
    new {
    old if (strlen($value) == 2)
    new if (strlen($value) >= 2)
    old $lang = strtolower($value);
    new $lang = strtolower($value);
    alter $lang to support ll-CC /home/jcobban/testlang/videoTutorials.php









    share|improve this question

























      1












      1








      1







      I have several hundred PHP scripts that expect a language field to contain an ISO 639-1 2-character identifier. for example "en", which I now want to modify to support language codes qualified by country code, for example "fr-CA". In each of these scripts there is the following code:



          case 'lang':
      { // language code
      if (strlen($value) == 2)
      $lang = strtolower($value);
      break;
      } // language code


      which I want to modify to:



          case 'lang':
      { // language code
      if (strlen($value) >= 2)
      $lang = strtolower(substr($value,0,2));
      break;
      } // language code


      So I wrote a perl script to run over the entire directory tree and modify all of the matching scripts. For testing I have set the script up to create all of the modified scripts in a new directory structure:



      use strict;
      use warnings;
      use 5.010;

      use File::Find;
      use File::Slurp;

      my @content;
      find( &wanted, '/home/jcobban/public_html/');

      exit;

      sub wanted {
      if (-f)
      {
      print "wanted: ", $File::Find::name, "n";
      my $odir = '/home/jcobban/testlang' . substr($File::Find::dir, 25);
      if ((substr $odir, -1) ne "/"){
      $odir = "$odir/";
      }
      if (! -d $odir){
      mkdir $odir;
      }
      print "odir '$odir'n";
      my @lines = read_file($File::Find::name);
      my $caselang = 0;
      my $updated = 0;
      foreach my $line (@lines){
      if ($line =~ /bcaseb/)
      {
      $caselang = $line =~ /blangb/i;
      }

      if ($line =~ /bbreakb/)
      {
      $caselang = 0;
      }

      if ($caselang)
      {
      print "old $linen";
      $line =~ s/ == 2/ >= 2/;
      $line =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;
      $updated = 1;
      print "new $linen";
      }
      }

      if ($updated)
      {
      # my $newfile = $File::Find::dir . "/" . $_;
      my $newfile = $odir . $_;
      print "alter $lang to support ll-CC $newfilen";
      write_file($newfile, @lines);
      }
      else
      {
      print "did not find lang support in $_n";
      }

      }
      return;
      }


      The first match replace works, to change the == to >=, but the second match replace does not modify any lines and I do not understand well. I thought maybe there was a problem with matching to "$" so I replaced it with "." but still no lines are changed. I applied the same command to other regex engines and they all worked. The output for a typical file is:



      wanted: /home/jcobban/public_html/videoTutorials.php
      odir '/home/jcobban/testlang/'
      old case 'lang':
      new case 'lang':
      old {
      new {
      old if (strlen($value) == 2)
      new if (strlen($value) >= 2)
      old $lang = strtolower($value);
      new $lang = strtolower($value);
      alter $lang to support ll-CC /home/jcobban/testlang/videoTutorials.php









      share|improve this question













      I have several hundred PHP scripts that expect a language field to contain an ISO 639-1 2-character identifier. for example "en", which I now want to modify to support language codes qualified by country code, for example "fr-CA". In each of these scripts there is the following code:



          case 'lang':
      { // language code
      if (strlen($value) == 2)
      $lang = strtolower($value);
      break;
      } // language code


      which I want to modify to:



          case 'lang':
      { // language code
      if (strlen($value) >= 2)
      $lang = strtolower(substr($value,0,2));
      break;
      } // language code


      So I wrote a perl script to run over the entire directory tree and modify all of the matching scripts. For testing I have set the script up to create all of the modified scripts in a new directory structure:



      use strict;
      use warnings;
      use 5.010;

      use File::Find;
      use File::Slurp;

      my @content;
      find( &wanted, '/home/jcobban/public_html/');

      exit;

      sub wanted {
      if (-f)
      {
      print "wanted: ", $File::Find::name, "n";
      my $odir = '/home/jcobban/testlang' . substr($File::Find::dir, 25);
      if ((substr $odir, -1) ne "/"){
      $odir = "$odir/";
      }
      if (! -d $odir){
      mkdir $odir;
      }
      print "odir '$odir'n";
      my @lines = read_file($File::Find::name);
      my $caselang = 0;
      my $updated = 0;
      foreach my $line (@lines){
      if ($line =~ /bcaseb/)
      {
      $caselang = $line =~ /blangb/i;
      }

      if ($line =~ /bbreakb/)
      {
      $caselang = 0;
      }

      if ($caselang)
      {
      print "old $linen";
      $line =~ s/ == 2/ >= 2/;
      $line =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;
      $updated = 1;
      print "new $linen";
      }
      }

      if ($updated)
      {
      # my $newfile = $File::Find::dir . "/" . $_;
      my $newfile = $odir . $_;
      print "alter $lang to support ll-CC $newfilen";
      write_file($newfile, @lines);
      }
      else
      {
      print "did not find lang support in $_n";
      }

      }
      return;
      }


      The first match replace works, to change the == to >=, but the second match replace does not modify any lines and I do not understand well. I thought maybe there was a problem with matching to "$" so I replaced it with "." but still no lines are changed. I applied the same command to other regex engines and they all worked. The output for a typical file is:



      wanted: /home/jcobban/public_html/videoTutorials.php
      odir '/home/jcobban/testlang/'
      old case 'lang':
      new case 'lang':
      old {
      new {
      old if (strlen($value) == 2)
      new if (strlen($value) >= 2)
      old $lang = strtolower($value);
      new $lang = strtolower($value);
      alter $lang to support ll-CC /home/jcobban/testlang/videoTutorials.php






      regex perl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 2:09









      James Cobban

      624




      624
























          2 Answers
          2






          active

          oldest

          votes


















          2














          I have obviously been spending too much time using VIM. The problem with my code was that I needed to escape the round brackets so they were not interpreted as a subpattern.



              $line       =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;





          share|improve this answer





























            0














            Just want to show some hacks, maybe it will be interesting for you:



            s'strtolower(K$value'substr($value,0,2)'


            We can quote substitution with whatever we want:



            s/foo/bar/;
            s'foo'bar';
            s(foo)(bar);


            If we choose single quotes, variables will not be interpolated, but we still have to escape dollar sign in pattern side, because it will be treated as "end of line" by re engine.



            K Keep the stuff left of the K



            more information in perldoc perlre






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439876%2fhow-do-i-get-perl-to-replace-strtolowervalue-with-strtolowersubstrvalue-0%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              I have obviously been spending too much time using VIM. The problem with my code was that I needed to escape the round brackets so they were not interpreted as a subpattern.



                  $line       =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;





              share|improve this answer


























                2














                I have obviously been spending too much time using VIM. The problem with my code was that I needed to escape the round brackets so they were not interpreted as a subpattern.



                    $line       =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;





                share|improve this answer
























                  2












                  2








                  2






                  I have obviously been spending too much time using VIM. The problem with my code was that I needed to escape the round brackets so they were not interpreted as a subpattern.



                      $line       =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;





                  share|improve this answer












                  I have obviously been spending too much time using VIM. The problem with my code was that I needed to escape the round brackets so they were not interpreted as a subpattern.



                      $line       =~ s/strtolower(.value)/strtolower(substr($value,0,2))/;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 at 2:16









                  James Cobban

                  624




                  624

























                      0














                      Just want to show some hacks, maybe it will be interesting for you:



                      s'strtolower(K$value'substr($value,0,2)'


                      We can quote substitution with whatever we want:



                      s/foo/bar/;
                      s'foo'bar';
                      s(foo)(bar);


                      If we choose single quotes, variables will not be interpolated, but we still have to escape dollar sign in pattern side, because it will be treated as "end of line" by re engine.



                      K Keep the stuff left of the K



                      more information in perldoc perlre






                      share|improve this answer


























                        0














                        Just want to show some hacks, maybe it will be interesting for you:



                        s'strtolower(K$value'substr($value,0,2)'


                        We can quote substitution with whatever we want:



                        s/foo/bar/;
                        s'foo'bar';
                        s(foo)(bar);


                        If we choose single quotes, variables will not be interpolated, but we still have to escape dollar sign in pattern side, because it will be treated as "end of line" by re engine.



                        K Keep the stuff left of the K



                        more information in perldoc perlre






                        share|improve this answer
























                          0












                          0








                          0






                          Just want to show some hacks, maybe it will be interesting for you:



                          s'strtolower(K$value'substr($value,0,2)'


                          We can quote substitution with whatever we want:



                          s/foo/bar/;
                          s'foo'bar';
                          s(foo)(bar);


                          If we choose single quotes, variables will not be interpolated, but we still have to escape dollar sign in pattern side, because it will be treated as "end of line" by re engine.



                          K Keep the stuff left of the K



                          more information in perldoc perlre






                          share|improve this answer












                          Just want to show some hacks, maybe it will be interesting for you:



                          s'strtolower(K$value'substr($value,0,2)'


                          We can quote substitution with whatever we want:



                          s/foo/bar/;
                          s'foo'bar';
                          s(foo)(bar);


                          If we choose single quotes, variables will not be interpolated, but we still have to escape dollar sign in pattern side, because it will be treated as "end of line" by re engine.



                          K Keep the stuff left of the K



                          more information in perldoc perlre







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 25 at 5:24









                          k-mx

                          212




                          212






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439876%2fhow-do-i-get-perl-to-replace-strtolowervalue-with-strtolowersubstrvalue-0%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)