Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

$^O empty in windows powershell

by WithABeard (Sexton)
on Nov 21, 2022 at 11:44 UTC ( [id://11148285]=perlquestion: print w/replies, xml ) Need Help??

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

When I run the following code in command prompt (cmd), I get the "MSWin32", as expected

perl -e "print $^O;"

But when I run the same code in powershell, I get nothing.

The following code works though, outputting the same in both shells:

perl -MWin32 -e "print Win32::GetOSName();"

This outputs Win10Build 19043 (64-bit)

Is this a bug?

I'm using StrawberryPerl, perl version: v5.32.1 built for MSWin32-x64-multi-thread

Replies are listed 'Best First'.
Re: $^O empty in windows powershell
by Corion (Patriarch) on Nov 21, 2022 at 11:53 UTC

    I guess that PowerShell uses double quotes like Perl does. Maybe try single quotes?

    perl -e 'print $^O;'

      Right you are! How typical of Microsoft to not be consistent, atleast this is an improvement :P

Re: $^O empty in windows powershell
by LanX (Saint) on Nov 22, 2022 at 11:06 UTC
    For a full answer:

    PS copied many concepts from Bash and Perl. (After all it's an attempt to fix problems with cmd and bat and full of aliases to support *nix commands)

    For instance

    • $ as sigil for $variables
    • interpolation in double-quoted strings

    C:\tmp>powershell Windows PowerShell Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten. PS C:\tmp> $x="xxx" PS C:\tmp> echo "a${x}b" axxxb PS C:\tmp> echo 'a${x}b' a${x}b PS C:\tmp> exit C:\tmp>

    That's why you need a single-quoted string for perl -e

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      the following code does not output anything:

      perl -e 'print "this is a test";'

      I can't figure out why, shouldn't this work?

      this works though:

      perl -e 'print qq(this is a test);'
        my guess is that doublequotes are stripped from the input

        PS C:\Windows\system32> perl -E'say @ARGV' 'say "bla"' say bla PS C:\Windows\system32> perl -E'say @ARGV' 'say qq(bla)' say qq(bla) PS C:\Windows\system32> echo 'say "bla"' say "bla" PS C:\Windows\system32> echo 'print "bla"' |perl bla PS C:\Windows\system32> echo 'print "bla"' |perl -E'say <>' print "bla" PS C:\Windows\system32>

        this could be an artifact of Perl trying to repair input from CMD ... (?)

        (I think eyepops will soon come up with an older discussion about the Perl trying to cleverly fix CMD idiosyncrasies ;)

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: $^O empty in windows powershell -- portableshell.bat ?
by Discipulus (Canon) on Nov 22, 2022 at 07:59 UTC
    Hello WithABeard and welcome to the monastery and to the wonderful world of Perl!,

    interesting finding, I tried it and.. more doubts coming out

    PS C:\Users\me> Get-Host | Select-Object Version Version ------- 5.1.19041.1682 PS C:\Users\me> C:\EX_D\ulisseDUE\perl5.24.64bit\perl\bin\perl.exe -e +"print $^O;" PS C:\Users\me> + # no output as you noticed PS C:\Users\me> C:\EX_D\ulisseDUE\perl5.24.64bit\perl\bin\perl.exe -e +'print $^O;' MSWin32 + # ok with single quotes.. meh

    But what happens if I cast portableshell.bat coming within my portable strawberry perl?

    PS C:\Users\me> C:\EX_D\ulisseDUE\perl5.24.64bit\portableshell.bat ---------------------------------------------- Welcome to Strawberry Perl Portable Edition! * URL - http://www.strawberryperl.com/ * see README.TXT for more info ---------------------------------------------- Perl executable: C:\EX_D\ulisseDUE\perl5.24.64bit\perl\bin\perl.exe Perl version : 5.24.2 / MSWin32-x64-multi-thread C:\Users\me>perl -e "print $^O;" MSWin32 # + good 'ol expected behaviour C:\Users\me> C:\Users\me> C:\Users\me>perl -e 'print $^O;' Can't find string terminator "'" anywhere before EOF at -e line 1. # + no more single quote allowed

    More: the powershell console is no more a powershell console! Get-Host gives me command not found

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      you have to distinguish between console/terminal-window and a shell running within°, and you can run multiple shells nested within each other.²

      in your case portableshell.bat starts a fresh cmd° shell as a sub-process inside the current terminal window, after typing exit you'll be back to the outer powershell.

      compare

      C:\tmp> C:\tmp>cmd # new sub shell Microsoft Windows [Version 10.0.19044.2251] (c) Microsoft Corporation. Alle Rechte vorbehalten. C:\tmp>exit # leave sub shell C:\tmp>

      vs

      C:\tmp>start cmd # new window with su +b shell C:\tmp>

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

      °) that's mostly *nix speak, not sure how they call these on win

      ²) the motivation for the name "shell" is a surrounding process managing an environment for sub-processes

      Thanks! ^^

      Didn't know about the portableshell, it works fine :)
      I'll keep that handy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found