0

In some Perl code I have been asked to maintain, I encountered the following construct:

myArray => ['id1', 'id2', 'id3', 'id4']

I searched the web for some definition/explanation of this but all I could find was a reference to scalar hashing:

https://www.guru99.com/perl-tutorials.html

This is a snippet from the actual code I encountered:

$config = eval {
    XMLin(
        $inFile,
        MyArray => [
            'id1', 'id2',"id3", 'id4'
        ]
    );
};

What does that syntax of hashing an entire array (without the hash values) mean?

melpomene
  • 84,125
  • 8
  • 85
  • 148
datsb
  • 163
  • 9
  • duplicate https://stackoverflow.com/questions/4093895/how-does-double-arrow-operator-work-in-perl but welcome to SO – KeepCalmAndCarryOn Jul 07 '19 at 10:32
  • @KeepCalmAndCarryOn Thank you. I will check that link that you provided (I did search SO before I posted but couldn't find it). Maybe this is not an exact duplicate because I was referring to this confusing array declaration onlly. – datsb Jul 07 '19 at 10:38
  • 1
    The `[ ]` brackets create an array ref: https://stackoverflow.com/a/50155543/14955 – Thilo Jul 07 '19 at 10:44
  • 1
    What do you mean by "hashing an entire array without the hash values"? The array is the value. – melpomene Jul 07 '19 at 10:46
  • 1
    By the way, you should avoid XML::Simple. Despite its name, [it's so complicated to use](https://stackoverflow.com/q/33267765/589924) that its own documentation recommends avoiding it. – ikegami Jul 07 '19 at 11:59

1 Answers1

5

You mention hashing and hashes, but neither are involved.


IDENT => ...

is the same as

'IDENT', ...

[ ... ]

is basically equivalent to

do { my @anon = ( ... ); \@anon }

except without the new scope.


XMLin(
    $inFile,
    MyArray => [
        'id1', 'id2',"id3", 'id4'
    ]
);

is equivalent to

my @anon = ( 'id1', 'id2', 'id3', 'id4' );
XMLin($inFile, 'MyArray', \@anon);

This isn't a proper call to XMLin. If present, the second argument should be the name of one of the options (e.g. ForceArray.)

ikegami
  • 367,544
  • 15
  • 269
  • 518