Win32::Filenames - The great new Win32::Filenames!


Win32-Filenames documentation Contained in the Win32-Filenames distribution.

Index


Code Index:

NAME

Top

Win32::Filenames - The great new Win32::Filenames!

VERSION

Top

Version 0.01

SYNOPSIS

Top

This module can be used to validate if a given filename is a valid Windows filename and if it is not valid can fix the filename by replacing the invalid characters.

Perhaps a little code snippet.

    use Win32::Filenames qw(validate sanitize);

    my @filenames = ( 'file1.txt', '(file;;2.doc)', 'file::.doc?', 'file6',
                      'file7>>.txt' );
    my $test;
    foreach $test (@tests) {
      print "checking filename: [$test] ...";

      if ( validate($test) ) {
      	print "Filename is ok.\n";
      }else{
        print "Filename is bad. **\n";
	print "\tTRY: ";
	print sanitize($test), "\n";
      }
    }

EXPORT

Top

Nothing is exported by default.

The following are allowed to be exported:

* sanitize() --> fix filename.
* validate() --> check filename.
* $ERR_CHAR --> what character was invalid in last filename check.

FUNCTIONS

Top

validate($filename);

This function is passed a filename which is searched for any invalid characters. If there are no invalid characters then the function returns true. If the filename does contain invalid characters, the invalid character encountered is stored in the GLOBAL variable $ERR_CHAR and the function returns undef.

sanitize($filename, [ $replace_str ]);

This function is passed a filename to convert to a valid Windows filename. The function is also optionaly passed a string or character to be used to replace invalid characters with. If no replacement character is passed to the function, it defaults to use '-' to replace invalid characters.

If the replacement character passed to the function is itself an invalid character, then the function croaks. Otherwise, the filename is searched for any invalid characters, and string is returned with the invalid characters replaced.

AUTHOR

Top

Brent Hostetler, <brent@the-hostetlers.net>

BUGS

Top

Please report any bugs or feature requests to bug-win32-filenames@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Win32-Filenames. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

Top

I would like to acknowledge myself! :P

COPYRIGHT & LICENSE

Top


Win32-Filenames documentation Contained in the Win32-Filenames distribution.

package Win32::Filenames;

use warnings;
use strict;

use Exporter 'import';
our @EXPORT_OK = qw(&sanitize &validate $ERR_CHAR);

use Carp;

our $VERSION = '0.01';

our $ERR_CHAR = '';

my @_INVALIDS = ( '\\','/', '|', ':', '<', '>', '"', '?', '*' );

# remove any of the following invalid characters:
#   \ / | : ? * " < >
sub sanitize {
	my $str  = shift;
	my $char = $_[0] || '-';
	
	croak "Invalid character cannot be used in sanitize function. [$char]\n" if
		( grep { $char =~ /\Q$_\E/g } @_INVALIDS);
	$str =~ s/\\|\/|:|\*|\?|"|<|>|\|/$char/g;
	return $str;
}

sub validate {
	local $_;
	$_ = shift;
	
	if (/\\|\/|:|\*|\?|"|<|>|\|/) {
		$ERR_CHAR = $&;
		return undef;
	}else{
		return 1;
	}
}

1; # End of Win32::Filenames