1

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;
    }
}

2 Answers 2

1
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)
Sign up to request clarification or add additional context in comments.

7 Comments

when i used system('cat',$file) i'am getting the below 'cat' is not recognized as an internal or external command, operable program or batch file.
Do you have cat? If so, fix your path. If not, what do you expect?
Not sure if i'am following you..i see the values in the Loop for variable $file are coming with absolute path passing it to cat command.
Did you install cat on your machine? No version of Windows comes with it. Like you said, it's a unix tool.
ahh, that explains.. i was on windows machine trying to run the above code, So, what if i want use something DOS equivalent to stuff up the array in my code, what should i do?
|
1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.