in reply to Adjusting variable context when workign with JSON data?
In particular, the syntax for my $workspace (@$decoded) seems really weird to me.
It does not seem weird to me, but for me it is very normal to choose to work with references rather than real arrays. In particular for function calls, it is much more efficient for a function to return a reference than to return a full list (of arbitrary length) to the caller.
It takes some getting used to, the core concept is that when declaring a variable the sigil specifies the type of container, but when accessing a variable the sigil specifies the type of structure we are asking for. Thus:
my @a = (1 .. 5); say $a[0]; # '$' sigil, we are picking out a scalar say @a[3,1,2]; # '@' sigil, we are picking out a list (an array-like + structure) my %h = (a => 1, b => 2, c => 3); say $h{a}; # '$' sigil, we are picking out a scalar say @h{qw{b c}}; # '@' sigil, we are picking out a list say %h{qw{a c}}; # '%' sigil, we are picking out a hash slice (a hash- +like structure)
With references it is similar, except that the reference itself is always a scalar that needs dereferencing
my $aref = [ 1 .. 5 ]; say $aref->[0]; # or ${$aref}[1] say @{$aref}[3,1,2]; my $href = { a => 1, b => 2, c => 3 }; say $href->{a}; # or ${$href}{a} say @{$href}{qw{b c}}; say %{$href}{qw{a c}};
Given this, it is natural that given an array reference $aref we would reference the whole array as @$aref, and given a hash reference $href we would reference the whole hash as %$href.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adjusting variable context when workign with JSON data?
by unmatched (Sexton) on Dec 04, 2024 at 17:21 UTC |