[Chicago-talk] Probably and easy explanation I'm missing...

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Thu Aug 24 13:48:41 PDT 2006


my $filename   = $GLOBAL{'FILE_NAME'}; 
        $filename =~ s/.+\\([^\\]+)$|.+\/([^\/]+)$/$1/;

> How does the $1 work in this? Is it necessary? 

$1 in this context is the first captured chunk of the match on the LHS of 
the subst command:
        $filename =~ s/.+\\([^\\]+)$|.+\/([^\/]+)$/$1/;

Sooo .... "one or more non-\n chars followed by a literal "\" and then a 
captured one or more non"\"  chars to the end of th line *OR* one or more 
non-\n chars followed by a literal '/' and then a captured one or more 
non-'\' chars up to the end of the line." Maybe.  Replace it all w/ the 
captured text - the idea being:
$filenmae eq '/hi/ho/mom'
becomes:
$filename eq 'mom';

regardless of the slashes.    What error this generates would be helpful 
but ... one problem is, you don't know if the subst worked. It should be 
(at least)
my $filename   = $GLOBAL{'FILE_NAME'}; 
if ($filename =~ s/.+\\([^\\]+)$|.+\/([^\/]+)$/$1/ ) {
     # now we have a bare filename
}
else { 
      # now we have a filename that didn't match
}    # if filename =~


Conventional wisdome is to use the core module File::Basename - which 
handles all the slashing problems (perldoc File::Basename)
use File::Basename;

           ($name,$path,$suffix) = fileparse($fullname, at suffixlist)
           fileparse_set_fstype($os_string);
           $basename = basename($fullname, at suffixlist);
           $dirname = dirname($fullname);

           ($name,$path,$suffix) = 
fileparse("lib/File/Basename.pm",qr{\.pm});
           fileparse_set_fstype("VMS");
           $basename = basename("lib/File/Basename.pm",qr{\.pm});
           $dirname = dirname("lib/File/Basename.pm");

a 

Andy Bach, Sys. Mangler
Internet: andy_bach at wiwb.uscourts.gov 
VOICE: (608) 261-5738  FAX 264-5932

"They think they invented NIH."  Colin McCormack on Debian insiders


More information about the Chicago-talk mailing list