CUFP
Juerd
snippet
<div class="Description"><P><em>19:50 <@[Petruchio]> Right now I'm inclined to code up a subroutine to overload
chomp, so that it'll chomp every scalar value it finds
recursively throughout a nested data structure.</em><br>
</p><p>
I could not refrain myself from coding it immediately, so here it is :)<br>
</p><p>
<strong>Update</strong> (200112202209+0100) It _does_ work, and doesn't use the outer $_. The line containing "#### <<< :-)" explains why. (for-modifier). [merlyn] was right about this not being easy to maintain. Sorry about that, let's just hope it doesn't need maintenance, then.<br>
</p><p>
<strong>Update</strong> (200112211546+0100) [gbarr] uses defined() in his not-recursive version. I should have done so too, so I fixed that mistake. Also made it work with references to references.<br>
</p> </div>
<CODE>
sub mychomp; # Needed so mychomp can be used without parens
# from within mychomp
sub mychomp {
ref eq 'ARRAY' ? do { mychomp $_ for @$_ } :
ref eq 'HASH' ? do { mychomp $_ for values %$_ } :
ref eq 'REF' ? do { mychomp $$_ } :
ref eq 'SCALAR' ? eval { defined && chomp $$_ } :
ref || defined && chomp for @_ #### <<< :-)
}
# Useful stuff ends here :)
# Testing stuff starts here :)
$foo = "scalar\n";
@foo = (["first\n", "second\n", "third\n"], [[ "FIRST\n", "SECOND\n", "THIRD\n"],
[{xyzzy => "1st\n"}],{ xyzzy => "1<sup>st</sup>\n"} ],
"Just testing\n", \ $foo, \ \ \ $foo, \ [ "Testing :)\n" ],
[[[[[[[[[[[[[[[[[ "DEEEEEPER!\n" ]]]]]]]]]]]]]]]]]
);
mychomp @foo;
use Data::Dumper;
print Data::Dumper->Dump(\@foo);
# oh, and $foo is chomped too, of course
</code>