user1627901
user1627901

Reputation: 159

how to get results into array from unix commands

I'm trying to get the dump of each file into an array from a unix command in a Perl script. below is the error I'm getting. Could anyone please help me fix this issue?

Can't locate object method "cat" via package "C:/prac/cmm_ping.txt" (perhaps you forgot to load "C:/test/cmm_ping.txt"?) at fandd.pl line 25.

below is my program

#!/usr/bin/perl

use warnings;

@files=glob("C:/prac/*");

foreach $file (@files){
   @data=system(cat  $file);
      foreach $line (@data){`
        print $line;
    }
}

Upvotes: 1

Views: 934

Answers (2)

user1627901
user1627901

Reputation: 159

I took a different route for the issue I was having about running the Unix commands in Perl, and I was able to figure that out with the below code.

@files = <C:/prac/*.ext>;

for $file (@files){
  open (FILE, "$file")             or die $!;
  open (OUT,">> C:/prac/data.txt") or die $!;

  while($line= <FILE> ) {
    print OUT $line if $line =~ /something/ ;
  }

  close FILE;
  close OUT;
}

Upvotes: 1

ikegami
ikegami

Reputation: 386361

system(cat $file)

contains an indirect method call. The above is equivalent to:

system($file->cat)

You meant

system("cat $file")

but that's wrong since you don't convert $file into a shell literal. It's best to avoid creating a shell command entirely by bypassing a shell you don't need anyway.

system('cat', $file)

Upvotes: 1

Related Questions