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