Perl has the feature that strings named like a file handle are taken to be a filehandle:
# let this be some nice class I wrote
package Input {
sub awesome { ... }
}
So when we do Input->awesome
or extra-careful: 'Input'->awesome
, the method will get called. Unless:
# now somewhere far, far away, in package main, somebody does this:
open Input, "<&", \*STDIN or die $!; # normally we'd open to a file
This code doesn't even have to be executed, but only be seen by the parser in order to have Perl interpret the string 'Input'
as a file handle from now on. Therefore, a method call 'Input'->awesome
will die because the object representing the file handle doesn't have awesome methods.
As I am only in control of my class but not of other code, I can't simply decide to only use lexical filehandles everywhere.
Is there any way I can force Input->awesome
to always be a method call on the Input
package, but never a file handle (at least in scopes controlled by me)? I'd think there shouldn't be any name clash because the Input
package is actually the %Input::
stash.
Full code to reproduce the problem (see also this ideone):
use strict;
use warnings;
use feature 'say';
say "This is perl $^V";
package Input {
sub awesome {
say "yay, this works";
}
}
# this works
'Input'->awesome;
# the "open" is parsed, but not actually executed
eval <<'END';
sub red_herring {
open Input, "<&", \*STDIN or die $!;
}
END
say "eval failed: $@" if $@;
# this will die
eval {
'Input'->awesome;
};
say "Caught: $@" if $@;
Example output:
This is perl v5.16.2
yay, this works
Caught: Can't locate object method "awesome" via package "IO::File" at prog.pl line 27.
awesome Input
behaves the same asInput->awesome
. Interestingly,awesome 'Input'
doesn't compile as it's taken to be a function call. – Sobersidedawesome Class::
is equivalent toClass::->awesome
. – Sherrodawesome Input::
doesn't seem to force Perl to interpret the bareword as a class name either, despite what the documentation indicates:You can force Perl to interpret the bareword as a class name by appending "::" to it
– RufeInput::awesome
works – RufeInput
inheritsawesome
from its parent class). – Sherrodimport
which will be called implicitly when my package isuse
d. This fails very subtly becauseIO::File->import
gets called instead. – Sobersided