use strict; # clean a directory path of extraneous '/' sub clean_path { for (my $i = 0; $i <= $#_; $i++) { # match two or more '/' between a pair of # other non '/' chars and replace the # multiple occurance of '/' with a single '/'. $_[$i] =~ s|([^/])[/]{2,}([^/])|$1/$2|g; # remove trailing garbage such as '/'... $_[$i] =~ s|[/\n\t\s]+$||; # also remove silly '/./...' things $_[$i] =~ s|\./||g; $_[$i] =~ s|/\.$||g; # some path have that wicked '/foo/bar/.' '.' in the end! } } ### MAIN my $path_list = undef; ## a bunch of code here ## . . . . . ## ## I'm checking that if no path list was provided, ## I use at a 'blank' path so that the loop would ## execute at least once (as if for root directory). ## foreach my $some_path ( (defined $path_list && $#$path_list >= 0) ? @{$path_list} : "" ) { clean_path($some_path); # do stuff with path print "Working with $some_path\n"; } print "done\n"; #### Modification of a read-only value attempted at test.pl line 10, chunk 1. main::clean_path('') called at test.pl line 33