Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:18:13+00:00 2026-05-23T08:18:13+00:00

I’m trying to wrap my head around IPC::Run to be able to do the

  • 0

I’m trying to wrap my head around IPC::Run to be able to do the following. For a list of files:

my @list = ('/my/file1.gz','/my/file2.gz','/my/file3.gz');

I want to execute a program that has built-in decompression, does some editing and filtering to them, and prints to stdout, giving some stats to stderr:

~/myprogram options $file

I want to append the stdout of the execution for all the files in the list to one single $out file, and be able to parse and store a couple of lines in each stderr as variables, while letting the rest be written out into separate fileN.log files for each input file.
I want stdout to all go into a “>>$all_into_one_single_out_file”, it’s the err that I want to keep in different logs.

After reading the manual, I’ve gone so far as to the code below, where the commented part I don’t know how to do:

for $file in @list {
  my @cmd;
  push @cmd, "~/myprogram options $file";
  IPC::Run::run \@cmd, \undef, ">>$out", 
    sub { 
      my $foo .= $_[0]; 
      #check if I want to keep my line, save value to $mylog1 or $mylog2
      #let $foo and all the other lines be written into $file.log
    };
}

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:18:14+00:00Added an answer on May 23, 2026 at 8:18 am

    First things first. my $foo .= $_[0] is not necessary. $foo is a new (empty) value, so appending to it via .= doesn’t do anything. What you really want is a simple my ($foo) = @_;.

    Next, you want to have output go to one specific file for each command while also (depending on some conditional) putting that same output to a common file.

    Perl (among other languages) has a great facility to help in problems like this, and it is called closure. Whichever variables are in scope at the time of a subroutine definition, those variables are available for you to use.

    use strict;
    use warnings;
    
    use IPC::Run qw(run new_chunker);
    
    my @list           = qw( /my/file1 /my/file2 /my/file3 );
    
    open my $shared_fh, '>', '/my/all-stdout-goes-here' or die;
    open my $log1_fh, '>', '/my/log1' or die "Cannot open /my/log1: $!\n";
    open my $log2_fh, '>', '/my/log2' or die "Cannot open /my/log2: $!\n"; 
    
    foreach my $file ( @list ) {
      my @cmd = ( "~/myprogram", option1, option2, ..., $file );
    
      open my $log_fh, '>', "$file.log"
          or die "Cannot open $file.log: $!\n";
    
      run \@cmd, '>', $shared_fh,
                 '2>', new_chunker, sub {
          # $out contains each line of stderr from the command
          my ($out) = @_;
          if ( $out =~ /something interesting/ ) {
              print $log1_fh $out;
          }
          if ( $out =~ /something else interesting/ ) {
              print $log2_fh $out;
          }
          print $log_fh $out;
          return 1;
        };
    }
    

    Each of the output file handles will get closed when they’re no longer referenced by anything — in this case at the end of this snippet.

    I fixed your @cmd, though I don’t know what your option1, option2, … will be.

    I also changed the way you are calling run. You can call it with a simple > to tell it the next thing is for output, and the new_chunker (from IPC::Run) will break your output into one-line-at-a-time instead of getting all the output all-at-once.

    I also skipped over the fact that you’re outputting to .gz files. If you want to write to compressed files, instead of opening as:

    open my $fh, '>', $file  or die "Cannot open $file: $!\n";
    

    Just open up:

    open my $fh, '|-', "gzip -c > $file"  or die "Cannot startup gzip: $!\n";
    

    Be careful here as this is a good place for command injection (e.g. let $file be /dev/null; /sbin/reboot. How to handle this is given in many, many other places and is beyond the scope of what you’re actually asking.

    EDIT: re-read problem a bit more, and changed answer to more closely reflect the actual problem.

    EDIT2:: Updated per your comment. All stdout goes to one file, and the stderr from command is fed to the inline subroutine. Also fixed a stupid typo (for syntax was pseudo code not Perl).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.