modules and constants

Hi All,

Its there a way, if you import a module that is also
imports a bunch of constants into your main program
to be used globally?

If so, what is the syntax in the module and in the main program?

And what happends if there is a name conflict between
the module and main program?

Many thanks,
-T
0
perl6
12/6/2019 7:39:32 AM
perl.perl6.users 1278 articles. 0 followers. Follow

5 Replies
16 Views

Similar Articles

[PageSpeed] 13

--000000000000a473d10599047546
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Todd,

AFAIK Perl=E2=80=99s culture is not to use so many constants in actual prog=
ramming.
:)

Tom

On Fri, Dec 6, 2019 at 3:47 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> Its there a way, if you import a module that is also
> imports a bunch of constants into your main program
> to be used globally?
>
> If so, what is the syntax in the module and in the main program?
>
> And what happends if there is a name conflict between
> the module and main program?
>
> Many thanks,
> -T
>

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

<div><div dir=3D"auto">Todd,</div></div><div dir=3D"auto"><br></div><div di=
r=3D"auto">AFAIK Perl=E2=80=99s culture is not to use so many constants in =
actual programming. :)</div><div dir=3D"auto"><br></div><div dir=3D"auto">T=
om</div><div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Fri, Dec 6, 2019 at 3:47 PM ToddAndMargo via perl6-users &lt;<a h=
ref=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br>=
</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l=
eft:1px #ccc solid;padding-left:1ex">Hi All,<br>
<br>
Its there a way, if you import a module that is also<br>
imports a bunch of constants into your main program<br>
to be used globally?<br>
<br>
If so, what is the syntax in the module and in the main program?<br>
<br>
And what happends if there is a name conflict between<br>
the module and main program?<br>
<br>
Many thanks,<br>
-T<br>
</blockquote></div></div>

--000000000000a473d10599047546--
0
tom
12/6/2019 8:01:45 AM
On 2019-12-06 00:01, Tom Blackwood wrote:
> Todd,
>=20
> AFAIK Perl=E2=80=99s culture is not to use so many constants in actual =

> programming. :)
>=20
> Tom
>=20

Is there a way to make constants universal inside a module, or
do I have to declare them inside every sub?
0
perl6
12/6/2019 9:02:20 AM
--0000000000008e44c3059905d10d
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

If you define them in the top level of your module then all your subs have
access :

constant FOO =3D 2;

sub inc-by-foo( $a ) {
  $a+FOO;
}

On Fri, 6 Dec 2019 at 09:05, ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2019-12-06 00:01, Tom Blackwood wrote:
> > Todd,
> >
> > AFAIK Perl=E2=80=99s culture is not to use so many constants in actual
> > programming. :)
> >
> > Tom
> >
>
> Is there a way to make constants universal inside a module, or
> do I have to declare them inside every sub?
>


--=20
Simon Proctor
Cognoscite aliquid novum cotidie

http://www.khanate.co.uk/

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

<div dir=3D"ltr"><div>If you define them in the top level of your module th=
en all your subs have access :</div><div><br></div><div>constant FOO =3D 2;=
</div><div><br></div><div>sub inc-by-foo( $a ) {</div><div>=C2=A0 $a+FOO;<b=
r></div><div>}<br></div></div><br><div class=3D"gmail_quote"><div dir=3D"lt=
r" class=3D"gmail_attr">On Fri, 6 Dec 2019 at 09:05, ToddAndMargo via perl6=
-users &lt;<a href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>=
&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px =
0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2=
019-12-06 00:01, Tom Blackwood wrote:<br>
&gt; Todd,<br>
&gt; <br>
&gt; AFAIK Perl=E2=80=99s culture is not to use so many constants in actual=
 <br>
&gt; programming. :)<br>
&gt; <br>
&gt; Tom<br>
&gt; <br>
<br>
Is there a way to make constants universal inside a module, or<br>
do I have to declare them inside every sub?<br>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature">Simon Proctor<br>Cognoscite aliquid novum cotidie<br><br><a=
 href=3D"http://www.khanate.co.uk/" target=3D"_blank">http://www.khanate.co=
..uk/</a></div>

--0000000000008e44c3059905d10d--
0
simon
12/6/2019 9:39:02 AM
>> On Fri, 6 Dec 2019 at 09:05, ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>=20
>>     On 2019-12-06 00:01, Tom Blackwood wrote:
>>      > Todd,
>>      >
>>      > AFAIK Perl=E2=80=99s culture is not to use so many constants in=
 actual
>>      > programming. :)
>>      >
>>      > Tom
>>      >
>>=20
>>     Is there a way to make constants universal inside a module, or
>>     do I have to declare them inside every sub?

On 2019-12-06 01:39, Simon Proctor wrote:
> If you define them in the top level of your module then all your subs=20
> have access :
>=20
> constant FOO =3D 2;
>=20
> sub inc-by-foo( $a ) {
>  =C2=A0 $a+FOO;
> }
>=20

Do you mean

     sub inc-by-foo( $a ) is export {
        $a+FOO;
     }
?

I tried that several years ago and had no joy.  I
had to put the constants inside the sub for the sub
to see them.   Is this a change?
0
perl6
12/6/2019 11:06:57 AM
On 2019-12-05 23:39, ToddAndMargo via perl6-users wrote:
> Hi All,
> 
> Its there a way, if you import a module that is also
> imports a bunch of constants into your main program
> to be used globally?
> 
> If so, what is the syntax in the module and in the main program?
> 
> And what happends if there is a name conflict between
> the module and main program?
> 
> Many thanks,
> -T


Follow up:

With the help of Holli over on Stack Overflow,
I came up with a way I really like.

Thank you all for the tips!

-T

`anonymous constraint` is a bit obscure language,
but ...


My keeper on the subject:


Perl6: constraining variable in sub declarations:

References:
 
https://stackoverflow.com/questions/59222421/perl6-raku-how-to-i-restrict-the-values-allowed-in-a-variable
     https://docs.raku.org/type/Signature#index-entry-where_clause


Note: the `where clause` not confined to just subs.

Example format:
    sub foo( Int $binary where * ~~ 0|1 ) { ... }


Sample sub:

     sub abc( Str $x where * ~~ "abc" | "def" ) {say $x;}

     abc("abc")
     abc

     abc("def")
     def

     abc("hij")
     Constraint type check failed in binding to parameter '$x';
     expected anonymous constraint to be met but got Str ("hij")
0
perl6
12/7/2019 7:54:58 AM
Reply: