use vs require with version

--Sig_/3nZTXBuKNI.Ajjwv4LiHSOo
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Why is =C2=ABuse Module Version;=C2=BB supported, but =C2=ABrequire Module =
Version;=C2=BB
not?

$ perl -we'use DBI'
$ perl -we'use DBI 0.3'
$ perl -we'use DBI 3.0'
DBI version 3 required--this is only version 1.641 at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
$ perl -we'use DBI 0.3 ()'
$ perl -we'use DBI 3.0 ()'
DBI version 3 required--this is only version 1.641 at -e line 1.
BEGIN failed--compilation aborted at -e line 1.

$ perl -we'require DBI'
$ perl -we'require DBI 0.3'
Number found where operator expected at -e line 1, near "require DBI 0.3"
        (Do you need to predeclare require?)
syntax error at -e line 1, near "require DBI 0.3"


--=20
H.Merijn Brand  http://tux.nl   Perl Monger  http://amsterdam.pm.org/
using perl5.00307 .. 5.27   porting perl5 on HP-UX, AIX, and openSUSE
http://mirrors.develooper.com/hpux/        http://www.test-smoke.org/
http://qa.perl.org   http://www.goldmark.org/jeff/stupid-disclaimers/

--Sig_/3nZTXBuKNI.Ajjwv4LiHSOo
Content-Type: application/pgp-signature
Content-Description: OpenPGP digital signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJau6kXAAoJEAOhR6E+XcCYgdoIAKexn1nCl/WBOfxWjEdd1Min
6JQ0+u3ngeNA+rtFzZWMamGSlYoIS1/RJRqsTZCjFTlhuuzUlIKrLa/6HD9Exf5l
fK0IBoo1uje+Y8aVl7wYhQDgmOTBPRihCGi6chbxKl9WFI4Ww43eUl/lC+QrMoeS
gpq5ghq0lnHpSm8HpAcEfSgT5hHZNANcJUKnRPGnd7Blj0mh9yvGGK8K4oHmH5ae
Zq2MILYL1UREZS1c+At8Q29mcGya5B70vsSY0bBuQrto2k/GdTqVp4qiFWVPp6ql
84WF1bvAkNiQUQEatff9kyY03haLS1mcxyFfAwFPGbSqf5ENJ+dUryTiOyaRWAg=
=Yk9t
-----END PGP SIGNATURE-----

--Sig_/3nZTXBuKNI.Ajjwv4LiHSOo--
0
h
3/28/2018 2:39:12 PM
perl.perl5.porters 47123 articles. 0 followers. Follow

7 Replies
14 Views

Similar Articles

[PageSpeed] 55

H.Merijn Brand wrote:
>Why is "use Module Version;" supported, but "require Module Version;"
>not?

Because "require" and "use" do different jobs, not just the same job
at different times.  Note that "require" doesn't imply any call to the
subject's ->import method.  It would be equally out of place for it to
imply a call to ->VERSION.

-zefram
0
zefram
4/8/2018 9:44:34 AM
On Sun, Apr 8, 2018 at 11:44 AM, Zefram <zefram@fysh.org> wrote:
> H.Merijn Brand wrote:
>>Why is "use Module Version;" supported, but "require Module Version;"
>>not?
>
> Because "require" and "use" do different jobs, not just the same job
> at different times.  Note that "require" doesn't imply any call to the
> subject's ->import method.  It would be equally out of place for it to
> imply a call to ->VERSION.

Indeed, however I wish perl had such a feature. I.e. just a
"use_runtime" or whatever, which would work exactly like what you can
manually do with some combination of require(), calling ->import(),
and ->VERSION(), but less verbose. Also having a require() that
accepted Like::This instead of Like/This.pm would be very useful, CPAN
modules exist just to paper over that trivial missing implementation
detail when you need to "require $variable".
0
avarab
4/8/2018 6:28:55 PM
On 04/08/2018 12:28 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason wrote:
> On Sun, Apr 8, 2018 at 11:44 AM, Zefram <zefram@fysh.org> wrote:
>> H.Merijn Brand wrote:
>>> Why is "use Module Version;" supported, but "require Module Version;"
>>> not?
>>
>> Because "require" and "use" do different jobs, not just the same job
>> at different times.  Note that "require" doesn't imply any call to the
>> subject's ->import method.  It would be equally out of place for it to
>> imply a call to ->VERSION.
>=20
> Indeed, however I wish perl had such a feature. I.e. just a
> "use_runtime" or whatever, which would work exactly like what you can
> manually do with some combination of require(), calling ->import(),
> and ->VERSION(), but less verbose. Also having a require() that
> accepted Like::This instead of Like/This.pm would be very useful, CPAN
> modules exist just to paper over that trivial missing implementation
> detail when you need to "require $variable".
>=20

