I think the thing is that use Module (); is a unique case, as the docs highlight: "Again, there is a distinction between omitting LIST (import called with no arguments) and an explicit empty LIST () (import not called)." I think that the LIST in use Module VERSION LIST; is passed to the Module->import(LIST) method using Perl's normal argument list handling, meaning that empty lists will be flattened. This includes everything after the "if" in use if CONDITION, MODULE => ARGUMENTS;.

$ cat Foo.pm package Foo; use warnings; use strict; use Data::Dump 'pp'; our $VERSION = 1.23; sub import { print "import ".pp(\@_)."\n" } 1; $ perl -wMstrict -e 'use Foo ()' $ perl -wMstrict -e 'use Foo' import ["Foo"] $ perl -wMstrict -e 'use Foo "bar"' import ["Foo", "bar"] $ perl -wMstrict -e 'use Foo 1.20 "bar"' import ["Foo", "bar"] $ perl -wMstrict -e 'use Foo "bar", "quz"' import ["Foo", "bar", "quz"] $ perl -wMstrict -e 'use Foo "bar", ()' import ["Foo", "bar"] $ perl -wMstrict -e 'use Foo "bar", (), "quz"' import ["Foo", "bar", "quz"] $ perl -wMstrict -e 'use Foo "CONDITION", "MODULE" => "ARGUMENTS"' import ["Foo", "CONDITION", "MODULE", "ARGUMENTS"] $ perl -wMstrict -e 'use Foo "CONDITION", "MODULE"' import ["Foo", "CONDITION", "MODULE"] $ perl -wMstrict -e 'use Foo "CONDITION", "MODULE" => ()' import ["Foo", "CONDITION", "MODULE"]

So if's behavior isn't really surprising, but I agree that its docs could mention this. As ikegami said, it's the perfect chance to get a patch in :-)

Personally I wouldn't mind the workaround BEGIN { require Module if CONDITION }, but you're right about the doc issue.


In reply to Re: 'if' pragma and modules with an empty import list by haukex
in thread 'if' pragma and modules with an empty import list by kcott

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.