DEV Community

Discussion on: JavaScript Destructs

Collapse
 
strredwolf profile image
STrRedWolf

On other words, EMCA stole from Perl, stretched one idea out, and made another an explicit option.

my @list = ("A","B","C","D");  # Simple array
my ($a,$b,@rest) = @list;
print "$a and $b.\n";  # "A and B."
print (join " ", @rest),"\n";  # "C D"

my @longerlist = (@list,"E","F","G");  # Perl flattens out arrays.
my ($lla, @ll) = @longerlist;
print "$lla but not ",@ll,"\n"; # "A but not BCDEFG"
Enter fullscreen mode Exit fullscreen mode

The first example, I'm able to pull two values out and save the rest for later. This is used with subroutines in Perl, where options are put in a localized @_ array. With EMCAScript, that will need the "..." prefix to tell it explicitly to save the rest of the objects in the array/hash.

The second example makes use of Perl's "always flatten arrays" policy. EMCAScript will need the "..." prefix.

The only difference is that EMCAScript allows nesting. I think with Perl you will need to unroll each layer to get your variable.

Collapse
 
kayis profile image
K

Interesting. I never used Perl :)