The problem is that the first argument of open should either be a filehandle, or a reference to a filehandle. Since 5.6, open will autovivify an undefined value into a filehandle (most people say "since 5.6, we have lexical filehandles". That's not true. Neither is the first argument of open a filehandle in this case, nor is 5.6 a requirement to have a lexical reference to a filehandle. Both have been possible long before 5.6 - it's only the autovivification that's new). But you're passing in a variable with a defined value; that's fine, Perl will try to use it as a reference to a filehandle. And the
*1 typeglob does have a slot for filehandles. It will all work fine and dandy, except that by enabling
strict 'refs', you're explicitely forbidden Perl to do its utter best to satisfy your request.
If you'd just replace $filehandle=$val; with my $filehandle;, it should all work.