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

File::Filename - expect a filename to be named by a person to be metadata

DESCRIPTION

A lot of people use the filename as a place to insert metadata for the file. This module has some routines to help with treating those filenames. This code takes into consideration what human beings would name files as.

People often expect a space to be *part of* something. To us, used to the prompt, a space is a delimiter, not a delimeter.

People see an underscore and it is a delimiter to them, but to us it is a "word character". This is one of a colleciton of modules to help consolidate the real world of file archiving in offices (multiple human users naming files by hand) with the needs of people maintaining such filesystem hierarchy structures.

SYNOPSIS

        use File::Filename 'get_filename_segments';
        use Smart::Comments '###';
        
        opendir(DIR,$ENV{HOME});
        
        map { 
                my $segments = get_filename_segments($_); 
                ### $segments
        } grep { !/^\.+$/ } readdir DIR;
        
        closedir DIR;
        

get_filename_segments()

argument is a filename, can be absolute path (the location is ignored) optional argument is a quoted regex that matches non field characters (delimiters). returns array ref

Default regex is qr/[^a-zA-Z0-9 ]+/

In the below examples, You see the filename, and the resulting elements

        122706-BRANDYWINE WISCONSIN LLC-005779-@API.pdf  
   ['122706','BRANDYWINE WISCONSIN LLC','005779','API','pdf']
   
        122706-GUARDIAN REALTY MANAGEMENT INC-005776-@API.pdf 
   [122706','GUARDIAN REALTY MANAGEMENT INC','005776','API','pdf]
   
        122705-V & F COFFEE INC-004702-@API.pdf 
   [122705','V','F COFFEE INC','004702','API','pdf]

What if you wanted the ampersand to be part of word characters?

   $File::Filename::delimiter = qr/[^\&a-zA-Z0-9 ]/;

   # or
   
   get_filename_segments($filename, qr/[^\&a-zA-Z0-9 ]/); 

Empty segments are not returned.

SEE ALSO

File::Filename::Convention

Revision

$Revision: 1.2 $

AUTHOR

Leo Charre leocharre at cpan dot org