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

Hello. I'm new to PERL, and been trying to figure things out, but got stuck at one part. One of the many things I'm trying to do is call a command line (yes the windows cmd tool) from PERL. I'm trying to control a signal analyzer via cmd line (remotely) along with some other automation scripts using PERL. Can anyone help or head me to the right direction? Any help will be appreciated. Thank you.

  • Comment on [Newbie Question] How to call cmd line within PERL?

Replies are listed 'Best First'.
Re: [Newbie Question] How to call cmd line within PERL?
by kcott (Archbishop) on Jul 20, 2016 at 07:15 UTC

    G'day bill5262,

    Welcome to the Monastery.

    [Note: The language is "Perl"; the executable is "perl".]

    The simplest method — although, I suspect, too simplistic for your needs — is to use backticks, i.e. `command`, or the equivalent qx{command}: see "perlop: Quote and Quote-like Operators". Another is the system function, which probably also falls into this "too simplistic" category.

    You can use the open function to pipe to and from your command. Depending on how "new" you are, you may want to first read "perlintro: Files and I/O" and the tutorial perlopentut. "perlipc: Using open() for IPC" has more detailed information.

    Also see the builtin "IPC::* modules" and the CPAN module Capture::Tiny.

    All of the above is an attempt to head you in "the right direction", as requested. With more specific information from you, there may be better answers from us: these guidelines will help in that regard.

    — Ken

Re: [Newbie Question] How to call cmd line within PERL?
by haukex (Archbishop) on Jul 20, 2016 at 07:06 UTC

    Hi bill5262,

    I'm going to guess that by "the windows cmd tool" you don't mean you want a cmd.exe window to open, but instead you want to issue commands from your Perl script which you otherwise would have typed into the command line. The AM already mentioned two good modules, to add to that there's also IPC::System::Simple, and I mention a few more in one of my posts: Re: Using IPC::Open3 instead of backtick operator (updated)

    If any of the programs you're running are interactive, meaning they show a prompt of their own and expect you to input something, then the first thing to check is if they also offer a "batch mode" where they accept a command script. Otherwise, you may have to try talking to the program interactively from your Perl script via IPC::Run or Expect (disclaimer: I don't have any experience with these in Windows).

    If the programs you're running are not interactive, meaning they just accept arguments on the command line and then run without user input, you can use any of the modules from the first paragraph above. I might suggest IPC::System::Simple because it has the best error reporting of those modules. If you need to capture the program's output and find that IPC::System::Simple is not capturing everything (that's because it doesn't capture STDERR), I'd recommend IPC::Run3.

    Hope this helps,
    -- Hauke D

Re: How to call cmd line within PERL?
by Anonymous Monk on Jul 20, 2016 at 06:30 UTC

      Thanks.
      (ps. The Tiny link was broken so I looked into http://perldoc.net/Capture/Tiny.pod hoping that's what you meant to refer)

      I maybe a bit lost honestly.

      I'll try to put it this way. I'm using a signal analyzer (SA) as a part of my testing on a daily basis with some other eqiupments, and I'm trying to make a PERL script to automate the repeititions.

      Here's what I found from the manufacturer's application note.

      --------------------------------
      Command-Line Operation

      To speed up frequently used commands (e.g. taking screenshots only from one instrument), you can pass the command via the command line to GPIBShot. The following commands are available:

      /{Number between 1 and 31} set IEEE address
      /COLOR switch to color mode
      /BW switch to b/w mode
      /SCREEN take a screen shot
      /TRACE{1..4} read out trace data
      /HIDE supress trace display on PC
      /SHOW shows window again
      /END terminates the program
      /NAME=<name> specify the name of the file to generate

      All combinations ot the commands are possible. The command line is parsed serial.

      Example: Calling "GPIBSHOT.EXE /HIDE /20 /COLOR /NAME=IMG1 /SCREEN /NAME=DAT1 /TRACE2 /END" takes a colored hardcopy, stored as "IMG1.WMF", and stores the data of trace 2 with the filename "DAT1.TXT" of IEEE device 20 without displaying the graphical user interface.
      --------------------------------

      On a second thought, can I just call the said example "gpibshot.exe /hide blah blah blah..." from PERL using the system funcion or one of the two methods you provided?

      I was initially thinking calling the command prompt tool (cmd), then call the said command. But it seems like I can just straightly call the command from PERL with the right method?

        Hi bill5262,

        In addition to my other post here's a really quick example:

        #!/usr/bin/env perl use warnings; use strict; use IPC::System::Simple 'system'; my $ieeeaddr = 20; system('GPIBSHOT.EXE','/HIDE',"/$ieeeaddr",'/COLOR','/NAME=IMG1', '/SCREEN','/NAME=DAT1','/TRACE2','/END');

        Update: The line "use IPC::System::Simple;" should have been "use IPC::System::Simple 'system';", since system is not exported by default!

        Hope this helps,
        -- Hauke D

        (ps. The Tiny link was broken so I looked into http://perldoc.net/Capture/Tiny.pod hoping that's what you meant to refer)
        http://perldoc.net/Capture/Tiny.pod looks very nice, but outdated, as it currently describes version 0.08,
        while CPAN (Capture::Tiny) and MetaCPAN (Capture::Tiny) have version 0.42

        Update: One year later, we are at version 0.46, perldoc.net still at 0.08 …