id like to include a unix command in my perl code.
would you please tell me how to make a "ll" or "cd .." from my perl programm.
tanx
There are a few ways to do this. Which one you use depends upon what you'd like to do with the output of commands you run - if you'd like to do anything at all.
system() function
system("ls -l"
That runs the ls -l command, the output will appear on your screen. This is the easiest way to run an external command. It's difficult to do anything with the output though.
backticks
$a = `ls -l`;
or
@a = `ls -l`;
Backticks let you run a command and capture the output all in one go so that you can process it later.
The first example puts all of the output of ls -l in one Perl variable, a scalar variable as they say.
The second example puts each line of the output into an element of the @a array. The first line of the output from ls -l will be in $a[0], the second in $a[1] and so on.
Using a pipe.
This is a way of processing data from a command as it gets produced by that command, you don't have to wait for the command to finish before working with the data. It's useful when you're running a command that might take a long while to finish - or maybe it will never finish and you need to process data from it constantly.
open(OP, "ls -l |"
while(<OP>){
print $_;
}
close OP;
I hope that this goes someway to answering your query.
Mike
Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
It's like this; even samurai have teddy bears, and even teddy bears get drunk.
tanx mike.
your help has been defenitely helpful.
but remember
if you get drunk you go to sleep
if you go to sleep you commit no sin
if you commit no seen you go to heaven
so let's get drunk to go to heaven
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.