Article View: comp.lang.perl.misc
Article #265Re: Most efficient method to call a system routine and capture it's output
From: Michael Carman
Date: Fri, 19 Jul 2002 15:24
Date: Fri, 19 Jul 2002 15:24
65 lines
3524 bytes
3524 bytes
On 7/19/02 10:00 AM, Ralph SOBEK wrote:
>
> Since I have not seen any followups or received any e-mail,
> probably due to everyone being already on vacation,
The whole world goes on vacation at the same time?!
> What is the most efficient method to call a system routine
> repeatedly and capture it's short output?
>
> Supposedly, in loop:
>
> #1 system @cmd;
>
> is efficient. But using backquotes:
>
> #2 (my $sum = `@cmd 2>/dev/null`) =~ s/\s+.*$//;
>
> captures the output correctly, but is horrendously slow, if repeated a
> couple of thousand times.
Perl provides three methods of executing external programs:
* system() - To execute a program and capture its exit status.
* `` (backticks) - To execute a program and capture its output.
* exec() - To replace the current instance of perl with another
program.
If you want to capture output, you use backticks. If it's slower than
system(), well... that's because system() doesn't have to fuss with
capturing output.
> Is there a possibility to prepare the fork in #1 in such a way
> as to recover the forks output afterwards?
I don't think so, but if there were it would probably end up being just
as slow as backticks, and then we'd be back where we started.
> Furthermore, in #2 there is the additional problem that the
> shell causes with unusual file names.
I take it you're referring to filenames that contain shell wildcard
characters? system() will do wildcard expansion as well if called with a
single string argument instead of a list.
> Any suggestions?
I would try using system() but having your external command redirect
(appending) output to a file. Then when you're finished, read in the file.
> Please do not reply directly to this posting, but use the
> addresses below in my signature. This is to thwart those
> mean SPAMmers!
How does my /responding/ to your post here help spammers?
> Please reply by e-mail, if at all possible, and I will
> summarize to the net if interest warrants.
Did you not see Tad's post? Usenet is *not* a personal helpdesk. It's
acceptable to ask for a Cc: of posts, but asking for answers that only
you see is rude. That's likely to be at least part of the reason that no
one has responded.
-mjc
Technical Details
Message-ID:
<3D382F17.1000506@mchsi.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!howland.erols.net!news.airnews.net!cabal12.airnews.net!cabal10.airnews.net!news-chi-1.sprintlink.net!news-central.sprintlink.net!news-in-central.sprintlink.net!news.sprintlink.net!onews.collins.rockwell.com!not-for-mailReferences:
<ausn2ib8ny.fsf@irit.irit.fr> <aud6tjbw4d.fsf@irit.irit.fr>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 112333,d48ce9f80ca98172 X-Google-Attributes: gid112333,public X-Google-Thread: fa0ae,4cb838d93b34e411 X-Google-Attributes: gidfa0ae,public X-Google-ArrivalTime: 2002-07-19 09:11:49 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!howland.erols.net!news.airnews.net!cabal12.airnews.net!cabal10.airnews.net!news-chi-1.sprintlink.net!news-central.sprintlink.net!news-in-central.sprintlink.net!news.sprintlink.net!onews.collins.rockwell.com!not-for-mail From: Michael Carman <mjcarman@mchsi.com> Newsgroups: comp.lang.perl.misc,fr.comp.lang.perl Subject: Re: Most efficient method to call a system routine and capture it's output Date: Fri, 19 Jul 2002 10:24:07 -0500 Organization: None (anarchist) Lines: 66 Message-ID: <3D382F17.1000506@mchsi.com> References: <ausn2ib8ny.fsf@irit.irit.fr> <aud6tjbw4d.fsf@irit.irit.fr> NNTP-Posting-Host: fw01.rockwellcollins.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.1a) Gecko/20020611 X-Accept-Language: en-us, en