**@args question

Hi All,

Looking at

    https://docs.perl6.org/routine/print

I see

    multi sub print(**@args --> True)

Question.  If I wanted to create my own print routine
using **@args, how would I declare it?

    sub printx( **@args data ) {...}


Many thanks,
-T


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When you say, "I wrote a program that
crashed Windows," people just stare at
you blankly and say, "Hey, I got those
with the system, for free."
      -- Linus Torvalds
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
1/3/2019 2:40:36 AM
perl.perl6.users 1186 articles. 0 followers. Follow

8 Replies
68 Views

Similar Articles

[PageSpeed] 49

The variable name in :(**@args) is @args, it could be any valid array
identifier like @data

    sub printx( **@data ){=E2=80=A6}

Note that there are several forms for slurpy positional arguments.

    :(   @a ) # not slurpy at all, takes one Positional argument and
aliases it as @a.

    :(  *@a ) # flattening slurpy  (1,),2,3 =E2=86=92 1,2,3 and ((1,),2,3) =
=E2=86=92 1,2,3
    :( **@a ) # structured slurpy (1,),2,3 =E2=86=92 (1,),2,3 and ((1,),2,3=
) =E2=86=92
((1,),2,3) # note there is no change
    :(  +@a ) # one-arg rule slurpy (1,),2,3 =E2=86=92 (1,),2,3 and ((1,),2=
,3)
=E2=86=92 (1,),2,3 # note that the second one changes

Note that itemized values do not get flattened for :(*@a), and that
Array values are itemized.

I like to consider the one-arg rule slurpy :(+@a) to be like a
combination between non-slurpy :(@a) and structured slurpy :(**@a)
That is a one-arg rule slurpy will sometimes be like an alias to a
singular positional, otherwise it will act like a structured slurpy.

Note that all but the [aliasing] non-slurpy :(@a) are [almost] always Array=
's.

---

The one-arg rule slurpy is the oddest one of the bunch so here is some
brief information:

The one-arg rule slurpy can be sigiless :(+a), in which case it will
be a List instead of an Array or an alias to the single argument
depending on what it was.

    sub one-arg-rule ( +args ){
        say args.^name, " # ", args.perl;
    }

    one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
    one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg rule
    one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq # one-arg rule
    one-arg-rule(  1..3   ); # List # (1, 2, 3)
    one-arg-rule( (1..3,) ); # List # (1..3,)

    sub one-arg-Array ( +@args ){
        say @args.^name, " # ", @args.perl;
    }

    one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
    one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
    one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
    one-arg-Array(  1..3   ); # Array # [1, 2, 3]
    one-arg-Array( (1..3,) ); # Array # [1..3,]

The one-arg rule exists because people tend to write the following:

    my @a =3D [1,2,3];

When the correct way to write it is:

    my @a =3D 1,2,3;

There are various other places where the one-arg rule is also used, so
it was made available to everyone.

---

Don't get too flustered if you don't understand all of the nuances, it
take everybody a while to understand them.

On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> Hi All,
>
> Looking at
>
>     https://docs.perl6.org/routine/print
>
> I see
>
>     multi sub print(**@args --> True)
>
> Question.  If I wanted to create my own print routine
> using **@args, how would I declare it?
>
>     sub printx( **@args data ) {...}
>
>
> Many thanks,
> -T
>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> When you say, "I wrote a program that
> crashed Windows," people just stare at
> you blankly and say, "Hey, I got those
> with the system, for free."
>       -- Linus Torvalds
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
b2gills
1/3/2019 6:06:13 AM
 > On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
 > <perl6-users@perl.org> wrote:
 >>
 >> Hi All,
 >>
 >> Looking at
 >>
 >>      https://docs.perl6.org/routine/print
 >>
 >> I see
 >>
 >>      multi sub print(**@args --> True)
 >>
 >> Question.  If I wanted to create my own print routine
 >> using **@args, how would I declare it?
 >>
 >>      sub printx( **@args data ) {...}
 >>
 >>
 >> Many thanks,
 >> -T


On 1/2/19 10:06 PM, Brad Gilbert wrote:
> The variable name in :(**@args) is @args, it could be any valid array
> identifier like @data
>=20
>      sub printx( **@data ){=E2=80=A6}
>=20
> Note that there are several forms for slurpy positional arguments.
>=20
>      :(   @a ) # not slurpy at all, takes one Positional argument and
> aliases it as @a.
>=20
>      :(  *@a ) # flattening slurpy  (1,),2,3 =E2=86=92 1,2,3 and ((1,),=
2,3) =E2=86=92 1,2,3
>      :( **@a ) # structured slurpy (1,),2,3 =E2=86=92 (1,),2,3 and ((1,=
),2,3) =E2=86=92
> ((1,),2,3) # note there is no change
>      :(  +@a ) # one-arg rule slurpy (1,),2,3 =E2=86=92 (1,),2,3 and ((=
1,),2,3)
> =E2=86=92 (1,),2,3 # note that the second one changes
>=20
> Note that itemized values do not get flattened for :(*@a), and that
> Array values are itemized.
>=20
> I like to consider the one-arg rule slurpy :(+@a) to be like a
> combination between non-slurpy :(@a) and structured slurpy :(**@a)
> That is a one-arg rule slurpy will sometimes be like an alias to a
> singular positional, otherwise it will act like a structured slurpy.
>=20
> Note that all but the [aliasing] non-slurpy :(@a) are [almost] always A=
rray's.
>=20
> ---
>=20
> The one-arg rule slurpy is the oddest one of the bunch so here is some
> brief information:
>=20
> The one-arg rule slurpy can be sigiless :(+a), in which case it will
> be a List instead of an Array or an alias to the single argument
> depending on what it was.
>=20
>      sub one-arg-rule ( +args ){
>          say args.^name, " # ", args.perl;
>      }
>=20
>      one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
>      one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg rule
>      one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq # one-arg=
 rule
>      one-arg-rule(  1..3   ); # List # (1, 2, 3)
>      one-arg-rule( (1..3,) ); # List # (1..3,)
>=20
>      sub one-arg-Array ( +@args ){
>          say @args.^name, " # ", @args.perl;
>      }
>=20
>      one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
>      one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
>      one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
>      one-arg-Array(  1..3   ); # Array # [1, 2, 3]
>      one-arg-Array( (1..3,) ); # Array # [1..3,]
>=20
> The one-arg rule exists because people tend to write the following:
>=20
>      my @a =3D [1,2,3];
>=20
> When the correct way to write it is:
>=20
>      my @a =3D 1,2,3;
>=20
> There are various other places where the one-arg rule is also used, so
> it was made available to everyone.
>=20
> ---
>=20
> Don't get too flustered if you don't understand all of the nuances, it
> take everybody a while to understand them.
>=20

Thank you!

The term "slurpy" did help a lot.

:-)

I am writing your explanation down for my records.
0
perl6
1/3/2019 6:17:49 AM
On 1/2/19 10:17 PM, ToddAndMargo via perl6-users wrote:
>  > On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
>  > <perl6-users@perl.org> wrote:
>  >>
>  >> Hi All,
>  >>
>  >> Looking at
>  >>
>  >>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 https://docs.perl6.org/routine/print
>  >>
>  >> I see
>  >>
>  >>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 multi sub print(**@args --> True)
>  >>
>  >> Question.=C2=A0 If I wanted to create my own print routine
>  >> using **@args, how would I declare it?
>  >>
>  >>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub printx( **@args data ) {...}
>  >>
>  >>
>  >> Many thanks,
>  >> -T
>=20
>=20
> On 1/2/19 10:06 PM, Brad Gilbert wrote:
>> The variable name in :(**@args) is @args, it could be any valid array
>> identifier like @data
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 sub printx( **@data ){=E2=80=A6}
>>
>> Note that there are several forms for slurpy positional arguments.
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 :(=C2=A0=C2=A0 @a ) # not slurpy at all, take=
s one Positional argument and
>> aliases it as @a.
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 :(=C2=A0 *@a ) # flattening slurpy=C2=A0 (1,)=
,2,3 =E2=86=92 1,2,3 and ((1,),2,3) =E2=86=92=20
>> 1,2,3
>> =C2=A0=C2=A0=C2=A0=C2=A0 :( **@a ) # structured slurpy (1,),2,3 =E2=86=
=92 (1,),2,3 and ((1,),2,3) =E2=86=92
>> ((1,),2,3) # note there is no change
>> =C2=A0=C2=A0=C2=A0=C2=A0 :(=C2=A0 +@a ) # one-arg rule slurpy (1,),2,3=
 =E2=86=92 (1,),2,3 and ((1,),2,3)
