That's because the code you were given is wrong. opendir takes two arguments, not one, and returns true if successful. It may help you to look at the opendir documentation. The code should read something like
opendir my $dirHandle, $dirpath
or die qq{opendir: $dirpath: $!\n};