This solution is less good than simply assigning the value to each variable separately in my $a = my $b = my $c = 'TRUE' because it avoids having to count how many variables you have. In your code, if you got the number wrong then the trailing variables will be left undefined which is far and away from the intent to leave them with a true value. You're making it easy to write in a bug later. | [reply] [Watch: Dir/Any] [d/l] |
use Want 'howmany';
my ($a, $b, $c) = &{ sub{ (1) x howmany } };
Not sure if that's shorter/better than other solutions (plus requires Want), but it's another way of keeping the number of variables in sync.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [Watch: Dir/Any] [d/l] |
Jeepers! the 'x' operator works on lists too!?
Looks like time to refactor out all ...
my ($a,$b,$c) = map {'TRUE'}(0 .. 2);
| [reply] [Watch: Dir/Any] [d/l] |
$,= " "; $\ = "\n";
print( map { int rand 10 } 0 .. 6 );
print( ( int rand 10 ) x 7 );
The x operator only evaluates the list expression once. So you can't always substitute one for the other.
Makeshifts last the longest. | [reply] [Watch: Dir/Any] [d/l] |
scooterm,
Not only that, but if the what the list contains is a reference, the reference is replicated as well. The first useful application I remember seeing of this is in tye's Algorithm::Loops. I found a useful variation that looked a little something like:
my @signal = qw(ABRT STOP TERM INT);
@SIG{ @signal } = (sub { kill 9, $pid; exit }) x @signal;
Incidently, diotalevi makes a good point concerning ensuring that you have the same number of things on both sides of the assignment which is not an issue in my example.
| [reply] [Watch: Dir/Any] [d/l] |