use string in "use"

I've written my first re-usable modules in Perl, and all goes well. 
However, I want to know if / how I can use a string variable in the 
'use' clause.

In PHP I have a simple system of turning on/off debugging and version 
control.  Any file *1.html is development version.  I then have

$DEBUG=(preg_match('/1.htm/',$_SERVER['REQUEST_URI'])) ? '1' : '';
include_once("sql$DEBUG.inc");
include_once("security$DEBUG.inc");

This way I have a live and a development version of every HTML and every 
inc file, and putting any one file is simply a case of copying that file 
over.

I'm looking to replicate  this in Perl.  How can I do the following?

#!/usr/bin/perl -w

use warnings;
use strict;

my $DEBUG=($0=~/1$/) ? '1' : '';
use RW::Sql$DEBUG;
use RW::Docs$DEBUG;

I've found that you can use "require" and pass a path.  I understand 
that require is run time, while use is compile time.  Are there any 
down-sides to using require?
0
gary
12/14/2020 3:23:26 PM
perl.beginners 29388 articles. 4 followers. Follow

2 Replies
180 Views

Similar Articles

[PageSpeed] 50
Get it on Google Play
Get it on Apple App Store

I have not used it myself, but the =E2=80=98if=E2=80=99 module allows =
one to load modules conditionally:

use if CONDITION, MODULE =3D> ARGUMENTS;

See =E2=80=98perldoc if=E2=80=99 for more details.


> On Dec 14, 2020, at 7:23 AM, Gary Stainburn =
<gary.stainburn@ringways.co.uk> wrote:
>=20
> I've written my first re-usable modules in Perl, and all goes well. =
However, I want to know if / how I can use a string variable in the =
'use' clause.
>=20
> In PHP I have a simple system of turning on/off debugging and version =
control.  Any file *1.html is development version.  I then have
>=20
> $DEBUG=3D(preg_match('/1.htm/',$_SERVER['REQUEST_URI'])) ? '1' : '';
> include_once("sql$DEBUG.inc");
> include_once("security$DEBUG.inc");
>=20
> This way I have a live and a development version of every HTML and =
every inc file, and putting any one file is simply a case of copying =
that file over.
>=20
> I'm looking to replicate  this in Perl.  How can I do the following?
>=20
> #!/usr/bin/perl -w
>=20
> use warnings;
> use strict;
>=20
> my $DEBUG=3D($0=3D~/1$/) ? '1' : '';
> use RW::Sql$DEBUG;
> use RW::Docs$DEBUG;
>=20
> I've found that you can use "require" and pass a path.  I understand =
that require is run time, while use is compile time.  Are there any =
down-sides to using require?
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20

Jim Gibson
0
jimsgibson
12/14/2020 3:55:43 PM
--000000000000236cca05b68107a0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

The use function happens at compile time and must take a bareword, but it
is functionally equivalent to

BEGIN { require Module; Module->import( LIST ); }

And the require function allows you to pass a string, but be aware there
are lots of differences in behavior when passing a string vs a bareword:
https://perldoc.perl.org/functions/require



On Mon, Dec 14, 2020, 10:57 Jim Gibson <jimsgibson@gmail.com> wrote:

> I have not used it myself, but the =E2=80=98if=E2=80=99 module allows one=
 to load modules
> conditionally:
>
> use if CONDITION, MODULE =3D> ARGUMENTS;
>
> See =E2=80=98perldoc if=E2=80=99 for more details.
>
>
> > On Dec 14, 2020, at 7:23 AM, Gary Stainburn <
> gary.stainburn@ringways.co.uk> wrote:
> >
> > I've written my first re-usable modules in Perl, and all goes well.
> However, I want to know if / how I can use a string variable in the 'use'
> clause.
> >
> > In PHP I have a simple system of turning on/off debugging and version
> control.  Any file *1.html is development version.  I then have
> >
> > $DEBUG=3D(preg_match('/1.htm/',$_SERVER['REQUEST_URI'])) ? '1' : '';
> > include_once("sql$DEBUG.inc");
> > include_once("security$DEBUG.inc");
> >
> > This way I have a live and a development version of every HTML and ever=
y
> inc file, and putting any one file is simply a case of copying that file
> over.
> >
> > I'm looking to replicate  this in Perl.  How can I do the following?
> >
> > #!/usr/bin/perl -w
> >
> > use warnings;
> > use strict;
> >
> > my $DEBUG=3D($0=3D~/1$/) ? '1' : '';
> > use RW::Sql$DEBUG;
> > use RW::Docs$DEBUG;
> >
> > I've found that you can use "require" and pass a path.  I understand
> that require is run time, while use is compile time.  Are there any
> down-sides to using require?
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > http://learn.perl.org/
> >
> >
>
> Jim Gibson
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

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

<div dir=3D"auto">The use function happens at compile time and must take a =
bareword, but it is functionally equivalent to<div dir=3D"auto"><br></div><=
div dir=3D"auto">BEGIN {=C2=A0require Module; Module-&gt;import( LIST ); }<=
/div><div dir=3D"auto"><br></div><div dir=3D"auto">And the require function=
 allows you to pass a string, but be aware there are lots of differences in=
 behavior when passing a string vs a bareword:=C2=A0<a href=3D"https://perl=
doc.perl.org/functions/require">https://perldoc.perl.org/functions/require<=
/a></div><div dir=3D"auto"><br></div><br><br><div class=3D"gmail_quote" dir=
=3D"auto"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Dec 14, 2020, 10:57=
 Jim Gibson &lt;<a href=3D"mailto:jimsgibson@gmail.com">jimsgibson@gmail.co=
m</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin=
:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have not used it=
 myself, but the =E2=80=98if=E2=80=99 module allows one to load modules con=
ditionally:<br>
<br>
use if CONDITION, MODULE =3D&gt; ARGUMENTS;<br>
<br>
See =E2=80=98perldoc if=E2=80=99 for more details.<br>
<br>
<br>
&gt; On Dec 14, 2020, at 7:23 AM, Gary Stainburn &lt;<a href=3D"mailto:gary=
..stainburn@ringways.co.uk" target=3D"_blank" rel=3D"noreferrer">gary.stainb=
urn@ringways.co.uk</a>&gt; wrote:<br>
&gt; <br>
&gt; I&#39;ve written my first re-usable modules in Perl, and all goes well=
.. However, I want to know if / how I can use a string variable in the &#39;=
use&#39; clause.<br>
&gt; <br>
&gt; In PHP I have a simple system of turning on/off debugging and version =
control.=C2=A0 Any file *1.html is development version.=C2=A0 I then have<b=
r>
&gt; <br>
&gt; $DEBUG=3D(preg_match(&#39;/1.htm/&#39;,$_SERVER[&#39;REQUEST_URI&#39;]=
)) ? &#39;1&#39; : &#39;&#39;;<br>
&gt; include_once(&quot;sql$DEBUG.inc&quot;);<br>
&gt; include_once(&quot;security$DEBUG.inc&quot;);<br>
&gt; <br>
&gt; This way I have a live and a development version of every HTML and eve=
ry inc file, and putting any one file is simply a case of copying that file=
 over.<br>
&gt; <br>
&gt; I&#39;m looking to replicate=C2=A0 this in Perl.=C2=A0 How can I do th=
e following?<br>
&gt; <br>
&gt; #!/usr/bin/perl -w<br>
&gt; <br>
&gt; use warnings;<br>
&gt; use strict;<br>
&gt; <br>
&gt; my $DEBUG=3D($0=3D~/1$/) ? &#39;1&#39; : &#39;&#39;;<br>
&gt; use RW::Sql$DEBUG;<br>
&gt; use RW::Docs$DEBUG;<br>
&gt; <br>
&gt; I&#39;ve found that you can use &quot;require&quot; and pass a path.=
=C2=A0 I understand that require is run time, while use is compile time.=C2=
=A0 Are there any down-sides to using require?<br>
&gt; <br>
&gt; -- <br>
&gt; To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.o=
rg" target=3D"_blank" rel=3D"noreferrer">beginners-unsubscribe@perl.org</a>=
<br>
&gt; For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl=
..org" target=3D"_blank" rel=3D"noreferrer">beginners-help@perl.org</a><br>
&gt; <a href=3D"http://learn.perl.org/" rel=3D"noreferrer noreferrer" targe=
t=3D"_blank">http://learn.perl.org/</a><br>
&gt; <br>
&gt; <br>
<br>
Jim Gibson<br>
<br>
-- <br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org" t=
arget=3D"_blank" rel=3D"noreferrer">beginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
 target=3D"_blank" rel=3D"noreferrer">beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer noreferrer" target=3D"=
_blank">http://learn.perl.org/</a><br>
<br>
<br>
</blockquote></div></div>

--000000000000236cca05b68107a0--
0
chas
12/15/2020 1:49:17 PM
Reply: