in reply to 'if' pragma and modules with an empty import list

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.

Replies are listed 'Best First'.
Re^2: 'if' pragma and modules with an empty import list
by kcott (Archbishop) on Apr 12, 2017 at 00:59 UTC

    G'day haukex,

    Take a look at ++beech's detailed response.

    The patch has been sent. perlbug is telling me: "... automated response acknowledging your message within a few hours ...". I'll post the ticket number when I have it.

    — Ken