I want to learn about best practices here. Suppose I want to get the contents of some line in a file. I can use a single line shell command to get a response or write a subroutine as shown in the code below.
A text file with the name some_text:
She laughed. Then both continued eating in silence, like strangers,
but after dinner they walked side by side; and there sprang up
between them the light jesting conversation of people who are free
and satisfied, to whom it does not matter where they go or what
they talk about.
Code to get the contents of line 5 of the file
use warnings;
use strict;
my $file = "some_text";
my $lnum = 5;
my $shellcmd = "awk 'NR==$lnum' $file";
print qx($shellcmd);
print getSrcLine($file, $lnum);
sub getSrcLine {
my($file, $lnum) = @_;
open FILE, $file or die "$!";
my @ray = <FILE>;
return $ray[$lnum-1];
}
I ask about this because I see many Perl scripts where at some point a shell command is called, and at some later point the same task was performed by calling a (library or handwritten) function, for example, rm -rfagainst File::Path::rmtree. I just want to make it consistent.
What is recommended to do?