+1
0
public
4/8/2018 6:31:20 PM
--000000000000507c5e05695a9111
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Sun, Apr 8, 2018 at 2:28 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason <ava=
rab@gmail.com>
wrote:

> On Sun, Apr 8, 2018 at 11:44 AM, Zefram <zefram@fysh.org> wrote:
> > H.Merijn Brand wrote:
> >>Why is "use Module Version;" supported, but "require Module Version;"
> >>not?
> >
> > Because "require" and "use" do different jobs, not just the same job
> > at different times.  Note that "require" doesn't imply any call to the
> > subject's ->import method.  It would be equally out of place for it to
> > imply a call to ->VERSION.
>
> Indeed, however I wish perl had such a feature. I.e. just a
> "use_runtime" or whatever, which would work exactly like what you can
> manually do with some combination of require(), calling ->import(),
> and ->VERSION(), but less verbose. Also having a require() that
> accepted Like::This instead of Like/This.pm would be very useful, CPAN
> modules exist just to paper over that trivial missing implementation
> detail when you need to "require $variable".
>

Mostly agreed, however runtime import will usually not be useful since
imports tend to be used to set up symbols for the parser. Like you
mentioned, Module::Runtime provides the runtime variable module name
require and version check in use_module().

-Dan

--000000000000507c5e05695a9111
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On S=
un, Apr 8, 2018 at 2:28 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason <span di=
r=3D"ltr">&lt;<a href=3D"mailto:avarab@gmail.com" target=3D"_blank">avarab@=
gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span cl=
ass=3D"">On Sun, Apr 8, 2018 at 11:44 AM, Zefram &lt;<a href=3D"mailto:zefr=
am@fysh.org">zefram@fysh.org</a>&gt; wrote:<br>
&gt; H.Merijn Brand wrote:<br>
&gt;&gt;Why is &quot;use Module Version;&quot; supported, but &quot;require=
 Module Version;&quot;<br>
&gt;&gt;not?<br>
&gt;<br>
&gt; Because &quot;require&quot; and &quot;use&quot; do different jobs, not=
 just the same job<br>
&gt; at different times.=C2=A0 Note that &quot;require&quot; doesn&#39;t im=
ply any call to the<br>
&gt; subject&#39;s -&gt;import method.=C2=A0 It would be equally out of pla=
ce for it to<br>
&gt; imply a call to -&gt;VERSION.<br>
<br>
</span>Indeed, however I wish perl had such a feature. I.e. just a<br>
&quot;use_runtime&quot; or whatever, which would work exactly like what you=
 can<br>
manually do with some combination of require(), calling -&gt;import(),<br>
and -&gt;VERSION(), but less verbose. Also having a require() that<br>
accepted Like::This instead of Like/This.pm would be very useful, CPAN<br>
modules exist just to paper over that trivial missing implementation<br>
detail when you need to &quot;require $variable&quot;.<br>
</blockquote></div><br></div><div class=3D"gmail_extra">Mostly agreed, howe=
ver runtime import will usually not be useful since imports tend to be used=
 to set up symbols for the parser. Like you mentioned, Module::Runtime prov=
ides the runtime variable module name require and version check in use_modu=
le().</div><div class=3D"gmail_extra"><br></div><div class=3D"gmail_extra">=
-Dan</div></div>

--000000000000507c5e05695a9111--
0
grinnz
4/8/2018 6:36:29 PM
On Sun, 8 Apr 2018 12:31:20 -0600
Karl Williamson <public@khwilliamson.com> wrote:

> On 04/08/2018 12:28 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason wrote:
> > On Sun, Apr 8, 2018 at 11:44 AM, Zefram <zefram@fysh.org> wrote: =20
> >> H.Merijn Brand wrote: =20
> >>> Why is "use Module Version;" supported, but "require Module Version;"
> >>> not? =20
> >>
> >> Because "require" and "use" do different jobs, not just the same job
> >> at different times.  Note that "require" doesn't imply any call to the
> >> subject's ->import method.  It would be equally out of place for it to
> >> imply a call to ->VERSION. =20
> >=20
> > Indeed, however I wish perl had such a feature. I.e. just a
> > "use_runtime" or whatever, which would work exactly like what you can
> > manually do with some combination of require(), calling ->import(),
> > and ->VERSION(), but less verbose. Also having a require() that
> > accepted Like::This instead of Like/This.pm would be very useful, CPAN
> > modules exist just to paper over that trivial missing implementation
> > detail when you need to "require $variable".
> >  =20
>=20
> +1

+1 from me as well.

--=20
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
http://www.shlomifish.org/humour/Summerschool-at-the-NSA/

If the mountain does not come to Muhammad, then Chuck Norris will bring the
mountain over.
    =E2=80=94 http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - http://shlom.in/reply .
0
shlomif
4/8/2018 6:37:49 PM
On Sun, Apr 08 2018, Dan Book wrote:

> On Sun, Apr 8, 2018 at 2:28 PM, Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> wrote:
>
>> On Sun, Apr 8, 2018 at 11:44 AM, Zefram <zefram@fysh.org> wrote:
>> > H.Merijn Brand wrote:
>> >>Why is "use Module Version;" supported, but "require Module Version;"
>> >>not?
>> >
>> > Because "require" and "use" do different jobs, not just the same job
>> > at different times.  Note that "require" doesn't imply any call to the
>> > subject's ->import method.  It would be equally out of place for it to
>> > imply a call to ->VERSION.
>>
>> Indeed, however I wish perl had such a feature. I.e. just a
>> "use_runtime" or whatever, which would work exactly like what you can
>> manually do with some combination of require(), calling ->import(),
>> and ->VERSION(), but less verbose. Also having a require() that
>> accepted Like::This instead of Like/This.pm would be very useful, CPAN
>> modules exist just to paper over that trivial missing implementation
>> detail when you need to "require $variable".
>>
>
> Mostly agreed, however runtime import will usually not be useful since
> imports tend to be used to set up symbols for the parser. Like you
> mentioned, Module::Runtime provides the runtime variable module name
> require and version check in use_module().

That only matters if you're using the import to trigger the parse-time
disambiguation logic between functions and other things, not if you just
want to get away from typing a fully-qualified function name.

I.e. this is "use":

    $ perl -MList::Util=sum -wE 'if (shift) { say sum @ARGV }' 1 2 3
    5

And this is the case you're talking about:

    $ perl -wE 'if (shift) { require List::Util; List::Util->import("sum"); say sum @ARGV }' 1 2 3
    Unquoted string "sum" may clash with future reserved word at -e line 1.
    Name "main::sum" used only once: possible typo at -e line 1.
    say() on unopened filehandle sum at -e line 1.

So yeah, you can't use "sum" without parens, however you can do:

    $ perl -wE 'if (shift) { require List::Util; List::Util->import("sum"); say sum(@ARGV) }' 1 2 3
    5

As opposed to:

    $ perl -wE 'if (shift) { require List::Util; say List::Util::sum(@ARGV) }' 1 2 3
    5

Which I daresay is the thing most users care about, so they could do
e.g. (new hypothehtical syntax):

    $ perl -wE 'if (shift) { require List::Util "sum"; say sum(@ARGV) }' 1 2 3
    5
0
avarab
4/8/2018 8:31:08 PM
--0000000000008e9ce5056960ae33
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Sun, Apr 8, 2018 at 4:31 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason <ava=
rab@gmail.com>
wrote:

>
> On Sun, Apr 08 2018, Dan Book wrote:
>
> > On Sun, Apr 8, 2018 at 2:28 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason =
<
> avarab@gmail.com>
> > wrote:
> >
> >> On Sun, Apr 8, 2018 at 11:44 AM, Zefram <zefram@fysh.org> wrote:
> >> > H.Merijn Brand wrote:
> >> >>Why is "use Module Version;" supported, but "require Module Version;=
"
> >> >>not?
> >> >
> >> > Because "require" and "use" do different jobs, not just the same job
> >> > at different times.  Note that "require" doesn't imply any call to t=
he
> >> > subject's ->import method.  It would be equally out of place for it =
to
> >> > imply a call to ->VERSION.
> >>
> >> Indeed, however I wish perl had such a feature. I.e. just a
> >> "use_runtime" or whatever, which would work exactly like what you can
> >> manually do with some combination of require(), calling ->import(),
> >> and ->VERSION(), but less verbose. Also having a require() that
> >> accepted Like::This instead of Like/This.pm would be very useful, CPAN
> >> modules exist just to paper over that trivial missing implementation
> >> detail when you need to "require $variable".
> >>
> >
> > Mostly agreed, however runtime import will usually not be useful since
> > imports tend to be used to set up symbols for the parser. Like you
> > mentioned, Module::Runtime provides the runtime variable module name
> > require and version check in use_module().
>
> That only matters if you're using the import to trigger the parse-time
> disambiguation logic between functions and other things, not if you just
> want to get away from typing a fully-qualified function name.
>
> I.e. this is "use":
>
>     $ perl -MList::Util=3Dsum -wE 'if (shift) { say sum @ARGV }' 1 2 3
>     5
>
> And this is the case you're talking about:
>
>     $ perl -wE 'if (shift) { require List::Util;
> List::Util->import("sum"); say sum @ARGV }' 1 2 3
>     Unquoted string "sum" may clash with future reserved word at -e line =
1.
>     Name "main::sum" used only once: possible typo at -e line 1.
>     say() on unopened filehandle sum at -e line 1.
>
> So yeah, you can't use "sum" without parens, however you can do:
>
>     $ perl -wE 'if (shift) { require List::Util;
> List::Util->import("sum"); say sum(@ARGV) }' 1 2 3
>     5
>
> As opposed to:
>
>     $ perl -wE 'if (shift) { require List::Util; say
> List::Util::sum(@ARGV) }' 1 2 3
>     5
>
> Which I daresay is the thing most users care about, so they could do
> e.g. (new hypothehtical syntax):
>
>     $ perl -wE 'if (shift) { require List::Util "sum"; say sum(@ARGV) }' =
1
> 2 3
>     5
>

It also matters to make the imported symbols resolvable at compile time
(which is especially important for any imported constants). This does not
work:

$ perl -E 'if (shift) { require List::Util; List::Util->import("sum"); say
+sum @ARGV }' 1 2 3

-Dan

--0000000000008e9ce5056960ae33
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On S=
un, Apr 8, 2018 at 4:31 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason <span di=
r=3D"ltr">&lt;<a href=3D"mailto:avarab@gmail.com" target=3D"_blank">avarab@=
gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span cl=
ass=3D""><br>
On Sun, Apr 08 2018, Dan Book wrote:<br>
<br>
&gt; On Sun, Apr 8, 2018 at 2:28 PM, =C3=86var Arnfj=C3=B6r=C3=B0 Bjarmason=
 &lt;<a href=3D"mailto:avarab@gmail.com">avarab@gmail.com</a>&gt;<br>
&gt; wrote:<br>
&gt;<br>
&gt;&gt; On Sun, Apr 8, 2018 at 11:44 AM, Zefram &lt;<a href=3D"mailto:zefr=
am@fysh.org">zefram@fysh.org</a>&gt; wrote:<br>
&gt;&gt; &gt; H.Merijn Brand wrote:<br>
&gt;&gt; &gt;&gt;Why is &quot;use Module Version;&quot; supported, but &quo=
t;require Module Version;&quot;<br>
&gt;&gt; &gt;&gt;not?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Because &quot;require&quot; and &quot;use&quot; do different =
jobs, not just the same job<br>
&gt;&gt; &gt; at different times.=C2=A0 Note that &quot;require&quot; doesn=
&#39;t imply any call to the<br>
&gt;&gt; &gt; subject&#39;s -&gt;import method.=C2=A0 It would be equally o=
ut of place for it to<br>
&gt;&gt; &gt; imply a call to -&gt;VERSION.<br>
&gt;&gt;<br>
&gt;&gt; Indeed, however I wish perl had such a feature. I.e. just a<br>
&gt;&gt; &quot;use_runtime&quot; or whatever, which would work exactly like=
 what you can<br>
&gt;&gt; manually do with some combination of require(), calling -&gt;impor=
t(),<br>
&gt;&gt; and -&gt;VERSION(), but less verbose. Also having a require() that=
<br>
&gt;&gt; accepted Like::This instead of Like/This.pm would be very useful, =
CPAN<br>
&gt;&gt; modules exist just to paper over that trivial missing implementati=
on<br>
&gt;&gt; detail when you need to &quot;require $variable&quot;.<br>
&gt;&gt;<br>
&gt;<br>
&gt; Mostly agreed, however runtime import will usually not be useful since=
<br>
&gt; imports tend to be used to set up symbols for the parser. Like you<br>
&gt; mentioned, Module::Runtime provides the runtime variable module name<b=
r>
&gt; require and version check in use_module().<br>
<br>
</span>That only matters if you&#39;re using the import to trigger the pars=
e-time<br>
disambiguation logic between functions and other things, not if you just<br=
>
want to get away from typing a fully-qualified function name.<br>
<br>
I.e. this is &quot;use&quot;:<br>
<br>
=C2=A0 =C2=A0 $ perl -MList::Util=3Dsum -wE &#39;if (shift) { say sum @ARGV=
 }&#39; 1 2 3<br>
=C2=A0 =C2=A0 5<br>
<br>
And this is the case you&#39;re talking about:<br>
<br>
=C2=A0 =C2=A0 $ perl -wE &#39;if (shift) { require List::Util; List::Util-&=
gt;import(&quot;sum&quot;); say sum @ARGV }&#39; 1 2 3<br>
=C2=A0 =C2=A0 Unquoted string &quot;sum&quot; may clash with future reserve=
d word at -e line 1.<br>
=C2=A0 =C2=A0 Name &quot;main::sum&quot; used only once: possible typo at -=
e line 1.<br>
=C2=A0 =C2=A0 say() on unopened filehandle sum at -e line 1.<br>
<br>
So yeah, you can&#39;t use &quot;sum&quot; without parens, however you can =
do:<br>
<br>
=C2=A0 =C2=A0 $ perl -wE &#39;if (shift) { require List::Util; List::Util-&=
gt;import(&quot;sum&quot;); say sum(@ARGV) }&#39; 1 2 3<br>
=C2=A0 =C2=A0 5<br>
<br>
As opposed to:<br>
<br>
=C2=A0 =C2=A0 $ perl -wE &#39;if (shift) { require List::Util; say List::Ut=
il::sum(@ARGV) }&#39; 1 2 3<br>
=C2=A0 =C2=A0 5<br>
<br>
Which I daresay is the thing most users care about, so they could do<br>
e.g. (new hypothehtical syntax):<br>
<br>
=C2=A0 =C2=A0 $ perl -wE &#39;if (shift) { require List::Util &quot;sum&quo=
t;; say sum(@ARGV) }&#39; 1 2 3<br>
=C2=A0 =C2=A0 5<br>
</blockquote></div><br></div><div class=3D"gmail_extra">It also matters to =
make the imported symbols resolvable at compile time (which is especially i=
mportant for any imported constants). This does not work:</div><div class=
=3D"gmail_extra"><br></div><div class=3D"gmail_extra">$ perl -E &#39;if (sh=
ift) { require List::Util; List::Util-&gt;import(&quot;sum&quot;); say +sum=
 @ARGV }&#39; 1 2 3</div><div class=3D"gmail_extra"><br></div><div class=3D=
"gmail_extra">-Dan</div></div>

--0000000000008e9ce5056960ae33--
0
grinnz
4/9/2018 1:54:09 AM
Reply: