http://www.perlmonks.org?node_id=11134010


in reply to Windows and backslashes and replacements oh my!

My guess is something in your call is doing some escape code interpretation which translates \t to tab.

Which is weird for Windows. ..

One workaround is to replace every \\ with ${bs} in your Perl code and to initialize it with $bs= chr (92), like this a literal backslash wouldn't show up.


Update: or something is collapsing \\t to \t such that Perl sees a tab.

Anyway the problem is not in Perl but the surrounding shell .

- Ron
  • Comment on Re: Windows and backslashes and replacements oh my!

Replies are listed 'Best First'.
Re^2: Windows and backslashes and replacements oh my!
by stevieb (Canon) on Jun 18, 2021 at 18:57 UTC

    Changing the line to the following has resolved the issue:

    call perl -i.bak -ne "my $bs=chr(92); s/berrybrew(?!\\+test)/berrybrew +${bs}${bs}test/; print" test/data/config.json

    Thanks!

      ${bs}${bs} means using two consecutive backslashes, which is still weird.

      Looks like somewhere a Unix tool like bash or another Perl is processing the file.

      (Update: Obviously Perl needs to escape the backslash inside a regex)

      FWIW: using $b2=chr(92)x2 might save you some typing

      - Ron
        One $bs is sufficient in my tests:

        d:\>perl -E"for $x (qw/C:\berrybrew\test C:\berrybrew /) {$_=$x; $bs=c +hr(92); s/berrybrew(?!\\+test)/berrybrew${bs}test/; say}" C:\berrybrew\test C:\berrybrew\test d:\>

        > Obviously Perl needs to escape the backslash inside a regex

        not after interpolation of variables like $bs

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^2: Windows and backslashes and replacements oh my!
by Anonymous Monk on Jun 18, 2021 at 19:37 UTC

    Which is weird for Windows. ..

    it's not even a little bit weird. Variable expansion is basic job of shells.

    turn it off in your batch files

    SETLOCAL DISABLEDELAYEDEXPANSION

    help setlocal

      > it's not even a little bit weird. Variable expansion is basic job of shells.

      The discussion was about "escape sequences" with \ not "variable expansion".

      The escape meta in bat is ^ not \

      And \ is - unfortunately - used as path separator in win, which rules out any use as escape character.

      Anything else would be indeed "weird", because it would make using paths impossible!

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      ) see Escape_character#Windows_Command_Prompt