This does have the unfortunate side effect of not allowing someone to do:package Chomp; use Scalar::Readonly ':all'; BEGIN { *CORE::GLOBAL::chomp = sub { readonly_on( $/ ); my ($count, $fix) = (0, ''); local $/ = "\r\n"; for ( @_ ) { my ($first, $second) = (0, 0); eval { $first = chomp }; if ( $@ ) { die $@ if $_ !~ /^\r?\n$/; $fix = $_; last; } if ( ! $first ) { local $/ = "\n"; $second = chomp; } $count += $first + $second; } readonly_off( $/ ); return $fix ? ($count , $fix) : $count; }; } 42; # Then a script that uses it #!/usr/bin/perl use strict; use warnings; use Chomp; my $foo = "foo\n\r\n"; my $bar = "bar\n\n\n"; print chomp $foo, $/; # prints 2 print chomp $bar, "\n"; # prints 1 print chomp ($foo, $bar), "\n"; # prints 2
I know this is ugly and there are probably a few more gotchas in there, but It was kind of fun to work on. Note: This can be done without a module, but it is much uglier. Anyone wanting to see that should say so.chomp($/); # $/ = undef;
Cheers - L~R
In reply to Re: Re: Re: Redefining chomp()
by Limbic~Region
in thread Redefining chomp()
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |