FAQ
Firstly, please forgive me if this isn't the right place for asking this question. I tried a couple of programmer's forums but up to now, my question hasn't even gained one answer! And yet it seems like a simple (and probably very common) requirement. I'm hoping that someone here will take pity on me!

I've been using Strawberry perl for about 6 months and I'm generally happy with it. The one thing I just can't seem to make it do is to run an external program and capture that program's output to a file. I came across perl's 'system' command. Let's say I add these lines to a perl script:-

         @my_command = ( "the_exe_name", "arg1", "arg2", "etc" );
         system(@my_command);

If I now open a DOS window and run the perl script, sure enough, it runs 'the_exe_name'. And if my exe produces any text output I can see that output in my DOS window.. Suppose my perl script is called "my_perl_script.pl". If I try to redirect its text output (like this) something interesting happens:-

         my_perl_script.pl > output.txt

The above seems to work in Windows 7. However, it doesn't work in Windows 8 or Windows XP. In both cases, the file "output.txt" does get created. And in both cases I no longer see the exe's output text in my DOS window (i.e. as if it's getting redirected to the file). But at the end of the process (in both cases) output.txt will be an empty file. :-(

My next thought was to handle any redirection within the actual perl script - i.e.

         @my_command = ( "the_exe_name", "arg1", "arg2", "etc" ">", "output.txt" );
         system(@my_command);

Unfortunately, that doesn't seem to work either (again, it always creates an empty file).

Is there a way to achieve this using Strawberry perl? Or am I making some rookie mistake here? Admittedly I'm not very experienced with perl but I'm amazed that such a simple task seems to defeat it :-(

Thanks for any help.

John

Search Discussions

  • Kmx at Apr 17, 2014 at 7:14 am

    On 17.4.2014 8:47, John Emmas wrote:
    Firstly, please forgive me if this isn't the right place for asking this
    question. I tried a couple of programmer's forums but up to now, my
    question hasn't even gained one answer! And yet it seems like a simple
    (and probably very common) requirement. I'm hoping that someone here
    will take pity on me!

    I've been using Strawberry perl for about 6 months and I'm generally
    happy with it. The one thing I just can't seem to make it do is to run
    an external program and capture that program's output to a file. I came
    across perl's 'system' command. Let's say I add these lines to a perl
    script:-

    @my_command = ( "the_exe_name", "arg1", "arg2", "etc" );
    system(@my_command);

    If I now open a DOS window and run the perl script, sure enough, it runs
    'the_exe_name'. And if my exe produces any text output I can see that
    output in my DOS window.. Suppose my perl script is called
    "my_perl_script.pl". If I try to redirect its text output (like this)
    something interesting happens:-

    my_perl_script.pl > output.txt

    The above seems to work in Windows 7. However, it doesn't work in
    Windows 8 or Windows XP. In both cases, the file "output.txt" does get
    created. And in both cases I no longer see the exe's output text in my
    DOS window (i.e. as if it's getting redirected to the file). But at the
    end of the process (in both cases) output.txt will be an empty file. :-(

    My next thought was to handle any redirection within the actual perl
    script - i.e.

    @my_command = ( "the_exe_name", "arg1", "arg2", "etc" ">",
    "output.txt" );
    system(@my_command);

    Unfortunately, that doesn't seem to work either (again, it always creates
    an empty file).

    Is there a way to achieve this using Strawberry perl? Or am I making
    some rookie mistake here? Admittedly I'm not very experienced with perl
    but I'm amazed that such a simple task seems to defeat it :-(
    Try:

    my $output = `the_exe_name arg1 arg2`;

    Or have a look at https://metacpan.org/pod/IPC::Run3

    use IPC::Run3;
    my $output;
    run3(["the_exe_name", "arg1", "arg2", "etc"], undef, \$output);

    --
    kmx
  • Kmx at Apr 17, 2014 at 7:36 am

    On 17.4.2014 9:24, Sergei Steshenko wrote:
    ________________________________
    From: kmx <kmx@atlas.cz>
    To: win32-vanilla@perl.org
    Sent: Thursday, April 17, 2014 10:14 AM
    Subject: Re: Run an external program and capture its output


    On 17.4.2014 8:47, John Emmas wrote:

    Firstly, please forgive me if this isn't the right place for asking this question. I tried a couple of programmer's forums but up to now, my question hasn't even gained one answer! And yet it seems like a simple (and probably very common) requirement. I'm hoping that someone here will take pity on me!
    I've been using Strawberry perl for about 6 months and I'm generally happy with it. The one thing I just can't seem to make it do is to run an external program and capture that program's output to a file. I came across perl's 'system' command. Let's say I add these lines to a perl script:-



    @my_command = ( "the_exe_name", "arg1", "arg2", "etc" );
    system(@my_command);


    If I now open a DOS window and run the perl script, sure enough, it runs 'the_exe_name'. And if my exe produces any text output I can see that output in my DOS window.. Suppose my perl script is called "my_perl_script.pl". If I try to redirect its text output (like this) something interesting happens:-


    my_perl_script.pl > output.txt


    The above seems to work in Windows 7. However, it doesn't work in Windows 8 or Windows XP. In both cases, the file "output.txt" does get created. And in both cases I no longer see the exe's output text in my DOS window (i.e. as if it's getting redirected to the file). But at the end of the process (in both cases) output.txt will be an empty file. :-(


    My next thought was to handle any redirection within the actual perl script - i.e.


    @my_command = ( "the_exe_name", "arg1", "arg2", "etc" ">", "output.txt" );
    system(@my_command);


    Unfortunately, that doesn't seem to work either (again, it always creates an empty file).


    Is there a way to achieve this using Strawberry perl? Or am I making some rookie mistake here? Admittedly I'm not very experienced with perl but I'm amazed that such a simple task seems to defeat it :-(
    Try:

    my $output = `the_exe_name arg1 arg2`;

    Or have a look at https://metacpan.org/pod/IPC::Run3

    use IPC::Run3;
    my $output;
    run3(["the_exe_name", "arg1", "arg2", "etc"], undef, \$output);

    --
    kmx


    FWIW, the


    my $output = `the_exe_name arg1 arg2`;

    way is to capture just STDOUT - _not_ STDERR. Regardless of OS.


    I do not remember what 'IPC::Run3' WRT STDERR.
    IPC::Run3 works AFAIK like this:

    run3(["the_exe_name", "arg1", "arg2", "etc"], undef, \$std_out_and_err);
    or
    run3(["the_exe_name", "arg1", "arg2", "etc"], undef, \$std_out, \$std_err);

    --
    kmx
  • John Emmas at Apr 17, 2014 at 8:32 am

    On 17/04/2014 08:36, kmx wrote:
    FWIW, the
    my $output = `the_exe_name arg1 arg2`;

    way is to capture just STDOUT - _not_ STDERR. Regardless of OS.
    Many thanks. As it happens, I'm only interested in capturing stdout. I
    tried 3 x variations, based on your suggestions:-

    Attempt #1:
              my $output = 'gtk-update-icon-cache --force
    --ignore-theme-index --source builtin_icons gtk/stock-icons';

    Response a) - simply running 'my_perl-script.pl' produces no output file.
    Response b) - running 'my_perl_script.pl > test.txt' produces an output
    file called "test.txt" but it's still empty.

    Attempt #2:
              use IPC::Run3;
              my $output;
              run3(["gtk-update-icon-cache", "--force",
    "--ignore-theme-index", "--source", "builtin_icons", "gtk/stock-icons"],
    undef, \$output);

    Response a) - simply running 'my_perl-script.pl' produces no output file.
    Response b) - running 'my_perl_script.pl > test.txt' produces this error
    message:-
              run3(): Permission denied saving STDOUT at
    F:\GTK-SOURCES\my_per_script.pl line 6.

    Attempt #3:
              use IPC::Run3;
              my $output;
              run3(["gtk-update-icon-cache", "--force",
    "--ignore-theme-index", "--source", "builtin_icons", "gtk/stock-icons"],
    undef, \$std_out);

    Responses are identical to attempt #2

    It seems so near and yet so far away!

    John
  • John Emmas at Apr 17, 2014 at 9:02 am
    BTW - I should have added that if I run this command (directly from a
    DOS Command Prompt) it does populate 'test.txt' with the expected output:-

              gtk-update-icon-cache --force --ignore-theme-index --source
    builtin_icons gtk/stock-icons > test.txt

    So in other words, the actual command is valid. It works when run from
    a command line but I can't seem to make it work from a perl script :-(

    John
  • John Emmas at Apr 17, 2014 at 9:52 am

    On 17/04/2014 10:02, John Emmas wrote:
    if I run this command (directly from a DOS Command Prompt) it does
    populate 'test.txt' with the expected output:-

    gtk-update-icon-cache --force --ignore-theme-index --source
    builtin_icons gtk/stock-icons > test.txt

    So in other words, the actual command is valid. It works when run
    from a command line but I can't seem to make it work from a perl
    script :-(
    FWIW this command sequence in my perl script seems to work!

              use IPC::Run3;
              my $output;
              run3(["gtk-update-icon-cache", "--force",
    "--ignore-theme-index", "--source", "builtin_icons", "gtk/stock-icons"],
    \undef, "test.txt");

    Does that seem like a reasonable solution? So far, I've only tested it
    on Windows XP (one of the platforms where it wasn't previously
    working). I'll need to re-test it on Win7 and Win8.

    John
  • John Emmas at Apr 17, 2014 at 3:55 pm

    On 17/04/2014 15:06, John R. Hogerhuis wrote:
    In what you show you are using single quotes. In the example I think
    you were trying, back ticks are used. It matters.
    Wow... VERY well spotted John. After switching to back ticks it now
    seems to be working on all 3 platforms!!!

    A huge vote of thanks to everyone who helped me with this. I'd
    literally spent days struggling with this problem. Thanks a million!

    John
  • Sisyphus1 at Apr 17, 2014 at 10:24 am
    From: John Emmas
    Sent: Thursday, April 17, 2014 4:47 PM
    my_perl_script.pl > output.txt
    This is one that comes up from time to time - it's not specific to
    Strawberry Perl, and has to do with file associations and something else
    .... a registry setting ? ... I can never remember the details, nor of how
    to search for it.

    If it's the one I'm thinking of then this works:
    perl my_script.pl > output.txt

    It's just when you make use of file associations to run a script that you
    lose the redirection - and it's a simple fix once you find out what it is.

    Surely someone here knows what I'm alluding to ... and how to remedy it ?

    Cheers,
    Rob
  • Sisyphus1 at Apr 17, 2014 at 10:35 am
    -----Original Message-----
    From: sisyphus1@optusnet.com.au
    This is one that comes up from time to time - it's not specific to
    Strawberry Perl, and has to do with file associations and something else
    .... a registry setting ? ... I can never remember the details, nor of how
    to search for it.
    Aaah ... here's the solution I was thinking of:
    http://www.perlmonks.org/?node_id=1024609

    Cheers,
    Rob
  • John Emmas at Apr 17, 2014 at 11:49 am

    On 17/04/2014 11:34, sisyphus1@optusnet.com.au wrote:
    .
    This is one that comes up from time to time - it's not specific to
    Strawberry Perl, and has to do with file associations and something
    else .... a registry setting ? ... I can never remember the details,
    nor of how to search for it.
    Aaah ... here's the solution I was thinking of:
    http://www.perlmonks.org/?node_id=1024609
    Wow, I'm amazed! I've been programming on Windows for nearly 30 years,
    yet I never encountered this problem before. Nevertheless you're
    absolutely right Rob. Placing the word "perl" at the start of my
    command line solved the problem! Sadly, the fix suggested by that
    article didn't work in my case - but no matter, at least I've got a
    solution now.

    One more question - is there a way to obtain the Windows version
    information using a perl script? For example, can I obtain the value of
    WINVER somehow?

    John
  • Kmx at Apr 17, 2014 at 11:53 am

    On 17.4.2014 13:49, John Emmas wrote:
    On 17/04/2014 11:34, sisyphus1@optusnet.com.au wrote:
    .
    This is one that comes up from time to time - it's not specific to
    Strawberry Perl, and has to do with file associations and something
    else .... a registry setting ? ... I can never remember the details,
    nor of how to search for it.
    Aaah ... here's the solution I was thinking of:
    http://www.perlmonks.org/?node_id=1024609
    Wow, I'm amazed! I've been programming on Windows for nearly 30 years,
    yet I never encountered this problem before. Nevertheless you're
    absolutely right Rob. Placing the word "perl" at the start of my command
    line solved the problem! Sadly, the fix suggested by that article didn't
    work in my case - but no matter, at least I've got a solution now.

    One more question - is there a way to obtain the Windows version
    information using a perl script? For example, can I obtain the value of
    WINVER somehow?
    Check
    https://metacpan.org/pod/Win32#Win32::GetOSDisplayName
    and
    https://metacpan.org/pod/Win32#Win32::GetOSVersion

    --
    kmx
  • Matthew Persico at Apr 17, 2014 at 2:29 pm
    I've been fighting a few Windows boxes here that are balky with assoc and
    ftype. What I've found is that:

    1) You can't have any perl processes open when you run the fype and assoc
    commands.
    2) You have to run ftype first and assoc second.
    3) You must be an Administrator account when you run the fype and assoc
    commands.
    4)*** You must run the fype and assoc commands in a window that your
    started by RIGHT CLICKING A SHORTCUT for cmd.exe and you MUST chose RUN AS
    AN ADMINISTRATOR. Just being an administrator and opening a command window
    is not enough.

    ftype PerlScript="C:\Strawberry\perl\bin\perl.exe" "%1" %*
    assoc .pl=PerlScript

    Obviously, change the path to Perl as per your local install.
    --
    Matthew O. Persico

    Lazard
    30 Rockefeller Plaza
    New York, NY 10112
    212 632 6136



    From: kmx <kmx@atlas.cz>
    To: win32-vanilla@perl.org
    Date: 04/17/2014 07:53 AM
    Subject: Re: Run an external program and capture its output



    On 17.4.2014 13:49, John Emmas wrote:
    On 17/04/2014 11:34, sisyphus1@optusnet.com.au wrote:
    .
    This is one that comes up from time to time - it's not specific to
    Strawberry Perl, and has to do with file associations and something
    else .... a registry setting ? ... I can never remember the details,
    nor of how to search for it.
    Aaah ... here's the solution I was thinking of:
    http://www.perlmonks.org/?node_id=1024609
    Wow, I'm amazed! I've been programming on Windows for nearly 30 years,
    yet I never encountered this problem before. Nevertheless you're
    absolutely right Rob. Placing the word "perl" at the start of my command
    line solved the problem! Sadly, the fix suggested by that article didn't
    work in my case - but no matter, at least I've got a solution now.

    One more question - is there a way to obtain the Windows version
    information using a perl script? For example, can I obtain the value of
    WINVER somehow?
    Check
    https://metacpan.org/pod/Win32#Win32::GetOSDisplayName
    and
    https://metacpan.org/pod/Win32#Win32::GetOSVersion

    --
    kmx
  • Matthew Persico at Apr 18, 2014 at 10:10 pm
    5) Here's a crazy one I just found out today:
    The assoc command cannot have any SPACES after the assoc name. Yep, if you
    have any spaces after PerlScript in

    assoc .pl=PerlScript

    your association will never work. I had spaces because I generate the
    command in a script like this:

    echo ::@echo off >
    %post_exp_bat%
    echo :: >>
    %post_exp_bat%
    echo set owd=%%cd%% >>
    %post_exp_bat%
    echo :: >>
    %post_exp_bat%
    echo rem This should put us one level up, in order to link >>
    %post_exp_bat%
    echo rem then directory 'perl' to the strawberry dir >>
    %post_exp_bat%
    echo cd .. >>
    %post_exp_bat%
    echo :: >>
    %post_exp_bat%
    echo echo Remove any existing link >>
    %post_exp_bat%
    echo if exist perl rmdir perl >>
    %post_exp_bat%
    echo :: >>
    %post_exp_bat%
    echo echo Link the new version >>
    %post_exp_bat%
    echo mklink /d perl %%owd%% >>
    %post_exp_bat%
    echo echo Now add Perl to PATH >>
    %post_exp_bat%
    echo cd perl >>
    %post_exp_bat%
    echo call update_env.pl.bat --do >>
    %post_exp_bat%
    echo echo Associating .pl with perl.exe >>
    %post_exp_bat%
    echo ftype PerlScript="%%cd%%\bin\perl.exe" "%%1" %%* >>
    %post_exp_bat%
    echo assoc .pl=PerlScript >>
    %post_exp_bat%
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    see all these spaces ----||||||||||||||||||||||||||||||||||

    It was those spaces that were screwing be up.
    --
    Matthew O. Persico

    Lazard
    30 Rockefeller Plaza
    New York, NY 10112
    212 632 6136



    From: Matthew Persico/ITS/Lazard
    To: kmx <kmx@atlas.cz>
    Cc: win32-vanilla@perl.org
    Date: 04/17/2014 10:29 AM
    Subject: Re: Run an external program and capture its output


    I've been fighting a few Windows boxes here that are balky with assoc and
    ftype. What I've found is that:

    1) You can't have any perl processes open when you run the fype and assoc
    commands.
    2) You have to run ftype first and assoc second.
    3) You must be an Administrator account when you run the fype and assoc
    commands.
    4)*** You must run the fype and assoc commands in a window that your
    started by RIGHT CLICKING A SHORTCUT for cmd.exe and you MUST chose RUN AS
    AN ADMINISTRATOR. Just being an administrator and opening a command window
    is not enough.

    ftype PerlScript="C:\Strawberry\perl\bin\perl.exe" "%1" %*
    assoc .pl=PerlScript

    Obviously, change the path to Perl as per your local install.
    --
    Matthew O. Persico

    Lazard
    30 Rockefeller Plaza
    New York, NY 10112
    212 632 6136




    From: kmx <kmx@atlas.cz>
    To: win32-vanilla@perl.org
    Date: 04/17/2014 07:53 AM
    Subject: Re: Run an external program and capture its output



    On 17.4.2014 13:49, John Emmas wrote:
    On 17/04/2014 11:34, sisyphus1@optusnet.com.au wrote:
    .
    This is one that comes up from time to time - it's not specific to
    Strawberry Perl, and has to do with file associations and something
    else .... a registry setting ? ... I can never remember the details,
    nor of how to search for it.
    Aaah ... here's the solution I was thinking of:
    http://www.perlmonks.org/?node_id=1024609
    Wow, I'm amazed! I've been programming on Windows for nearly 30 years,
    yet I never encountered this problem before. Nevertheless you're
    absolutely right Rob. Placing the word "perl" at the start of my command
    line solved the problem! Sadly, the fix suggested by that article didn't
    work in my case - but no matter, at least I've got a solution now.

    One more question - is there a way to obtain the Windows version
    information using a perl script? For example, can I obtain the value of
    WINVER somehow?
    Check
    https://metacpan.org/pod/Win32#Win32::GetOSDisplayName
    and
    https://metacpan.org/pod/Win32#Win32::GetOSVersion

    --
    kmx

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupwin32-vanilla @
categoriesperl
postedApr 17, '14 at 6:47a
activeApr 18, '14 at 10:10p
posts13
users4
websitestrawberryperl.com

People

Translate

site design / logo © 2019 Grokbase