>> =E2=86=92 (1,),2,3 # note that the second one changes
>>
>> Note that itemized values do not get flattened for :(*@a), and that
>> Array values are itemized.
>>
>> I like to consider the one-arg rule slurpy :(+@a) to be like a
>> combination between non-slurpy :(@a) and structured slurpy :(**@a)
>> That is a one-arg rule slurpy will sometimes be like an alias to a
>> singular positional, otherwise it will act like a structured slurpy.
>>
>> Note that all but the [aliasing] non-slurpy :(@a) are [almost] always =

>> Array's.
>>
>> ---
>>
>> The one-arg rule slurpy is the oddest one of the bunch so here is some=

>> brief information:
>>
>> The one-arg rule slurpy can be sigiless :(+a), in which case it will
>> be a List instead of an Array or an alias to the single argument
>> depending on what it was.
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 sub one-arg-rule ( +args ){
>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 say args.^name, " # "=
, args.perl;
>> =C2=A0=C2=A0=C2=A0=C2=A0 }
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule(=C2=A0 (1,),2,3=C2=A0 ); # List =
# ((1,), 2, 3)
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule( ((1,),2,3) ); # List # ((1,), 2=
, 3) # one-arg rule
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,)=
, 2, 3).Seq #=20
>> one-arg rule
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule(=C2=A0 1..3=C2=A0=C2=A0 ); # Lis=
t # (1, 2, 3)
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule( (1..3,) ); # List # (1..3,)
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 sub one-arg-Array ( +@args ){
>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 say @args.^name, " # =
", @args.perl;
>> =C2=A0=C2=A0=C2=A0=C2=A0 }
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array(=C2=A0 (1,),2,3=C2=A0 ); # Arra=
y # [(1,), 2, 3]
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array( ((1,),2,3) ); # Array # [(1,),=
 2, 3]
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array( ((1,),2,3).Seq ); # List # ((1=
,), 2, 3)
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array(=C2=A0 1..3=C2=A0=C2=A0 ); # Ar=
ray # [1, 2, 3]
>> =C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array( (1..3,) ); # Array # [1..3,]
>>
>> The one-arg rule exists because people tend to write the following:
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 my @a =3D [1,2,3];
>>
>> When the correct way to write it is:
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 my @a =3D 1,2,3;
>>
>> There are various other places where the one-arg rule is also used, so=

>> it was made available to everyone.
>>
>> ---
>>
>> Don't get too flustered if you don't understand all of the nuances, it=

>> take everybody a while to understand them.
>>
>=20
> Thank you!
>=20
> The term "slurpy" did help a lot.
>=20
> :-)
>=20
> I am writing your explanation down for my records.


Well Golly!

$ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
abc 1 def

$ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
abc 1 def

Question:
$ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'
abc xyz def

Are the spaces suppose to be there?
0
perl6
1/3/2019 6:27:57 AM
<snip>
>>
>> The term "slurpy" did help a lot.
>>
>> :-)
>>
>> I am writing your explanation down for my records.
>
>
> Well Golly!
>
> $ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
> abc 1 def
>
> $ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
> abc 1 def
>
> Question:
> $ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'
> abc xyz def
>
> Are the spaces suppose to be there?

In each case you are using 'print @args', then `printx` adds on a "\n" 
either to the last item or the final string, which in each case results 
in exactly the same string. The way 'print' handles an array is to put a 
space between each item, which can be very confusing when dealing with 
items that are themselves strings with spaces.

For other "stringifications" of an array, try 'print @args.fmt("%s,")' 
if you want a comma, or 'print @args.perl'.

I find '.say for @args' is better because I get an item on each line.

Also 'dd @args' is quite useful.
0
rnhainsworth
1/3/2019 8:43:56 AM
On Thu, Jan 3, 2019 at 12:43 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> On 1/2/19 10:17 PM, ToddAndMargo via perl6-users wrote:
> >  > On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
> >  > <perl6-users@perl.org> wrote:
> >  >>
> >  >> Hi All,
> >  >>
> >  >> Looking at
> >  >>
> >  >>      https://docs.perl6.org/routine/print
> >  >>
> >  >> I see
> >  >>
> >  >>      multi sub print(**@args --> True)
> >  >>
> >  >> Question.  If I wanted to create my own print routine
> >  >> using **@args, how would I declare it?
> >  >>
> >  >>      sub printx( **@args data ) {...}
> >  >>
> >  >>
> >  >> Many thanks,
> >  >> -T
> >
> >
> > On 1/2/19 10:06 PM, Brad Gilbert wrote:
> >> The variable name in :(**@args) is @args, it could be any valid array
> >> identifier like @data
> >>
> >>      sub printx( **@data ){=E2=80=A6}
> >>
> >> Note that there are several forms for slurpy positional arguments.
> >>
> >>      :(   @a ) # not slurpy at all, takes one Positional argument and
> >> aliases it as @a.
> >>
> >>      :(  *@a ) # flattening slurpy  (1,),2,3 =E2=86=92 1,2,3 and ((1,)=
,2,3) =E2=86=92
> >> 1,2,3
> >>      :( **@a ) # structured slurpy (1,),2,3 =E2=86=92 (1,),2,3 and ((1=
,),2,3) =E2=86=92
> >> ((1,),2,3) # note there is no change
> >>      :(  +@a ) # one-arg rule slurpy (1,),2,3 =E2=86=92 (1,),2,3 and (=
(1,),2,3)
> >> =E2=86=92 (1,),2,3 # note that the second one changes
> >>
> >> Note that itemized values do not get flattened for :(*@a), and that
> >> Array values are itemized.
> >>
> >> I like to consider the one-arg rule slurpy :(+@a) to be like a
> >> combination between non-slurpy :(@a) and structured slurpy :(**@a)
> >> That is a one-arg rule slurpy will sometimes be like an alias to a
> >> singular positional, otherwise it will act like a structured slurpy.
> >>
> >> Note that all but the [aliasing] non-slurpy :(@a) are [almost] always
> >> Array's.
> >>
> >> ---
> >>
> >> The one-arg rule slurpy is the oddest one of the bunch so here is some
> >> brief information:
> >>
> >> The one-arg rule slurpy can be sigiless :(+a), in which case it will
> >> be a List instead of an Array or an alias to the single argument
> >> depending on what it was.
> >>
> >>      sub one-arg-rule ( +args ){
> >>          say args.^name, " # ", args.perl;
> >>      }
> >>
> >>      one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
> >>      one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg rule
> >>      one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq #
> >> one-arg rule
> >>      one-arg-rule(  1..3   ); # List # (1, 2, 3)
> >>      one-arg-rule( (1..3,) ); # List # (1..3,)
> >>
> >>      sub one-arg-Array ( +@args ){
> >>          say @args.^name, " # ", @args.perl;
> >>      }
> >>
> >>      one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
> >>      one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
> >>      one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
> >>      one-arg-Array(  1..3   ); # Array # [1, 2, 3]
> >>      one-arg-Array( (1..3,) ); # Array # [1..3,]
> >>
> >> The one-arg rule exists because people tend to write the following:
> >>
> >>      my @a =3D [1,2,3];
> >>
> >> When the correct way to write it is:
> >>
> >>      my @a =3D 1,2,3;
> >>
> >> There are various other places where the one-arg rule is also used, so
> >> it was made available to everyone.
> >>
> >> ---
> >>
> >> Don't get too flustered if you don't understand all of the nuances, it
> >> take everybody a while to understand them.
> >>
> >
> > Thank you!
> >
> > The term "slurpy" did help a lot.
> >
> > :-)
> >
> > I am writing your explanation down for my records.
>
>
> Well Golly!
>
> $ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
> abc 1 def
>
> $ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
> abc 1 def
>
> Question:
> $ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'
> abc xyz def
>
> Are the spaces suppose to be there?

The spaces are there because you are passing `@args` to `print` as a
single value.
`print` coerces all its values to `Str` before printing, and an Array
adds spaces between elements when turned into a Str.

Simple fix use `|@args` to Slip it into the call to `print`

    $ p6 'sub printx(**@args){print( |@args, "\n" )};
printx("abc","xyz","def");'
    abcxyzdef
0
b2gills
1/3/2019 10:58:57 AM
On 1/3/19 2:58 AM, Brad Gilbert wrote:
> On Thu, Jan 3, 2019 at 12:43 AM ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>>
>> On 1/2/19 10:17 PM, ToddAndMargo via perl6-users wrote:
>>>   > On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
>>>   > <perl6-users@perl.org> wrote:
>>>   >>
>>>   >> Hi All,
>>>   >>
>>>   >> Looking at
>>>   >>
>>>   >>      https://docs.perl6.org/routine/print
>>>   >>
>>>   >> I see
>>>   >>
>>>   >>      multi sub print(**@args --> True)
>>>   >>
>>>   >> Question.  If I wanted to create my own print routine
>>>   >> using **@args, how would I declare it?
>>>   >>
>>>   >>      sub printx( **@args data ) {...}
>>>   >>
>>>   >>
>>>   >> Many thanks,
>>>   >> -T
>>>
>>>
>>> On 1/2/19 10:06 PM, Brad Gilbert wrote:
>>>> The variable name in :(**@args) is @args, it could be any valid arra=
y
>>>> identifier like @data
>>>>
>>>>       sub printx( **@data ){=E2=80=A6}
>>>>
>>>> Note that there are several forms for slurpy positional arguments.
>>>>
>>>>       :(   @a ) # not slurpy at all, takes one Positional argument a=
nd
>>>> aliases it as @a.
>>>>
>>>>       :(  *@a ) # flattening slurpy  (1,),2,3 =E2=86=92 1,2,3 and ((=
1,),2,3) =E2=86=92
>>>> 1,2,3
>>>>       :( **@a ) # structured slurpy (1,),2,3 =E2=86=92 (1,),2,3 and =
((1,),2,3) =E2=86=92
>>>> ((1,),2,3) # note there is no change
>>>>       :(  +@a ) # one-arg rule slurpy (1,),2,3 =E2=86=92 (1,),2,3 an=
d ((1,),2,3)
>>>> =E2=86=92 (1,),2,3 # note that the second one changes
>>>>
>>>> Note that itemized values do not get flattened for :(*@a), and that
>>>> Array values are itemized.
>>>>
>>>> I like to consider the one-arg rule slurpy :(+@a) to be like a
>>>> combination between non-slurpy :(@a) and structured slurpy :(**@a)
>>>> That is a one-arg rule slurpy will sometimes be like an alias to a
>>>> singular positional, otherwise it will act like a structured slurpy.=

>>>>
>>>> Note that all but the [aliasing] non-slurpy :(@a) are [almost] alway=
s
>>>> Array's.
>>>>
>>>> ---
>>>>
>>>> The one-arg rule slurpy is the oddest one of the bunch so here is so=
me
>>>> brief information:
>>>>
>>>> The one-arg rule slurpy can be sigiless :(+a), in which case it will=

>>>> be a List instead of an Array or an alias to the single argument
>>>> depending on what it was.
>>>>
>>>>       sub one-arg-rule ( +args ){
>>>>           say args.^name, " # ", args.perl;
>>>>       }
>>>>
>>>>       one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
>>>>       one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg ru=
le
>>>>       one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq #
>>>> one-arg rule
>>>>       one-arg-rule(  1..3   ); # List # (1, 2, 3)
>>>>       one-arg-rule( (1..3,) ); # List # (1..3,)
>>>>
>>>>       sub one-arg-Array ( +@args ){
>>>>           say @args.^name, " # ", @args.perl;
>>>>       }
>>>>
>>>>       one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
>>>>       one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
>>>>       one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
>>>>       one-arg-Array(  1..3   ); # Array # [1, 2, 3]
>>>>       one-arg-Array( (1..3,) ); # Array # [1..3,]
>>>>
>>>> The one-arg rule exists because people tend to write the following:
>>>>
>>>>       my @a =3D [1,2,3];
>>>>
>>>> When the correct way to write it is:
>>>>
>>>>       my @a =3D 1,2,3;
>>>>
>>>> There are various other places where the one-arg rule is also used, =
so
>>>> it was made available to everyone.
>>>>
>>>> ---
>>>>
>>>> Don't get too flustered if you don't understand all of the nuances, =
it
>>>> take everybody a while to understand them.
>>>>
>>>
>>> Thank you!
>>>
>>> The term "slurpy" did help a lot.
>>>
>>> :-)
>>>
>>> I am writing your explanation down for my records.
>>
>>
>> Well Golly!
>>
>> $ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
>> abc 1 def
>>
>> $ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
>> abc 1 def
>>
>> Question:
>> $ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'=

>> abc xyz def
>>
>> Are the spaces suppose to be there?
>=20
> The spaces are there because you are passing `@args` to `print` as a
> single value.
> `print` coerces all its values to `Str` before printing, and an Array
> adds spaces between elements when turned into a Str.
>=20
> Simple fix use `|@args` to Slip it into the call to `print`
>=20
>      $ p6 'sub printx(**@args){print( |@args, "\n" )};
> printx("abc","xyz","def");'
>      abcxyzdef
>=20

$ p6 'sub printx(**@args){print(|@args)}; printx("abc", "xyz","def\n");'
abcxyzdef

Sweet!  Thank you!

--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
1/4/2019 7:39:04 PM
On 1/4/19 11:39 AM, ToddAndMargo via perl6-users wrote:
> On 1/3/19 2:58 AM, Brad Gilbert wrote:
>> On Thu, Jan 3, 2019 at 12:43 AM ToddAndMargo via perl6-users
>> <perl6-users@perl.org> wrote:
>>>
>>> On 1/2/19 10:17 PM, ToddAndMargo via perl6-users wrote:
>>>> =C2=A0 > On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users=

>>>> =C2=A0 > <perl6-users@perl.org> wrote:
>>>> =C2=A0 >>
>>>> =C2=A0 >> Hi All,
>>>> =C2=A0 >>
>>>> =C2=A0 >> Looking at
>>>> =C2=A0 >>
>>>> =C2=A0 >>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 https://docs.perl6.org/routi=
ne/print
>>>> =C2=A0 >>
>>>> =C2=A0 >> I see
>>>> =C2=A0 >>
>>>> =C2=A0 >>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 multi sub print(**@args --> =
True)
>>>> =C2=A0 >>
>>>> =C2=A0 >> Question.=C2=A0 If I wanted to create my own print routine=

>>>> =C2=A0 >> using **@args, how would I declare it?
>>>> =C2=A0 >>
>>>> =C2=A0 >>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub printx( **@args data ) {=
=2E..}
>>>> =C2=A0 >>
>>>> =C2=A0 >>
>>>> =C2=A0 >> Many thanks,
>>>> =C2=A0 >> -T
>>>>
>>>>
>>>> On 1/2/19 10:06 PM, Brad Gilbert wrote:
>>>>> The variable name in :(**@args) is @args, it could be any valid arr=
ay
>>>>> identifier like @data
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub printx( **@data ){=E2=80=A6}
>>>>>
>>>>> Note that there are several forms for slurpy positional arguments.
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 :(=C2=A0=C2=A0 @a ) # not slurpy at =
all, takes one Positional argument and
>>>>> aliases it as @a.
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 :(=C2=A0 *@a ) # flattening slurpy=C2=
=A0 (1,),2,3 =E2=86=92 1,2,3 and ((1,),2,3) =E2=86=92
>>>>> 1,2,3
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 :( **@a ) # structured slurpy (1,),2=
,3 =E2=86=92 (1,),2,3 and=20
>>>>> ((1,),2,3) =E2=86=92
>>>>> ((1,),2,3) # note there is no change
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 :(=C2=A0 +@a ) # one-arg rule slurpy=
 (1,),2,3 =E2=86=92 (1,),2,3 and=20
>>>>> ((1,),2,3)
>>>>> =E2=86=92 (1,),2,3 # note that the second one changes
>>>>>
>>>>> Note that itemized values do not get flattened for :(*@a), and that=

>>>>> Array values are itemized.
>>>>>
>>>>> I like to consider the one-arg rule slurpy :(+@a) to be like a
>>>>> combination between non-slurpy :(@a) and structured slurpy :(**@a)
>>>>> That is a one-arg rule slurpy will sometimes be like an alias to a
>>>>> singular positional, otherwise it will act like a structured slurpy=
=2E
>>>>>
>>>>> Note that all but the [aliasing] non-slurpy :(@a) are [almost] alwa=
ys
>>>>> Array's.
>>>>>
>>>>> ---
>>>>>
>>>>> The one-arg rule slurpy is the oddest one of the bunch so here is s=
ome
>>>>> brief information:
>>>>>
>>>>> The one-arg rule slurpy can be sigiless :(+a), in which case it wil=
l
>>>>> be a List instead of an Array or an alias to the single argument
>>>>> depending on what it was.
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub one-arg-rule ( +args ){
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 say args.^na=
me, " # ", args.perl;
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule(=C2=A0 (1,),2,3=C2=A0 )=
; # List # ((1,), 2, 3)
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule( ((1,),2,3) ); # List #=
 ((1,), 2, 3) # one-arg rule
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule( ((1,),2,3).Seq ); # Se=
q # ((1,), 2, 3).Seq #
>>>>> one-arg rule
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule(=C2=A0 1..3=C2=A0=C2=A0=
 ); # List # (1, 2, 3)
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-rule( (1..3,) ); # List # (1=
=2E.3,)
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub one-arg-Array ( +@args ){
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 say @args.^n=
ame, " # ", @args.perl;
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array(=C2=A0 (1,),2,3=C2=A0 =
); # Array # [(1,), 2, 3]
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array( ((1,),2,3) ); # Array=
 # [(1,), 2, 3]
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array( ((1,),2,3).Seq ); # L=
ist # ((1,), 2, 3)
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array(=C2=A0 1..3=C2=A0=C2=A0=
 ); # Array # [1, 2, 3]
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one-arg-Array( (1..3,) ); # Array # =
[1..3,]
>>>>>
>>>>> The one-arg rule exists because people tend to write the following:=

>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 my @a =3D [1,2,3];
>>>>>
>>>>> When the correct way to write it is:
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 my @a =3D 1,2,3;
>>>>>
>>>>> There are various other places where the one-arg rule is also used,=
 so
>>>>> it was made available to everyone.
>>>>>
>>>>> ---
>>>>>
>>>>> Don't get too flustered if you don't understand all of the nuances,=
 it
>>>>> take everybody a while to understand them.
>>>>>
>>>>
>>>> Thank you!
>>>>
>>>> The term "slurpy" did help a lot.
>>>>
>>>> :-)
>>>>
>>>> I am writing your explanation down for my records.
>>>
>>>
>>> Well Golly!
>>>
>>> $ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
>>> abc 1 def
>>>
>>> $ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'=

>>> abc 1 def
>>>
>>> Question:
>>> $ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");=
'
>>> abc xyz def
>>>
>>> Are the spaces suppose to be there?
>>
>> The spaces are there because you are passing `@args` to `print` as a
>> single value.
>> `print` coerces all its values to `Str` before printing, and an Array
>> adds spaces between elements when turned into a Str.
>>
>> Simple fix use `|@args` to Slip it into the call to `print`
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 $ p6 'sub printx(**@args){print( |@args, "\n"=
 )};
>> printx("abc","xyz","def");'
>> =C2=A0=C2=A0=C2=A0=C2=A0 abcxyzdef
>>
>=20
> $ p6 'sub printx(**@args){print(|@args)}; printx("abc", "xyz","def\n");=
'
> abcxyzdef
>=20
> Sweet!=C2=A0 Thank you!
>=20

Follow up:

<PrintColors.pm6>
unit module PrintColors;

#`{

      There subs give you the ability to print to the stand output and=20
standard error
      with color.

      To use these, place the following at the top(ish) of your program
         use lib "/home/linuxutil";
         use PrintColors;  # qx[ PrintRed  PrintGreen PrintBlue PrintErr =

PrintRedErr PrintGreenErr PrintBlueErr ]

}

use Terminal::ANSIColor;  # qx[ color ];

sub PrintRed   ( **@args ) is export { print color('bold'),=20
color('red'),   |@args, color('reset'); }
sub PrintGreen ( **@args ) is export { print color('bold'),=20
color('green'), |@args, color('reset'); }
sub PrintBlue  ( **@args ) is export { print color('bold'),=20
color('blue'),  |@args, color('reset'); }

sub PrintErr   ( **@args ) is export { $*ERR.print: |@args; }

sub PrintRedErr   ( **@args ) is export { $*ERR.print: color('bold'),=20
color('red'),   |@args, color('reset'); }
sub PrintGreenErr ( **@args ) is export { $*ERR.print: color('bold'),=20
color('green'), |@args, color('reset'); }
sub PrintBlueErr  ( **@args ) is export { $*ERR.print: color('bold'),=20
color('blue'),  |@args, color('reset'); }

# $ perl6 -I. -MPrintColors -e 'PrintRedTest( "abc", 1, "def", "\n" );'
sub PrintRedTest( **@args ) is export { print color('bold'),=20
color('red'), |@args, color('reset'); }
</PrintColors.pm6>
0
perl6
1/4/2019 7:50:06 PM
On 1/3/19 12:43 AM, Richard Hainsworth wrote:
> <snip>
>>>
>>> The term "slurpy" did help a lot.
>>>
>>> :-)
>>>
>>> I am writing your explanation down for my records.
>>
>>
>> Well Golly!
>>
>> $ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
>> abc 1 def
>>
>> $ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
>> abc 1 def
>>
>> Question:
>> $ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'
>> abc xyz def
>>
>> Are the spaces suppose to be there?
> 
> In each case you are using 'print @args', then `printx` adds on a "\n" 
> either to the last item or the final string, which in each case results 
> in exactly the same string. The way 'print' handles an array is to put a 
> space between each item, which can be very confusing when dealing with 
> items that are themselves strings with spaces.
> 
> For other "stringifications" of an array, try 'print @args.fmt("%s,")' 
> if you want a comma, or 'print @args.perl'.
> 
> I find '.say for @args' is better because I get an item on each line.
> 
> Also 'dd @args' is quite useful.


Thank you!
0
perl6
1/4/2019 7:50:45 PM
Reply: