git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] add -p: import Term::ReadKey with 'require'

From
Jeff King <peff@peff.net>
Date
Feb 6, 2009, 20:30 UTC
Message-ID
<20090206203037.GB19959@coredump.intra.peff.net>
In-Reply-To
<1233948601-4828-1-git-send-email-trast@student.ethz.ch>
On Fri, Feb 06, 2009 at 08:30:01PM +0100, Thomas Rast wrote:
Show 6 quoted lines
> The code below _seems_ to work.  I have to say that beyond the
> 'require', it's all voodoo to me, so I'd appreciate an extra-careful
> check.
> [...]
> +sub ReadMode;
> +sub ReadKey;

I believe it is fine. The tricky thing is that perl's parsing is dependent on what functions have been defined. So it is OK to say

  ReadKey 0;

if a subroutine ReadKey has been defined, but otherwise it generates a warning about using the bareword as a function. However

  ReadKey(0);

parses unambiguously, so it is always OK, even if no subroutine has yet been defined.

So with the "use" code, Term::ReadKey had already been loaded when parsing the bit about ReadKey. But now it is loaded at run-time, so at parse time we don't know about that subroutine yet. So your options are to forward-declare the subroutines (which you did), or to change calls to the obvious function-like form.

> -		use Term::ReadKey;
> +		require Term::ReadKey;
> +		Term::ReadKey->import;

And the two added lines are the exact runtime equivalent of the deleted line (note that you could also skip the import and just call Term::ReadKey::ReadKey by its full name).

-Peff
Previous: Thomas RastNext: Thomas Rast
Message 5 of 8 in “add -p: prompt for single characters”
  1. 3/4 add -p: prompt for single charactersThomas Rast, Feb 5, 2009
  2. 4/4 add -p: print errors in separate colorThomas Rast, Feb 5, 2009
  3. Jeff KingFeb 6, 2009
  4. add -p: import Term::ReadKey with 'require'Thomas Rast, Feb 6, 2009
  5. Jeff KingFeb 6, 2009
  6. Thomas RastFeb 6, 2009
  7. Jeff KingFeb 7, 2009
  8. Junio C HamanoFeb 7, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.