dagolden / Capture-Tiny

(Perl) Capture STDOUT and STDERR from Perl, XS or external programs
http://search.cpan.org/dist/Capture-Tiny/
39 stars 19 forks source link

chomping on a capture #33

Closed grtodd closed 9 years ago

grtodd commented 9 years ago

Thank you for Capture::Tiny.

When I don't want to chomp a capture:

my ($stdout, $stderr, @fsinfo) = capture{  
     map{ [ split/\s+/ ] } map { split/\n/ } 
     qx/mount -p -t zfs/ } ;

.. when I do:

 # array of lines
 my ($stdout, $stderr, @fsinfo)  = capture{  map { chomp; $_ } qx/ mount -p / } ;  
 # a big string
 my ($stdout, $stderr, $fsinfo)  = capture{ local $/; qx/ mount -p / } ;     

Is there a preferred way to chomp that could be documented with line or two of POD?

I'm not qualified judge whether a function like capture_chomp{ ... } (i.e. if it saved more than a trivial number of characters or was safer/faster/more correct) would look silly on such a great module :-)

Thanks again ++

dagolden commented 9 years ago

I think that's going to be weird since fsinfo will contain the captured stdout and stderr as well as the return value of the block.

The two examples are very different so I'm having trouble understanding what you're trying to achieve.

I don't think capture_chomp makes sense because you're looking to chomp the results, not the captured output, which only makes sense if you're returning lines of text as the result. That's too much of a special case.

grtodd commented 9 years ago

Sometimes I fool myself with github's UI into filing issues that really are more like mailing list discussions :-|

Anyway, my examples were a bit hastily cut and pasted - I'll change them so they are a bit more clear but will close this "issue". I agree: chomping lines of text is a special case even in my limited retinue of quick hacks :-) and local $/ map { chomp; $_ } can handle this depending on whether you want a big long string or line by line array.

Capture::Tiny makes it so easy to grab the output from external commands in a readable way that I've become spoiled.

dagolden commented 9 years ago

I'm always happy to have discussions with people using my code, so no worries. :-)