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

disambiguation with local
$ perl -fle " sub Snacks::Ahoy { q{Nom} } open Snacks , q{<}, \q{Rice} +; local *Snacks; print Snacks::->Ahoy " Nom

Replies are listed 'Best First'.
Re^3: Prevent Strings From Being Interpreted As A File Handle
by amon (Scribe) on Apr 27, 2014 at 14:33 UTC

    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->(); }
Re^3: Prevent Strings From Being Interpreted As A File Handle
by tobyink (Canon) on Apr 27, 2014 at 14:05 UTC

    This works. Good job.

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