Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Running Another Application

2 views
Skip to first unread message

Matt

unread,
Jan 17, 2004, 2:32:09 PM1/17/04
to
I need my perl application to run another application, count the number of
lines it returns and return that number as its output. Sounds so simple. I
imagine I use "exec" then somehow capture its output. Then somehow I get
what the script(radwho) returned in my application and then print the line
count to the command line with print. How do I get what the other script
returned?

Matt


gnari

unread,
Jan 17, 2004, 2:45:57 PM1/17/04
to
"Matt" <nospam...@yahoo.com> wrote in message
news:100j3f1...@corp.supernews.com...

there are several ways to do it, of course, but 'exec' is not one of them.
this is a FAQ :
perldoc -q "output of a command"

in short : qx()

gnari


W. Citoan

unread,
Jan 17, 2004, 4:47:27 PM1/17/04
to

Use backticks...

my $results = `/path-to/script`;

- W. Citoan
--
Be circumspect in your liaisons with women. It is better to be seen at
the opera with a man than at mass with a woman.
-- De Maintenon

David Efflandt

unread,
Jan 17, 2004, 6:05:09 PM1/17/04
to
On Sat, 17 Jan 2004 21:47:27 -0000, W. Citoan <wci...@NOSPAM-yahoo.com> wrote:
> On Sat, 17 Jan 2004 13:32:09 -0600, Matt wrote:
>> I need my perl application to run another application, count the number of
>> lines it returns and return that number as its output. Sounds so simple. I
>> imagine I use "exec" then somehow capture its output. Then somehow I get
>> what the script(radwho) returned in my application and then print the line
>> count to the command line with print. How do I get what the other script
>> returned?
>
> Use backticks...
>
> my $results = `/path-to/script`;

Since he wanted a line count, a list may be easier, example:

my @results = `/usr/bin/who`;
print scalar @results . " lines\n";
foreach (@results) { print; }

--
David Efflandt - All spam ignored http://www.de-srv.com/

John W. Krahn

unread,
Jan 18, 2004, 12:34:05 AM1/18/04
to

Easy enough. :-)

my $count = () = `another_application`;
print "$count\n";


John
--
use Perl;
program
fulfillment

Eric Wilhelm

unread,
Jan 18, 2004, 12:26:20 PM1/18/04
to

Backticks are the simplest way, but beware the security implications and
lack of robustness.


And. exec() is one of the ways to do this:
perldoc perlopentut

--Eric

gnari

unread,
Jan 18, 2004, 1:16:58 PM1/18/04
to
"Eric Wilhelm" <ewil...@somethinglike.sbcglobalDOTnet> wrote in message
news:pan.2004.01.18.11...@somethinglike.sbcglobalDOTnet...
[snip discussion about grabbing the output of a program]

>
> And. exec() is one of the ways to do this:
> perldoc perlopentut
>

can you point out the section you refer to?

gnari


Ben Morrow

unread,
Jan 18, 2004, 6:25:03 PM1/18/04
to

Given that perlopentut does not mention exec, my guess would be he's
referring to 'Safe Pipe Opens' in perlipc, which includes and exec in
its emulation of system. Of course, it is preceded by a fork...

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ b...@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'

Naveen Reddy

unread,
Jan 19, 2004, 11:55:38 AM1/19/04
to
"John W. Krahn" <kra...@acm.org> wrote in message news:<400A1AC1...@acm.org>...


Hello all,
I'm trying to learn Perl and have a question about the above solution.


> my $count = () = `another_application`;

What does the "()" in the line above mean?
Does it mean, take the output of `another application` in a scalar context?
I retried the above solution w/o the () as:

my $count = `ls` ;
print "$count\n" ;

And it printed all the elements in my pwd.
Does assigning () to the output make it a scalar?
Just curious :)

Thanks for your time,
Reddy

Ben Morrow

unread,
Jan 19, 2004, 12:31:40 PM1/19/04
to
[please follow-up in a more conventional style]

naveen_r...@hotmail.com (Naveen Reddy) wrote:
> "John W. Krahn" <kra...@acm.org> wrote in message news:<400A1AC1...@acm.org>...

> I'm trying to learn Perl and have a question about the above solution.
> > my $count = () = `another_application`;
>
> What does the "()" in the line above mean?
> Does it mean, take the output of `another application` in a scalar
> context?

No, rather the opposite. It evaluates the `` in list context, and then
(as the () = `` is in scalar context) returns a count of the values
returned: in this case, the number of lines.

> I retried the above solution w/o the () as:
>
> my $count = `ls` ;
> print "$count\n" ;
>
> And it printed all the elements in my pwd.

Yes. This simply evaluates the `` in scalar context, when it returns
all the output rather than splitting it into lines.

> Does assigning () to the output make it a scalar?

I think you have you associativity wrong... = associates
right-to-left, which means that the above expression is equivalent to

my $count = ( () = `...` );

rather than

( my $count = () ) = `...`;

. So the output of `` is being assigned to (), and then the result of
that expression (in sclar context) is being assigned to $count.

Ben

--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* b...@morrow.me.uk *

Anno Siegel

unread,
Jan 19, 2004, 12:58:17 PM1/19/04
to
Naveen Reddy <naveen_r...@hotmail.com> wrote in comp.lang.perl.misc:

> "John W. Krahn" <kra...@acm.org> wrote in message
> news:<400A1AC1...@acm.org>...

> > Easy enough. :-)


> >
> > my $count = () = `another_application`;
> > print "$count\n";

> Hello all,
> I'm trying to learn Perl and have a question about the above solution.
> > my $count = () = `another_application`;
>
> What does the "()" in the line above mean?
> Does it mean, take the output of `another application` in a scalar context?

The contrary. It puts the backtick operation is list context.

> I retried the above solution w/o the () as:
>
> my $count = `ls` ;
> print "$count\n" ;
>
> And it printed all the elements in my pwd.

Sure. That's the normal behavior.

> Does assigning () to the output make it a scalar?

Oh boy. No, that's not at all what's happening.

First, the result of `ls` is assigned to an empty list (). That doesn't
seem to make much sense, but it surely puts `ls` into list context.
However, in Perl, assignments have a value, and that value is assigned
to the scalar $count, so it is taken in scalar context.

Now it is one of the quirks of Perl that the *scalar* value of a list
assignment is the number of elements in the original list, and not the
number of elements that were actually assigned. This is in stark contrast
to the behavior of a list assignment in list context, whose value *is*
the list of elements after the assignment. The net result is that you
can do both, capture a few initial elements of a list, and find how many
elements it has in total with a series of two assignments. In the
limiting case you capture 0 variables in an empty list, but still have
list context to the right and scalar context to the left.

> Just curious :)

It's called DWIM.

Anno

Joe Smith

unread,
Jan 20, 2004, 8:10:40 AM1/20/04
to
Naveen Reddy wrote:

>>my $count = () = `another_application`;
>
> What does the "()" in the line above mean?
> Does it mean, take the output of `another application` in a scalar context?

No, just the opposite. Think of it as
my @lines = `another_application`'
my $count = @lines;
but without actually using the intermediate array.

-Joe

Naveen Reddy

unread,
Jan 21, 2004, 10:08:40 AM1/21/04
to
Thanks everbody for your responses.
0 new messages