$opts->{dir} is initialized with undef. so exists $opts->{dir} is always true.