[Techtalk] using shell commands in perl scripts

Berenice showercurtain2000 at yahoo.com
Sun May 25 03:15:50 EST 2003


Thanks Rasjid,

I understand how they work now.  I read up on some of them and have a
couple of questions...

1. Using the system function runs /bin/ls as a child process. What's
the purpose/advantage of using a child process?

2. I'm new to the idea of using pipe with open and filehandles, like
this:  open (LSPIPE, "/bin/ls |");  Is the purpose of the pipe to
direct output of /bin/ls to the LSPIPE filehandle?

3.  I looked at man perlsec about taint testing, in regards to 
#!/usr/bin/perl -Tw.  It sounds like a good idea, but why do I get
this message when I run the script with the -T option:
> Too late for "-T" option at bash_in_perl line 1.

cheers
Berenice


--- Rasjid Wilcox <rasjidw at openminddev.net> wrote:
> On Sunday 25 May 2003 12:47, Berenice wrote:
> > I've just started perl as part of my course and I'm wondering how
> you
> > can use commands such as rm, mkdir and less in perl scripts.
> 
> Below is a short program that illustrates three possible ways to
> interact with 
> command in perl.  This is perl, so there are almost certainly
> others.
> 
> #!/usr/bin/perl -Tw
> 
> $ENV{PATH}="";  # required due to taint testing
> 
> # Method 1: using back-ticks
> 
> print "Result:\n";
> my $result = `/bin/ls`;
> 
> print $result;
> 
> print "\n";
> 
> # Method 2: using system
> 
> my $result2 = system("/bin/ls");
> 
> print "\nResult2: $result2\n";
> 
> # Method 3: using open
> 
> print "\n\nNow using a while loop:\n";
> open (LSPIPE, "/bin/ls |");
> 
> while (my $line = <LSPIPE>) {
>     print $line;
> }
> 
> ---------------------
> 
> Note that method 1 gives the entire output in a single variable. 
> Method 2 
> only returns the exit code, perl itself does not see the output of
> the 
> command.  Method 3 allows line by line action on the output of the
> command.
> 
> Cheers,
> 
> Rasjid.
> 
> 
> -- 
> 
> Rasjid Wilcox
> Canberra, Australia  UTC + 10
> http://www.openminddev.net


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com


More information about the Techtalk mailing list