Matt
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
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
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/
Easy enough. :-)
my $count = () = `another_application`;
print "$count\n";
John
--
use Perl;
program
fulfillment
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
can you point out the section you refer to?
gnari
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'
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
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 *
> > 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
>>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