0

I'm very new to Perl and I'm trying to figure out just how to get this thing to work.

I found an example in this thread: Perl Regex - Print the matched value

Here is the example I'm trying to work with:

perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /(Xy = 3)/;'

I've tried running it using cmd, but that produces this error:

"Can't find string terminator "'" anywhere before EOF at -e line 1."

When I run it in powershell nothing happens.

My ultimate goal is to set a variable at the command line, run a regexp find (and sometimes replace), and to print the result. This way I don't have to write a script every time I write a regexp pattern.

I've tried using the debugger, but nothing happens when I do this after setting the variable:

print $str =~ /(Xy = 3)/;
Community
  • 1
  • 1
Drew Rush
  • 710
  • 5
  • 19

2 Answers2

1

It is better to put your statements inside a Perl script on a windows environment because you will need the double quotes for most of your Perl stuff, but escaping on the command line gets messy eventually.

brianadams
  • 233
  • 1
  • 5
0

Ok, well I figured it out for powershell. I had to escape the double quotes.

perl -e '$str=\"the variable Xy = 3 in this case\"; print $str =~ /(Xy = 3)/;'

And the same goes for cmd except that I had to replace the single quotes with double quotes since single quotes aren't a string delimiter there.

perl -e "$str=\"the variable Xy = 3 in this case\"; print $str =~ /(Xy = 3)/;"
Drew Rush
  • 710
  • 5
  • 19
  • 1
    I would change the string definition to qq{} instead of "" to avoid having to escape. perl -e "$str=qq{the variable Xy = 3 in this case}; print $str =~ /(Xy = 3)/;" – Miller Dec 22 '13 at 05:08