I hope I understood your problem right. I think your problem is that $command may contain characters that are special for the shell, and they cause the open to fail.

Answers to these problems can be found in man perlipc and perldoc -f exec. What you need to do is a "safe pipe open", opening a pipe that doesn't involve the shell. The way to do this is forking your process, with a pipe from the child to the parent, and then doing an exec to $command in the child.

Forking a child and opening a pipe between them can be done in a single Perl command:

my $pid = open my $kid => "-|";

This forks the program, returning the child PID in the parent, while opening a pipe from the child to the parent. If the fork fails, $pid is undefined.

The next tricky thing in the exec. If we would do a simple exec $command, Perl would call the shell if $command contains special characters, and that is what we are trying to avoid. If the command had arguments, we could supply exec with a list (of more than one element) and exec would avoid calling the shell, but we don't have that option. But there is another way we can have exec avoid calling the shell, and that is by giving it a block as first argument. The result of the block will be how the program we are going to call is named, so we can just supply $command. This would give us:

exec {$file} $file or die "exec() failed: $!\n";

A complete program that does a safe pipe open:

#!/usr/bin/perl use strict; use warnings; my $file = '....'; # Command with special characters. my $pid = open my $kid => "-|"; die "fork() failed: $!\n" unless defined $pid; unless ($pid) { exec {$file} $file or die "exec() failed: $!\n"; } while (<$kid>) { print; } __END__

Abigail


In reply to Re: Replacing charecters in files by Abigail-II
in thread Replacing charecters in files by raj8

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.