my $dir = '/tmp' my $sub_dir; my $file = 'foo.txt' my $path = join('/', grep { defined && $_ ne '' } ($dir, $sub_dir, $file)); print "$path\n";