For opendir the two argument form is still used.
Below is an example of opening a sub directory.
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use English;
my $dir = 'stats/';
my $subdir = '2007/';
opendir my $DIR, $dir . $subdir
or croak "Can't open directory: $OS_ERROR";
# do something
close $DIR;
exit;
The next version is also working:
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use English;
my $dir = 'stats';
my $subdir = '2007';
opendir my $DIR, $dir . '/' . $subdir
or croak "Can't open directory: $OS_ERROR";
# do something
close $DIR;
exit;
In the third example the variables $dir and $subdir are empty.
One would assume that this triggers the error message.
But the script is running without any error message. It simply
opens the "/" directory (the root directory) which is available
on every server.
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use English;
my $dir = '';
my $subdir = '';
opendir my $DIR, $dir . '/' . $subdir
or croak "Can't open directory: $OS_ERROR";
# do something
close $DIR;
exit;
Ok, why should someone define empty variables? Normally that's not the case.
But if the variables are assigned dynamically in a complex script this can happen.
So it's better to use the first version with the trailing slash.