3 ms·
In Perl this would be way more complicated Does it need to be more complicated, or are you perhaps trying to make the code look like Python? While there are g
by nkurz 17y ago
In Perl this would be way more complicated
Does it need to be more complicated, or are you perhaps trying to make the code look like Python? While there are good reasons for using references to pass lists to functions, it seems clearer (and more Perl-ish) to do the transformations on a list rather than a reference:
my @xs = (5, 6); # use a list directly
push @xs, 7;
print $xs[2];
push @xs, [];
push @{$xs[3]}, 6; # treat $xs[3] as array
some_func(12, "hello", \@xs, 1);
While one could probably argue that pushing to the inner array is clearer in Python, the rest seems just as clear. And if one was writing for an audience not comfortable with references, one could always use a temp variable for clarity:
my @inner;
push @xs, \@inner;
push @inner, 8;
For that matter, you could do the same for your 'omgwtf' line:
my $inner = $xs->[3];
push @$inner, 9;
While there's nothing wrong with the Python, they strike me as pretty comparable in clarity.
- Goladus 17y agoThe point of his example was to show why reference syntax is a pain. "# Use a list directly" is not what happens in a lot of code that I have to read.