Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Perl has the feature that strings named like a file handle are taken to be a file handle, which for example means that “open DUP, '>&STDOUT'”, but also “'STDOUT'->say("hello")” works.

This leads to conflicts if we have a class and a bareword file handle of the same name:

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.

I am aware that this can be avoided with lexical filehandles, and that adhering to proper naming conventions makes this collision less likely. For example, the above code does not fail if the script is in “package Foo” and the eval'd code in “package main”, but the problem occurs again when we do “'main::Input'->awesome” which should in theory be absolutely equivalent to “'Input'->awesome”.

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?

I assume one would have to write a pragma to hook into method resolution, e.g. by replacing the pp_method_named opcode, but I'd much rather hear the wisdom of more experienced monks on this.

Context: I'm hacking on the utility module Util::Underscore that misguidedly stuffs various utils into the “_” namespace so that they can be used comfortably without having to import them. As I want to prevent the “_” package from being used directly, I boobie-trapped the “_::import” method to blow with a helpful error message whenever touched. My tests started failing when another module contained file tests on the magic “_” file handle, like “-d $file && -w _”.

This question has previously been asked on Stack Overflow, but so far without the answer I'm seeking.


In reply to Prevent Strings From Being Interpreted As A File Handle by amon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-25 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found