Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't I use shell tools in Perl code?

Tags:

perl

It is generally advised not to use additional linux tools in a Perl code; e.g if someone intends to print the last line of a text file he can:

$last_line = `tail -1 $file` ;

or otherwise, open the file and read it line by line

 open(INFO,$file);
 while(<INFO>) {
   $last_line = $_ if eof;
   }

What are the pitfalls of using the previous and why should I avoid using shell tools in my code?

thanx,

like image 943
sud03r Avatar asked Nov 26 '22 20:11

sud03r


1 Answers

  • Efficiency - you don't have to spawn a new process
  • Portability - you don't have to worry about an executable not existing, accepting different switches, or having different output
  • Ease of use - you don't have to parse the output, the results are already in a usable form
  • Error handling - you have finer-grained control over errors and what to do about them in Perl.
like image 139
Chas. Owens Avatar answered Dec 11 '22 08:12

Chas. Owens