3 ms·
func %h; sub func { my %h = @_; Note that the above only works if %h is the only parameter to the function, because your solution swallows the entire par
by AceJohnny2 4y ago
func %h;
sub func {
my %h = @_;
Note that the above only works if %h is the only parameter to the function, because your solution swallows the entire parameter array. Otherwise you must pass a hashref.
In any case, these are examples of my discontent with Perl's syntax, which gets unreasonably complicated for non-trivial datastructures. To be clear, Perl's references, the \@a or \%h with the backslashes, are what I derisively call "pointers", because they introduce the same kind of complexity.
It's a problem when you must use different syntaxes to update/refer to a hash, depending on the scope you're using it:
$hash{$key}
vs
$hashref->{$key}
The first syntax is if you have direct access to the %hash, such as the scope that created it. The second syntax is whenever you're forced to pass the %hash as a ref, for example because it's one of many parameters to a function. (And the hashref syntax I used is sugar! The basic syntax, demonstrated with the "hash-in-array" example, is worse)
It's a problem when you have to "cast" a type, like in `keys %{$a[0]}` (for the audience: where $a[0] refers to a hashref inside an array, which has to be dereferenced via the %{} syntax, to be used as a hash). This is a simple example, but replace $a[0] with a more sophisticated datastructure, and things get exponentially unwieldy.
The above make it harder to keep track of data as you're browsing code.
In contrast, Python's everything-is-call-by-reference approach means that, whatever the scope, you can refer to something the same way. There never is differing syntax between 'something' and 'a reference to something'.