in reply to Prevent Strings From Being Interpreted As A File Handle

However, my question is: How can I disambiguate a method call on a string so that it is always resolved as a class method call, and not as a call on an IO instance?

You can't :) apparently filehandles rule the typeglob, thats why it hasn't been recommended for a long time

$ perl -fle " sub Snacks::Ahoy { q{Nom} } open Snacks, q{>}, \q{}; pri +nt $Snacks::{Ahoy}->() " Nom $ perl -fle " sub Snacks::Ahoy { q{Nom} } open Snacks, q{>}, \q{}; pri +nt bless(\(my$q=\{Snacks}),q{Snacks})->Ahoy " Nom $ perl -fle " sub Snacks::Ahoy { q{Nom} } sub Snacks { q{Rice} } print + Snacks::->Ahoy " Nom $ perl -fle " sub Snacks::Ahoy { q{Nom} } open Snacks , q{<}, \q{Rice} +; print Snacks::->Ahoy " Can't locate object method "Ahoy" via package "IO::File" at -e line 1.

Replies are listed 'Best First'.
Re^2: Prevent Strings From Being Interpreted As A File Handle
by Anonymous Monk on Apr 27, 2014 at 11:13 UTC
    disambiguation with local
    $ perl -fle " sub Snacks::Ahoy { q{Nom} } open Snacks , q{<}, \q{Rice} +; local *Snacks; print Snacks::->Ahoy " Nom

      This is clever. I like it.

      So we'd end up with something inconvenient like

      sub invoke_static { my ($class, $method, @args) = @_; no strict 'refs'; # I'd like to only localize *{$class}{IO}, # but we can't have everything, right? local *{$class}; $class->$method(@args); }

      or something insane like

      sub safe_from_evil_filehandles(&) { my ($cb) = @_; my @evil_globs; ... # walk through %:: and list all globs # where *{$entry}{IO} is defined # unless they're OK like "STDOUT" my $localizer; $localizer = sub { return $cb->() unless @evil_globs; local *{shift @evil_globs}; return $localizer->(); }; return $localizer->(); }

      This works. Good job.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name