Dunce
view release on metacpan or search on metacpan
lib/Dunce/Files.pm view on Meta::CPAN
chop() works a little differently. Using it in void context is fine,
but if it looks like you're using it to strip newlines it will throw a
warning reminding you about chomp().
B<NOTE> chop() was non-overridable before 5.7.0, so this feature will
only work on that perl or newer.
=cut
# Alas, chop often isn't overridable.
if( prototype("CORE::chop") ) {
override('chop',
sub {
# Hmm, should this be \n or (\012|\015)?
if( grep { /\n$/s } @_ ? @_ : $_ ) {
carp "Looks like you're using chop() to strip newlines. ".
"Use chomp() instead.\n";
}
}
);
}
=pod
=item B<dbmopen>
dbmopen() will warn you if the hash argument you gave it already
contains data.
=cut
override('dbmopen',
sub {
my $hash = $_[0];
carp "Hash given to dbmopen() already contains data"
if keys %$hash;
my $wantarray = (caller(1))[5];
carp "You didn't check if chmod() succeeded"
unless defined $wantarray;
}
);
=pod
=item B<open>
I<NOT YET IMPLEMENTED>
open() will warn you if you don't close its filehandle explicitly
before the program ends. It will also warn if you give it an already
open filehandle.
XXX I'd also like to have made sure $! is checked, but $! can't be
usefully tied. :(
=pod
#'#
# Waiting on postamble callbacks in Function::Override.
=cut
=item B<opendir>
I<NOT YET IMPLEMENTED>
Same as open().
=back
=head1 CAVEATS
Because of the way perl compiles, the following code will produce a
'Name main::FILE used only once: possible typo' where it shouldn't.
use Dunce::Files;
open(FILE, $filename) or die $!;
print <FILE>;
Because open() is really Dunce::Files::open() and not the real open,
Perl doesn't realize that FILE is the filehandle *FILE, so it thinks
its only being used once.
Turns out this is a useful feature. If you close FILE the warning
will go away, and you should have closed it in the first place.
=head1 TODO
Make a flag to have Dunce::Files die instead of just warning.
Complete Function::Override so I can finish open() and opendir().
=head1 AUTHOR
Michael G Schwern <schwern@pobox.com> with help from crysflame and
Simon Cozens. Thanks to Jay A. Kreibich for the chop() idea.
=head1 SEE ALSO
L<Function::Override>
=cut
1;
( run in 0.963 second using v1.01-cache-2.11-cpan-39bf76dae61 )