restricted value passed to a sub question

Hi All,

I am cooking up something where I want top pass a value to a sub, but I 
want to restrict what those values are.

For instance, things like

    AbortRetryIgnore
    CancelRetryContinue
    Help
    YesNo
    Maybe

And so on and so forth.

If the wrong value is passed, I want the checker to crash
the sub.

What is the best way of going about this?

Many thanks,
-T
0
perl6
12/4/2019 8:40:14 AM
perl.perl6.users 1455 articles. 0 followers. Follow

10 Replies
80 Views

Similar Articles

[PageSpeed] 41

--0000000000006c17b20598dd6fc3
Content-Type: text/plain; charset="UTF-8"

So I'd approach this in one of two ways. Firstly there's the multi sub with
constants option :

multi selector( "AbortRetryIgnore" ) { ... }
multi selector( "CancelRetryContinue" ) { ... }
multi selector( "Help" ) { ... }
multi selector( "YesNo" ) { ... }
multi selector( "Maybe" ) { ... }

I'd do this if each option is pretty different in what happens.

If on the other hand the way the options are handled pretty similar I'd go
with and Enum :

enum Option <AbortRetryIgnore CancelRetryContinue Help YesNo Maybe>;
subset OptStr of Str where { Options::{$_}:exists};

multi sub selector( OptStr $opt ) { callwith( Options::{$opt} ) }
multi sub selector( Option $opt ) { ... }

This lets us all the sub with with the Option enum value eg :
selector(YesNo) or the string variant (useful on the command line) eg :
selector("YesNo")

I'd do this if there's a lot of shared functionality in the selector sub.

Hope that helps.

You can combine the two options of course :)

enum Option <AbortRetryIgnore CancelRetryContinue Help YesNo Maybe>;
subset OptStr of Str where { Options::{$_}:exists};
multi sub selector( OptStr $opt ) { callwith( Options::{$opt} ) }

multi selector( AbortRetryIgnore ) { ... }
multi selector( CancelRetryContinue) { ... }
multi selector( Help ) { ... }
multi selector( YesNo ) { ... }
multi selector( Maybe ) { ... }

Simon


On Wed, 4 Dec 2019 at 08:45, ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> I am cooking up something where I want top pass a value to a sub, but I
> want to restrict what those values are.
>
> For instance, things like
>
>     AbortRetryIgnore
>     CancelRetryContinue
>     Help
>     YesNo
>     Maybe
>
> And so on and so forth.
>
> If the wrong value is passed, I want the checker to crash
> the sub.
>
> What is the best way of going about this?
>
> Many thanks,
> -T
>


-- 
Simon Proctor
Cognoscite aliquid novum cotidie

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

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

<div dir=3D"ltr"><div>So I&#39;d approach this in one of two ways. Firstly =
there&#39;s the multi sub with constants option :</div><div><br></div><div>=
multi selector( &quot;AbortRetryIgnore&quot; ) { ... }</div><div>multi sele=
ctor( &quot;CancelRetryContinue&quot; ) { ... }</div><div>multi selector( &=
quot;Help&quot; ) { ... }</div><div>multi selector( &quot;YesNo&quot; ) { .=
... }</div><div>multi selector( &quot;Maybe&quot; ) { ... }</div><div><br></=
div><div>I&#39;d do this if each option is pretty different in what happens=
.. <br></div><div><br></div><div>If on the other hand the way the options ar=
e handled pretty similar I&#39;d go with and Enum :</div><div><br></div><di=
v>enum Option &lt;AbortRetryIgnore CancelRetryContinue Help YesNo Maybe&gt;=
;</div><div>subset OptStr of Str where { Options::{$_}:exists};</div><div><=
br></div><div>multi sub selector( OptStr $opt ) { callwith( Options::{$opt}=
 ) }</div><div>multi sub selector( Option $opt ) { ... }</div><div><br></di=
v><div>This lets us all the sub with with the Option enum value eg : select=
or(YesNo) or the string variant (useful on the command line) eg : selector(=
&quot;YesNo&quot;) <br></div><div><br></div><div>I&#39;d do this if there&#=
39;s a lot of shared functionality in the selector sub. <br></div><div><br>=
</div><div>Hope that helps. </div><div><br></div><div>You can combine the t=
wo options of course :) <br></div><div><br></div><div><div>enum Option &lt;=
AbortRetryIgnore CancelRetryContinue Help YesNo Maybe&gt;;</div><div>subset=
 OptStr of Str where { Options::{$_}:exists};</div><div>multi sub selector(=
 OptStr $opt ) { callwith( Options::{$opt} ) }</div><div><br></div><div><di=
v>multi selector( AbortRetryIgnore ) { ... }</div><div>multi selector( Canc=
elRetryContinue) { ... }</div><div>multi selector( Help ) { ... }</div><div=
>multi selector( YesNo ) { ... }</div><div>multi selector( Maybe ) { ... }<=
/div><div><br></div><div>Simon<br></div></div><div><br></div></div></div><b=
r><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, =
4 Dec 2019 at 08:45, ToddAndMargo via perl6-users &lt;<a href=3D"mailto:per=
l6-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 so=
lid rgb(204,204,204);padding-left:1ex">Hi All,<br>
<br>
I am cooking up something where I want top pass a value to a sub, but I <br=
>
want to restrict what those values are.<br>
<br>
For instance, things like<br>
<br>
=C2=A0 =C2=A0 AbortRetryIgnore<br>
=C2=A0 =C2=A0 CancelRetryContinue<br>
=C2=A0 =C2=A0 Help<br>
=C2=A0 =C2=A0 YesNo<br>
=C2=A0 =C2=A0 Maybe<br>
<br>
And so on and so forth.<br>
<br>
If the wrong value is passed, I want the checker to crash<br>
the sub.<br>
<br>
What is the best way of going about this?<br>
<br>
Many thanks,<br>
-T<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>

--0000000000006c17b20598dd6fc3--
0
simon
12/4/2019 9:28:17 AM
Hi Todd,

Chapter 9 (Associatives) of "Learning Perl 6" by brian d foy has a
section on Maps, "the immutable mapping of zero or more keys to
values". In that section there are subsections entitled 'Checking
Keys', 'Creating from a Positional' and 'Checking Allowed Values.'

HTH, Bill.

On Wed, Dec 4, 2019 at 12:41 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> Hi All,
>
> I am cooking up something where I want top pass a value to a sub, but I
> want to restrict what those values are.
>
> For instance, things like
>
>     AbortRetryIgnore
>     CancelRetryContinue
>     Help
>     YesNo
>     Maybe
>
> And so on and so forth.
>
> If the wrong value is passed, I want the checker to crash
> the sub.
>
> What is the best way of going about this?
>
> Many thanks,
> -T
0
perl6
12/4/2019 9:44:43 AM
On 2019-12-04 01:44, William Michels via perl6-users wrote:
> Hi Todd,
> 
> Chapter 9 (Associatives) of "Learning Perl 6" by brian d foy has a
> section on Maps, "the immutable mapping of zero or more keys to
> values". In that section there are subsections entitled 'Checking
> Keys', 'Creating from a Positional' and 'Checking Allowed Values.'
> 
> HTH, Bill.
> 
> On Wed, Dec 4, 2019 at 12:41 AM ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>>
>> Hi All,
>>
>> I am cooking up something where I want top pass a value to a sub, but I
>> want to restrict what those values are.
>>
>> For instance, things like
>>
>>      AbortRetryIgnore
>>      CancelRetryContinue
>>      Help
>>      YesNo
>>      Maybe
>>
>> And so on and so forth.
>>
>> If the wrong value is passed, I want the checker to crash
>> the sub.
>>
>> What is the best way of going about this?
>>
>> Many thanks,
>> -T

Hi Bill,

I can't have books in my house.  It is along story.

But, based on what you said, I found this;

https://docs.perl6.org/type/Map.html

The docs are really poorly written for beginners, but
it is a good start

Thank you!
-T
0
perl6
12/4/2019 10:02:54 AM
>> On Wed, 4 Dec 2019 at 08:45, ToddAndMargo via perl6-users 
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>> 
>>     Hi All,
>> 
>>     I am cooking up something where I want top pass a value to a sub, but I
>>     want to restrict what those values are.
>> 
>>     For instance, things like
>> 
>>          AbortRetryIgnore
>>          CancelRetryContinue
>>          Help
>>          YesNo
>>          Maybe
>> 
>>     And so on and so forth.
>> 
>>     If the wrong value is passed, I want the checker to crash
>>     the sub.
>> 
>>     What is the best way of going about this?
>> 
>>     Many thanks,
>>     -T
>>


On 2019-12-04 01:28, Simon Proctor wrote:
> So I'd approach this in one of two ways. Firstly there's the multi sub 
> with constants option :
> 
> multi selector( "AbortRetryIgnore" ) { ... }
> multi selector( "CancelRetryContinue" ) { ... }
> multi selector( "Help" ) { ... }
> multi selector( "YesNo" ) { ... }
> multi selector( "Maybe" ) { ... }
> 
> I'd do this if each option is pretty different in what happens.
> 
> If on the other hand the way the options are handled pretty similar I'd 
> go with and Enum :
> 
> enum Option <AbortRetryIgnore CancelRetryContinue Help YesNo Maybe>;
> subset OptStr of Str where { Options::{$_}:exists};
> 
> multi sub selector( OptStr $opt ) { callwith( Options::{$opt} ) }
> multi sub selector( Option $opt ) { ... }
> 
> This lets us all the sub with with the Option enum value eg : 
> selector(YesNo) or the string variant (useful on the command line) eg : 
> selector("YesNo")
> 
> I'd do this if there's a lot of shared functionality in the selector sub.
> 
> Hope that helps.
> 
> You can combine the two options of course :)
> 
> enum Option <AbortRetryIgnore CancelRetryContinue Help YesNo Maybe>;
> subset OptStr of Str where { Options::{$_}:exists};
> multi sub selector( OptStr $opt ) { callwith( Options::{$opt} ) }
> 
> multi selector( AbortRetryIgnore ) { ... }
> multi selector( CancelRetryContinue) { ... }
> multi selector( Help ) { ... }
> multi selector( YesNo ) { ... }
> multi selector( Maybe ) { ... }
> 
> Simon

Hi Simon,

You are way over my head. I am going to have to read it over several 
times and do a lot of learning.

Thank you for the help!

-T
0
perl6
12/4/2019 10:04:23 AM
--000000000000993cf40598de0d80
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

El mi=C3=A9., 4 dic. 2019 a las 11:06, ToddAndMargo via perl6-users (<
perl6-users@perl.org>) escribi=C3=B3:

> On 2019-12-04 01:44, William Michels via perl6-users wrote:
> > Hi Todd,
> >
> > Chapter 9 (Associatives) of "Learning Perl 6" by brian d foy has a
> > section on Maps, "the immutable mapping of zero or more keys to
> > values". In that section there are subsections entitled 'Checking
> > Keys', 'Creating from a Positional' and 'Checking Allowed Values.'
> >
> > HTH, Bill.
> >
> > On Wed, Dec 4, 2019 at 12:41 AM ToddAndMargo via perl6-users
> > <perl6-users@perl.org> wrote:
> >>
> >> Hi All,
> >>
> >> I am cooking up something where I want top pass a value to a sub, but =
I
> >> want to restrict what those values are.
> >>
> >> For instance, things like
> >>
> >>      AbortRetryIgnore
> >>      CancelRetryContinue
> >>      Help
> >>      YesNo
> >>      Maybe
> >>
> >> And so on and so forth.
> >>
> >> If the wrong value is passed, I want the checker to crash
> >> the sub.
> >>
> >> What is the best way of going about this?
> >>
> >> Many thanks,
> >> -T
>
> Hi Bill,
>
> I can't have books in my house.  It is along story.
>
> But, based on what you said, I found this;
>
> https://docs.perl6.org/type/Map.html


 Please use this now: https://docs.raku.org/type/Map.html

(also, we should probably move the deprecation notice from the footer to
the header...)

JJ

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

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">El mi=C3=A9., 4 dic. 2019 a las 11:06=
, ToddAndMargo via perl6-users (&lt;<a href=3D"mailto:perl6-users@perl.org"=
>perl6-users@perl.org</a>&gt;) escribi=C3=B3:<br></div><blockquote class=3D=
"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(2=
04,204,204);padding-left:1ex">On 2019-12-04 01:44, William Michels via perl=
6-users wrote:<br>
&gt; Hi Todd,<br>
&gt; <br>
&gt; Chapter 9 (Associatives) of &quot;Learning Perl 6&quot; by brian d foy=
 has a<br>
&gt; section on Maps, &quot;the immutable mapping of zero or more keys to<b=
r>
&gt; values&quot;. In that section there are subsections entitled &#39;Chec=
king<br>
&gt; Keys&#39;, &#39;Creating from a Positional&#39; and &#39;Checking Allo=
wed Values.&#39;<br>
&gt; <br>
&gt; HTH, Bill.<br>
&gt; <br>
&gt; On Wed, Dec 4, 2019 at 12:41 AM ToddAndMargo via perl6-users<br>
&gt; &lt;<a href=3D"mailto:perl6-users@perl.org" target=3D"_blank">perl6-us=
ers@perl.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi All,<br>
&gt;&gt;<br>
&gt;&gt; I am cooking up something where I want top pass a value to a sub, =
but I<br>
&gt;&gt; want to restrict what those values are.<br>
&gt;&gt;<br>
&gt;&gt; For instance, things like<br>
&gt;&gt;<br>
&gt;&gt;=C2=A0 =C2=A0 =C2=A0 AbortRetryIgnore<br>
&gt;&gt;=C2=A0 =C2=A0 =C2=A0 CancelRetryContinue<br>
&gt;&gt;=C2=A0 =C2=A0 =C2=A0 Help<br>
&gt;&gt;=C2=A0 =C2=A0 =C2=A0 YesNo<br>
&gt;&gt;=C2=A0 =C2=A0 =C2=A0 Maybe<br>
&gt;&gt;<br>
&gt;&gt; And so on and so forth.<br>
&gt;&gt;<br>
&gt;&gt; If the wrong value is passed, I want the checker to crash<br>
&gt;&gt; the sub.<br>
&gt;&gt;<br>
&gt;&gt; What is the best way of going about this?<br>
&gt;&gt;<br>
&gt;&gt; Many thanks,<br>
&gt;&gt; -T<br>
<br>
Hi Bill,<br>
<br>
I can&#39;t have books in my house.=C2=A0 It is along story.<br>
<br>
But, based on what you said, I found this;<br>
<br>
<a href=3D"https://docs.perl6.org/type/Map.html" rel=3D"noreferrer" target=
=3D"_blank">https://docs.perl6.org/type/Map.html</a></blockquote><div><br><=
/div><div>=C2=A0Please use this now: <a href=3D"https://docs.raku.org/type/=
Map.html">https://docs.raku.org/type/Map.html</a></div><div><br></div><div>=
(also, we should probably move the deprecation notice from the footer to th=
e header...)</div><div><br></div><div>JJ</div></div></div>

--000000000000993cf40598de0d80--
0
jjmerelo
12/4/2019 10:12:29 AM
>>=20
>>=20
>> El mi=C3=A9., 4 dic. 2019 a las 11:06, ToddAndMargo via perl6-users=20
> =20
>>     https://docs.perl6.org/type/Map.html
>>=20
>>=20

On 2019-12-04 02:12, JJ Merelo wrote:
>  =C2=A0Please use this now: https://docs.raku.org/type/Map.html
>=20
> (also, we should probably move the deprecation notice from the footer t=
o=20
> the header...)
>=20
> JJ

Thank you.  Guess I am behind the time!

I don't personally care that we call Perl 6.  It
is a TON better than Perl 5.
0
perl6
12/4/2019 10:19:52 AM
On 2019-12-04 04:25, Mark Senn wrote:
>> I can't have books in my house.  It is along story.
> 
> If you can have ebooks a Google search showed
> 
> https://www.amazon.com/Learning-Perl-Keeping-Impossible-Within-ebook-dp-B07GT9KPP1/dp/B07GT9KPP1/ref=mt_kindle?_encoding=UTF8&me=&qid=
> 
> for Kindle versions or
> 
>      https://vechaz.com/product/learning-perl-6-keeping-the-easy-hard-and-impossible-within-reach-ebook-pdf-version/?gclid=EAIaIQobChMIiZzE-fub5gIVEdvACh39wgErEAQYBCABEgLHlvD_BwE
> 
>      https://sheellas.com/product/learning-perl-6-keeping-the-easy-hard-and-impossible-within-reach-ebook-pdf-1-1977/?gclid=EAIaIQobChMIiZzE-fub5gIVEdvACh39wgErEAQYBSABEgKjFPD_BwE
> 
> for PDF versions.
> 
> Mark Senn, Senior Software Engineer,
> Engineering Computer Network, Purdue University
> 

Love PDF's.  Thank you!
0
perl6
12/4/2019 5:10:06 PM
If you want to view a publisher-authorized preview of brian d foy's
"Learning Perl 6" book, here's a good place to start (there's also a
link to purchase an eBook):

https://books.google.com/books?id=sbRqDwAAQBAJ

Todd, you could try searching in the search box for: "Checking Allowed Values".

HTH, Bill.

On Wed, Dec 4, 2019 at 9:17 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> On 2019-12-04 04:25, Mark Senn wrote:
> >> I can't have books in my house.  It is along story.
> >
> > If you can have ebooks a Google search showed
> >
> > https://www.amazon.com/Learning-Perl-Keeping-Impossible-Within-ebook-dp-B07GT9KPP1/dp/B07GT9KPP1/ref=mt_kindle?_encoding=UTF8&me=&qid=
> >
> > for Kindle versions or
> >
> >      https://vechaz.com/product/learning-perl-6-keeping-the-easy-hard-and-impossible-within-reach-ebook-pdf-version/?gclid=EAIaIQobChMIiZzE-fub5gIVEdvACh39wgErEAQYBCABEgLHlvD_BwE
> >
> >      https://sheellas.com/product/learning-perl-6-keeping-the-easy-hard-and-impossible-within-reach-ebook-pdf-1-1977/?gclid=EAIaIQobChMIiZzE-fub5gIVEdvACh39wgErEAQYBSABEgKjFPD_BwE
> >
> > for PDF versions.
> >
> > Mark Senn, Senior Software Engineer,
> > Engineering Computer Network, Purdue University
> >
>
> Love PDF's.  Thank you!
0
perl6
12/5/2019 11:25:52 AM
On 2019-12-05 03:25, William Michels via perl6-users wrote:
> If you want to view a publisher-authorized preview of brian d foy's
> "Learning Perl 6" book, here's a good place to start (there's also a
> link to purchase an eBook):
> 
> https://books.google.com/books?id=sbRqDwAAQBAJ
> 
> Todd, you could try searching in the search box for: "Checking Allowed Values".
> 
> HTH, Bill.

Hi Bill,

I will look it over.

Typically, I do very poorly with these kinds of books.
I have the attention span of a gnat.

    say "Hi World"

And my head hits the table.  Especially when
my head is racing way ahead.  (Raku is NOT my
first language.)

But I will look at it.


I found
     https://andrewshitov.com/category/using-perl-6/

and I really adore is writing.  He makes the complex simple.

What I find works for me is just to dive in a do what
I need done.

Thank you for the tips!
-T
0
perl6
12/6/2019 6:46:06 AM
On 2019-12-04 00:40, ToddAndMargo via perl6-users wrote:
> Hi All,
>=20
> I am cooking up something where I want top pass a value to a sub, but I=
=20
> want to restrict what those values are.
>=20
> For instance, things like
>=20
>  =C2=A0=C2=A0 AbortRetryIgnore
>  =C2=A0=C2=A0 CancelRetryContinue
>  =C2=A0=C2=A0 Help
>  =C2=A0=C2=A0 YesNo
>  =C2=A0=C2=A0 Maybe
>=20
> And so on and so forth.
>=20
> If the wrong value is passed, I want the checker to crash
> the sub.
>=20
> What is the best way of going about this?
>=20
> Many thanks,
> -T



Follow up:

This may be a duplicate. But I can't find it in my sent bin,
so I am re-sending my keeper on what I came up with that
I like.


Perl6: constraining variable in sub declarations:

References:
=20
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 9:36:37 AM
Reply: