Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Using IPC::Open3 instead of backtick operator

by karlgoethebier (Abbot)
on Jun 11, 2016 at 12:16 UTC ( [id://1165393]=note: print w/replies, xml ) Need Help??


in reply to Using IPC::Open3 instead of backtick operator

..."...become a better Perl programmer...update the script to pass the perlcritic test..."

I fear you won't become a better Perl programmer if you use perlcritic. The opposite might happen and this would be annoying. From the linked node by our beloved deputy of the leader BUK:

..."Fully 95% of Perl::Critic's (and by implication PBP) justifictions are as puerile as banning ball games from school playgrounds because participants might skin their knees. Making their own mistakes and learning from them is how kids learn. Banning every construct and idiom, that might under some obscure circumstances cause the occasional program to fail, is like trying to wrap your kids in cotton wool. Overindulgent, counter-productive and ultimately futile..."

Below some code that i wrote a while ago for learning purposes and demonstration - how to capture everything from a compound external command:

#!/usr/bin/env perl use strict; use warnings; use IPC::Run qw( run harness ); use Try::Tiny; use Carp; my $file = shift || $0; my @cat = ( "cat", $file ); my @wc = qw( wc -l ); my ( $error, $result ); my @command = ( \@cat, '|', \@wc, '1>', \$result, '2>', \$error ); my $harness = harness @command; try { run $harness; croak $error if $harness->full_result; } catch { print qq(Error: $_); }; chomp $result; printf( "%s:\t%s\n", "result", $result ); printf( "%s:\t%s\n", "cat", $harness->full_result(0) ); printf( "%s:\t%s\n", "wc", $harness->full_result(1) ); __END__

Some mentioned already that IPC::Run might be the better tool.

The code fails with perlcritic, despite it is IMHO well formatted, easy to read, easy to understand and therefore easy to maintain. And it does what it should do.

karls-mac-mini:Desktop karl$ perlcritic -1 ipc.pl Code is not tidy at line 1, column 1. See page 33 of PBP. (Severity: + 1) No package-scoped "$VERSION" variable found at line 1, column 1. See +page 404 of PBP. (Severity: 2) Magic punctuation variable $0 used at line 9, column 21. See page 79 +of PBP. (Severity: 2) Useless interpolation of literal string at line 11, column 13. See pa +ge 51 of PBP. (Severity: 1) Quotes used with a noisy string at line 17, column 24. See page 53 of + PBP. (Severity: 2) Return value of flagged function ignored - print at line 26, column 5. + See pages 208,278 of PBP. (Severity: 1) Useless interpolation of literal string at line 31, column 21. See pa +ge 51 of PBP. (Severity: 1) Builtin function called with parentheses at line 33, column 1. See pa +ge 13 of PBP. (Severity: 1) Useless interpolation of literal string at line 33, column 22. See pa +ge 51 of PBP. (Severity: 1) Builtin function called with parentheses at line 35, column 1. See pa +ge 13 of PBP. (Severity: 1) Useless interpolation of literal string at line 35, column 22. See pa +ge 51 of PBP. (Severity: 1)

OK, the command is idiotic, but perlcritic doesn't complain about that it is idiotic.

No Perl shari'ah, thank's.

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1165393]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found