Reputation: 37146
Why doesn't the following script work?
use strict;
use warnings;
use Term::ANSIColor ':constants';
my @colors = ( BLUE, GREEN );
my $select;
my @data = 1 .. 10;
print { $colors[$select++ % @colors] } $_, "\n" for @data;
outputs:
Can't use string ("") as a symbol ref while "strict refs" in use at - line 9.
Upvotes: 3
Views: 522
Reputation: 74685
This seems to do what you want:
print @{[ $colors[$select++ % @colors] ]}, $_, "\n" for @data
I will hazard a guess at an explanation: the @{[ ]}
interpolates the value of a function. I've used them before in a HEREDOC.
Upvotes: 0
Reputation: 57640
You are using the print { $fh } @strings
syntax (documented here). The thing in curly braces (which are actually optional in simple cases) is interpreted as a file handle. Here, you pass a string instead of the file handle object, so Perl looks for a global variable with the name of the string. Unfortunately, this string contains funky command sequences (which happen to be unprintable) instead of some usable variable name.
Solution: don't use that weird syntax, and just do
my $select = 0;
print $colors[$select++ % @colors], $_, "\n" for 1 .. 10;
Upvotes: 4