2

I am running Strawberry perl on Windows 10 from either Powershell or the CMD prompt. With CMD, you can change perl's available features on the command line with the -M option, followed by a version number, like this (to enable 'say', for example):

D:\>echo hello world |perl -M5.20.0  -ne "chomp; s/ll/rr/; say"
herro world

D:\>

With Powershell, the -M option eats the initial version number and looks for a script following it:

PS D:\> echo "hello world" |perl -M5.20.0  -ne "s/ll/rr/; say"
Can't open perl script ".20.0": No such file or directory
PS D:\>

The CMD behaviour is what I would expect and is also what bash gives me.

Why is Powershell different and how can I use the -M<version#> feature in Powershell?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Wes
  • 423
  • 3
  • 12
  • 2
    I have found that putting double quotes around the "5.20.0" allows Powershell to pass the correct parameter on to perl. It remains a mystery to me why this should be the case. – Wes Jul 16 '23 at 05:59
  • 2
    Because PowerShell is a strange beast, neither fish nor fowl. – Tim Roberts Jul 16 '23 at 06:03
  • I assume `.` is a special character in powershell syntax. – Shawn Jul 16 '23 at 07:11
  • 4
    It is an - unfortunate and long-standing - PowerShell bug you're seeing and there are several workarounds: the simplest is to `\``-escape the `-`; alternatively, use quoting. See the linked duplicate for details. – mklement0 Jul 16 '23 at 13:08
  • 2
    In this case, you could also use `perl -ne"use v5.20; s/ll/rr/; say"`. – ikegami Jul 16 '23 at 16:49
  • @ikegami, I could, but I wanted to avoid putting the "use v5.20" inside the main loop. – Wes Jul 17 '23 at 00:41
  • Like, for performance? Doesn't matter. It has a compile-time effect. It's non-existent at run-time. – ikegami Jul 17 '23 at 05:11

0 Answers0