Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Trouble with back-references on Windows

by randy1234 (Initiate)
on Mar 14, 2019 at 18:32 UTC ( [id://1231285]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having trouble using back-references in a Windows one-liner and hoping someone knows what I'm doing wrong.

I can use a single back-reference but as soon as I try more than one it breaks.

Environment:

  • Powershell on Windows 10 (because it's seemingly more sane than trying to use the standard Command prompt).
  • Strawberry Perl (v5.26.0))

> echo "abcd" | perl -nle '/(b).(d)/ && print "$1" ' b > echo "abcd" | perl -nle '/(b).(d)/ && print "$2" ' d > echo "abcd" | perl -nle '/(b).(d)/ && print "$1 $2" ' Can't open $2 : No such file or directory.

The error changed when I tried to escape the double-quotes with a backslash and a back-tick:

> echo "abcd" | perl -nle '/(b).(d)/ && print \`"$1 $2\`" ' Can't find string terminator "`" anywhere before EOF at -e line 1.

Any ideas?

Replies are listed 'Best First'.
Re: Trouble with back-references on Windows
by poj (Abbot) on Mar 14, 2019 at 19:01 UTC
    Any ideas?
    echo "abcd" | perl -nle '/(b).(d)/ && print \"$1 $2\"'
    
    poj

      Thanks poj!

      I was using quotes for a more complicated regex earlier in the day (without the back-references) and found that it only worked with BOTH the backslash and the back-tick so I neglected to try it it in this instance.

Re: Trouble with back-references on Windows
by Laurent_R (Canon) on Mar 14, 2019 at 23:23 UTC
    Quoting is somewhat of a mess in Perl one-liners under Windows (which is one of the reasons why I prefer to use Cygwin or bash for Windows when I have to work with Windows). It is usually better to use double quotes for the one-liner script, but that makes interpolating quotes slightly more difficult to use.

    This works with the Windows command prompt:

    C:\Users\Laurent>echo "abcd" | perl -nle "/(b).(d)/ && print qq/$1 $2/ +;" b d

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1231285]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found