Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

The difference between [system] and `backticks` and [qx//] (on win32)

by BrowserUk (Patriarch)
on Oct 11, 2003 at 02:50 UTC ( [id://298473]=perlquestion: print w/replies, xml ) Need Help??

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

I ought to know why this is the case, but I don't.

Why does system 'cls' clear my CLI session screens

but `cls` and qx[cls] not?

perl -e"system 'cls'" perl -e"`cls`" perl -e"qx[cls]"

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: The difference between [system] and `backticks` and [qx//] (on win32)
by Abigail-II (Bishop) on Oct 11, 2003 at 02:58 UTC
    My guess (I don't use Windows, but 'system "clear"' works on my Linux/bash/xterm, but `clear` doesn't, so I expect the reason is the same) is that 'cls' on Windows, just like 'clear' on Linux/Unix print out an escape sequence to standard out, which gets interpreted by the terminal driver to clear the screen.

    The use of `` or qx makes that Perl gets the output of the command, not the terminal driver.

    $ perl -wle '$_ = `clear`; s/(.)/sprintf "0x%02x", ord $1/ge; print' 0x1b0x5b0x480x1b0x5b0x4a $ perl -we 'print chr for 0x1b,0x5b,0x48,0x1b,0x5b,0x4a' $ # Clears the screen! $ clear | cat -v ^[[H^[[J

    Abigail

      Your right! `cls` does indeed return an 'escape sequence' to perl. In this case, ascii 12 (form feed). I'm guessing that is a throw-over from DOS when clearing the screen was considered the glass-teletype equivalent of throwing a new page on a printer.

      However, the situation isn't that clear cut. I can capture the character into a file CLS > junk and output that file to the screentype junk, and see the character being produced, but it doesn't have the desired effect as the equivalent would under sh/csh/ksh/bash et al.

      In fact, I've tried half a dozen other ways of passing the sequence to the terminal driver but it steadfastly refuses to act upon it! So, my conclusion is that the CLS command, which is built-in to CMD.EXE, emits the escape sequence to clear the screen, but the terminal driver only acts upon it if it is recieved from CMD.EXE!

      Of course, 'terminal driver' is an alien concept to a system that doesn't use terminals. In reality, I suspect that it is CMD.EXE itself that either clears the screen, or calls a system API to do it. Which kind of begs the question, why does it bother emiting the escape sequence only to detect it, remove it from the buffer and act upon it in another part of it's own internal code. I guess that is, of necessity, a rhetorical question.

      {sigh} I can hear your mirth now, despite the 1000 ks between our locations:)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail

        Your right! `cls` does indeed return an 'escape sequence' to perl. In this case, ascii 12 (form feed). I'm guessing that is a throw-over from DOS when clearing the screen was considered the glass-teletype equivalent of throwing a new page on a printer.
        If I remember correctly, this is still how it is done on an OpenVMS system. The command to "clear the screen" is something like
        $ PRINT NL:

        This has the effect of outputting the NULL device to the screen. Have no idea how that would look using Perl, never got the opportunity to use Perl on OpenVMS (if anyone has a spare Alpha workstation gathering dust though.... :-)


        "Ex libris un peut de tout"

        Pure speculation, but maybe cmd.exe executes a system call to put the screen in ANSI mode, then prints an escape sequence to clear the screen, then executes another syscall to take it out of ANSI mode. Maybe the screen clearing requires both a syscall and something printed, and your program is only doing one.

        I don't think it depends on whether the "escape sequence" is send from CMD.EXE, I think it has to do with what system or BIOS call the program printing uses to print. Back in my old DOS programming days, there were basically three ways to print to the screen: via the DOS interrupt call, via the BIOS interrupt call, or by directly writing the data to the screen memory buffer. And I think the only way to get special characters interpretted specially was to print via the DOS interrupt call. Obviously, this most likely doesn't accurately reflect the current reality, but I wouldn't be suprised if it's something similar. *shrug*

        Also of note is the fact that, if you load ANSI.SYS in your CONFIG.SYS, then doing print "\e[2J" from perl should clear the screen. That depends, of course, on whether you can require that module be loaded or not.

        If you can't require ANSI.SYS be loaded, you could also look in to the Win32::Console::ANSI module on CPAN, which appears to emulate the functionality and includes support for the clear-screen escape sequence. Update: Or, perhaps more directly, Win32::Console's Cls.

        bbfu
        Black flowers blossom
        Fearless on my breath

        I can't help you there, as the equivalent does work on Linux:
        $ clear > /tmp/clear $ cat /tmp/clear # Clears the xterm

        Abigail

Log In?
Username:
Password:

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

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

    No recent polls found