The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

clobber - pragma to optionally prevent over-writing files

SYNOPSIS

  no clobber;

  #Fails if /tmp/xyzzy exists
  open(HNDL, '>/tmp/xyzzy');

  {
    use clobber;

    #It's clobberin' time
    open(HNDL, '>/tmp/xyzzy');
  }

DESCRIPTION

Do you occasionally get +> and +< mixed up, or accidentally leave off an > in the mode of an open? Want to run some relatively trustworthy code--such as some spaghetti monster you created in the days of yore--but can't be bothered to check it's semantics? Or perhaps you'd like to add a level of protection to operations on user-supplied files without coding the logic yourself.

Yes? Then this pragma could help you from blowing away valuable data.

Like the noclobber variable of some shells, this module will prevent the use of open modes which truncate if a file already exists. This behavior can be controlled at the block level, as demonstrated in the "SYNOPSIS".

DIAGNOSTICS

The pragma may throw the following exceptions:

%s: File exists

We saved data!

Failed to parse EXPR of 2-arg open: %s

The module could not figure out what mode was used, and decided to bail for safety.

This shouldn't happen.

ENVIRONMENT

You may disable clobber protection at compile-time by setting the environment variable clobber.pm to 1. This allows you to include clobber.pm in PERL5OPT as -M-clobber for general protection, but override it as needed for programs invoked via a pipeline.

TODO

TESTS!

I've done some basic-testing with 2- and 3-arg forms of read/write/append, but more thorough testing of mode-parsing and/or invocation needs to be done.

Interactive ask to run the more complex tests, with timeout to skip them.

as optional "imports"

AUTHOR

Jerrad Pierce <JPIERCE circle-a CPAN full-stop ORG>

LICENSE

  • Thou shalt not claim ownership of unmodified materials.

  • Thou shalt not claim whole ownership of modified materials.

  • Thou shalt grant the indemnity of the provider of materials.

  • Thou shalt use and dispense freely without other restrictions.

Or, if you prefer:

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.0 or, at your option, any later version of Perl 5 you may have available.