3 ms·
A slightly bizarre version of this that nobody should ever use. If you see someone doing this, tell them they are a bad programmer and shouldn't be putting cod
by simcop2387 8y ago
A slightly bizarre version of this that nobody should ever use. If you see someone doing this, tell them they are a bad programmer and shouldn't be putting code into production. It's also unlikely that this would actually be useful for the real code in the authors situation because it ends up mostly limited, but the technique can be useful even if this example shouldn't be done this way.
# declare our arrays
my @arr1;
my @arr2;
# alias specific elements to the arrays
my \$first = \$arr1[0];
my \$second = \$arr1[1];
my \$third = \$arr2[0];
my \$fourth = \$arr2[1];
# assign to those aliases. note the , 2) added to each split. This forces each split to only ever produce two values.
($first, $second, $third, $fourth) =
(index($str2, '-') != -1) ?
(split(/-/,$str2,2), split(/;/, $str4, 2)) :
(split(/:/,$str2,2), split(/;/, $str4, 2));
This is bad code simply because you're unrolling the arrays and not using things the way you should in the language. But this does demonstrate how refaliasing can be useful for doing some other weirdly specific things. This also works for things inside hashes and other perl data structures.