List repetition (xx) without evaluation?

  • A+
Category:Languages

The list repetition operator (xx) evaluates the list every time it is repeated. For example,

my @input = get() xx 5; 

will evaluate to the first 5 lines of STDIN. Is there any way I can repeat just the value of the element 5 times, rather than evaluating it each time? Currently, I've been assigning it to a variable, then repeating it, but it seems a bit cumbersome that way.

my $firstLine = get(); my @firstlineRepeated = $firstLine xx 5; 

Is there a prefix or something that lets me do it in one statement?

 


Using given to contextualize it into $_ is one fairly neat way:

my @input = ($_ xx 5 given get()); say @input; 

That, when I type hello, gives:

[hello hello hello hello hello] 

Since given simply contextualizes, rather than doing any kind of definedness or truth test, it's a bit safer as a general pattern than andthen.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: