develooper Front page | perl.perl6.language | Postings from March 2005

Re: A possible solution for s?pintf

Thread Previous | Thread Next
Rod Adams
March 12, 2005 20:26
Re: A possible solution for s?pintf
Message ID:
Matt Diephouse wrote:

>Brent 'Dax' Royal-Gordon <> wrote:
>>Besides, I think "as" will do just fine, especially since you can now
>>interpolate method calls as well.  You can even do something like this
>>if you want to perform bulk formatting:
>>    say join ' ', ($n1, $n2, $n3) >>.as('%d');
>What about:
>  say [ $n1, $n2, $n3 >>.as('%d') ].join;
>Can I (1) use join on a bracketed list 
Certainly. Almost anywhere you can use an array, an array ref will do 

>and (2) leave off the
>parentheses around C<$n1, $n2, $n3>? (I couldn't find where hyper ops
>are on the precedence table.)
I think you'd need the parens there, to distinguish that the . operator 
applies to the list, not to $n3. Hyper's are not on the list because 
they are adverbs to the existing operators. In this case, you're using 
unary ., so you follow it's precedence.

You could easily write the above as

    say (($n1, $n2, $n3)».as('%d')).join;

What I'm not certain about is if

    say ($n1, $n2, $n3)».as('%d').join;

does the same thing, but I think it does.

>And this:
>  say [ $num => '%d', $str => '%s' ] >>;
No. .key returns a string, which you call the .as method of, which is 
fine, but the .value is a separate expression, and references the 
current topic, which is not tied to the array.

Related to what I'm not sure about above is that I think you'd have to 
say C< [...]».key».as($pattern) > to get it working correctly.

>(4) use square brackets in
>this instance (to make sure my list doesn't form a parameter list to
Yes, but whitespace between the "say" and "(" should do the trick as 
well. Might generate a warning, however.

-- Rod Adams

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About