The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

OpenInteract2::Config::Readonly - Simple read/write for readonly files

SYNOPSIS

 use OpenInteract2::Config::Readonly;
 
 # See if some files are writeable in $dir
 
 my @files_to_write = ( 'blah.html', 'bleh.txt' );
 my $files_writeable = OpenInteract2::Config::Readonly
                         ->get_writeable_files( $dir, \@files_to_write );
 
 # Same thing, but read the nonwriteable files first
 
 my $readonly_files = OpenInteract2::Config::Readonly->read_config( $dir );
 my @files_to_write = ( 'blah.html', 'bleh.txt' );
 my $files_writeable = OpenInteract2::Config::Readonly
                         ->get_writeable_files( $readonly_files, \@files_to_write );
 
 # See if a single file is writeable
 
 my $original_path = '/path/to/distribution/foo.html';
 my $can_write = OpenInteract2::Config::Readonly
                         ->is_file_writeable( $dir, $original_path );
 if ( $can_write ) {
     cp( $original_path,
         File::Spec->catfile( $dir, basename( $original_path ) ) );
 }
 
 # Write a set of readonly files with a comment...
 
 OpenInteract2::Config::Readonly->write_config(
                         $dir,
                         { file    => [ 'file1', 'file2' ],
                           comment => 'OI will not overwrite these files' } );
 
 # ... or without
 OpenInteract2::Config::Readonly->write_config(
                         $dir,
                         [ 'file1', 'file2' ] );

DESCRIPTION

Simple module to read/write configuration that determines which files in a directory OpenInteract2 should not overwrite.

METHODS

Note: We only read, store and check against bare filenames from the readonly config -- that is, the result of a File::Basename basename call.

is_writeable_file( \@readonly_filenames | $directory, $filename )

Returns true if file $filename is writeable in $directory or if it is not found among \@readonly_filenames. We do a basename() against $filename before doing the check.

Examples:

 # These all return true
 OpenInteract2::Config::Readonly->is_writeable_file(
                    [ 'index.html' ], 'foo.html' );
 OpenInteract2::Config::Readonly->is_writeable_file(
                    [ 'index.html' ], 'INDEX.HTML' );
 OpenInteract2::Config::Readonly->is_writeable_file(
                    [ 'index.html' ], '/path/to/index.htm' );

 # These all return false
 OpenInteract2::Config::Readonly->is_writeable_file(
                    [ 'index.html' ], 'index.html' );
 OpenInteract2::Config::Readonly->is_writeable_file(
                    [ 'index.html' ], '/path/to/my/index.html' );

get_writeable_files( \@readonly_filenames | $directory, \@filenames )

Returns an arrayref of all writeable files from \@filenames as compared against the config in $directory or the readonly filenames in \@readonly_filenames. The filenames returned are whatever was stored in \@filenames rather than the basename.

Examples:

 my $files = OpenInteract2::Config::Readonly->get_writeable_files(
                    [ 'index.html' ], [ '/path/to/foo.html' ] );
 # $files = [ '/path/to/foo.html' ]
 
 my $files = OpenInteract2::Config::Readonly->get_writeable_files(
                    [ 'index.html' ], [ 'INDEX.HTML', '/path/to/README.txt' ] );
 # $files = [ 'INDEX.HTML', '/path/to/README.txt' ]
 
 my $files = OpenInteract2::Config::Readonly->get_writeable_files(
                    [ 'index.html' ], [ '/path/to/index.htm', '/path/to/index.html' ] );
 # $files = [ '/path/to/index.htm' ]

read_config( $dir )

Reads the file in $dir for files not to overwrite. This method should never die or throw an exception -- if there is an error reading the file or if the file does not exist, it simply returns an empty arrayref.

Returns: arrayref of filenames relative to $dir.

write_config( $dir, \@files_to_write | \%write_info )

Writes filenames to a file in $dir. The \%write_info parameters can be either an arrayref of filenames to write or a hashref with the following keys:

  • file: Arrayref of filenames to write

  • comment: Message to write as a comment.

No path information is written to the file, only the base filename.

Returns: full path to file written. If the file cannot be written, it will throw an exception. If there are no files passed in to write, it returns nothing.

BUGS

None known.

SEE ALSO

File::Basename

COPYRIGHT

Copyright (c) 2002-2003 Chris Winters. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHORS

Chris Winters <chris@cwinters.com>