News for hackers. Stuff that matters.
Re:On the Decline of Perl – external factors by arunix on Thursday April 01, 2021 @07:07AM (#26658904)
What does "really obfuscated handling of nested data structures" mean? Can you give an example?
Re:On the Decline of Perl – external factors by tyingq on Thursday April 01, 2021 @11:15AM (#26661482)
Perl has sigils for different data types.

  $foo="bar"; #scalar/string
  %hash = { "key" => "value", "key2" => "val2" }; #hashmap
  @arr = ( 1, 2, "three", 4); #array
But, it also has references, like pointers.

  $array_ref=\@somearray; # array ref
  $hash_ref=\%somehash; # hash ref
  $scalar_ref=\$some_scalar; # scalar ref
So, you can put scalars into arrays and hashes, refs into hashes and arrays, etc, etc. At whatever nested depth you want. But you have to extract pieces later. And Perl, as usual, allows a zillion ways to do it. These are all exactly the same thing:

  $$arrayref[0] = "data"; 
  ${$arrayref}[0] = "data"; 
  $arrayref->[0] = "data";
So imagine you need to extract a string that's at the bottom of some data structure. Here's an example:

  @IP = ('192.168.1.10','192.168.1.15'); 
  @PORT = ("5000","5002"); 
  @TCP = ("Q931","H225","H245"); 
  @LAYER = ("ETHERNET","IP",\@TCP); 
  @PKT = (
         \@IP,
         \@PORT,
         \@LAYER
        );
  $array_ref = \@PKT;
I can extract something using different approaches, like:

  ${${${$array_ref}[2]}[2]}[1];# oof!
  
  # same result, looks better, but imagine 
  # we put a hash in the data structure, or
  # had deeper structures

  $array_ref->[2]->[2]->[1];
Re:On the Decline of Perl – external factors by tripa on Thursday April 01, 2021 @12:05PM (#26662310)
Or just the simple

    $array_ref->[2][2][1];
Re:On the Decline of Perl – external factors by tyingq on Thursday April 01, 2021 @12:14PM (#26662453)
Yes, perhaps I shouldn't have gone with just nested arrays/arrayrefs :)

Here's a modified example where it's not as clear how to deref.

Like:

  @IP = ('192.168.1.10','192.168.1.15');
  @PORT = ("5000","5002");
  @TCP = ("Q931","H225","H245");
  %TAGS = (L2 =>"ETHERNET",L3=>"IP",L4=>\@TCP);
  @PKT = ( \@IP, \@PORT, \%TAGS);
  $array_ref=\@PKT;
  print $array_ref->[2]{L4}[0];# this works
  print $$array_ref[2]{L4}[0]; # this works
  print $array_ref[2]{L4}[0];  # this doesn't
Re:On the Decline of Perl – external factors by luckylion on Thursday April 01, 2021 @07:37AM (#26659151)
Probably dereferencing, since Perl never had multidimensional datastructures. You'd put a reference to an array into an array value, and then access it with $array[0]->[0] (iirc) to get the first value of the first array. That was the easy way to write it, the much more annoying way was like ${$array[0]}[0] (or $$array[0][0] if you really wanted to? my memory is fortunately fading), I believe, which got really out of hand when you went three or more levels deep.
Re:On the Decline of Perl – external factors by kqr on Thursday April 01, 2021 @07:57AM (#26659332)
The syntax for accessing nested indices actually automatically resolves references the way you'd expect. So for the specific case of accessing a single deeply nested value, no special incantation is needed.