How do I get the output of an external command in Perl?

后端 未结 7 1383
闹比i
闹比i 2020-12-03 13:45

I want to have output of Windows command-line program (say, powercfg -l) written into a file which is created using Perl and then read the file line by line in

相关标签:
7条回答
  • 2020-12-03 14:35
    my $output = qx(powercfg -l);
    
    ## You've got your output loaded into the $output variable. 
    ## Still want to write it to a file?
    open my $OUTPUT, '>', 'output.txt' or die "Couldn't open output.txt: $!\n";
    print $OUTPUT $output;
    close $OUTPUT
    
    ## Now you can loop through each line and
    ##   parse the $line variable to extract the info you are looking for.
    foreach my $line (split /[\r\n]+/, $output) {
      ## Regular expression magic to grab what you want
    }
    
    0 讨论(0)
提交回复
热议问题