Yes 5.10.1 is ancient. Still, I have in the past attempted to make sure code runs on it, if only because it's the standard Perl shipped with CentOS 6, which is one of the platforms I support.

Perhaps no more.

I have always believed that require acts only during the execution phase of a program, not during compilation.

Not under 5.10.1...

Here's the test code. Note that the require FileHandle line is in a block which is never run and commented out

use File::Temp qw( tempfile ); if ( 0 ) { print STDERR "required\n"; # require FileHandle; } my ($fh, $file) = tempfile( UNLINK => 1 ); $fh->getline;
Here's what I get if I run it:
$ perl ttrap.t
Now, uncomment that require FileHandle line, so that it's visible to the compiler. It's still in a block which will never be run.
$ perl ttrap.t Can't locate object method "getline" via package "FileHandle" at ttrap +.t line 12.
Huh? The require FileHandle is never executed, so why should it make a difference if the compiler sees the code? (And it's not actually loading FileHandle.pm; I've checked for that).

Now, change that if ( 0 ) to if ( 1 ) so that the block is executed, and rerun:

$ perl ttrap.t required
Here it actually is loading FileHandle.pm (I checked). So, it looks like the compiler does something special if it detects there's a possibility that FileHandle is being required, but that messes things up if it isn't actually required. Replacing
require FileHandle;
with
eval 'require FileHandle';
doesn't trigger the error, which bolsters the argument.

This makes me wonder if there are any other "optimizations" like this lurking in the mists.

(I've tried this code with earlier and later versions of Perl, and this discordant behavior is absent. I checked perldelta's and didn't see any relevant comment. Perhaps I missed it; that happens).

Update:

It seems the compiler is doing a pattern match for require Filehandle, as require FileHandle::WhoCaresAnyway; has the same effect.

Updated Update:

Ignore the conspiracy theories above. See tobyink's comment below.

tldr; If you're running on 5.10.1 and you conditionally require a module which is in the FileHandle hierarchy (e.g. FileHandle, FileHandle::Fmode), or you use something in that heirarchy below FileHandle (such as FileHandle::Fmode) you may run into problems using methods on file handles. To that end, I've put the following in code with those characteristics:

use if $^V ge v5.10.0 && $^V lt v5.11.0, 'FileHandle';

In reply to Compile time action of require in 5.10.1?? by djerius

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.