1

The documentation of File::Modified says:

my $d = File::Modified->new(files=>['Import.cfg','Export.cfg']);

The files parameter looks to me as being an array.

Why can I not directly hand over an array?

my $d = File::Modified->new(files=>@array);

This creates a runtime error.

toolic
  • 57,801
  • 17
  • 75
  • 117
averlon
  • 325
  • 3
  • 14
  • Does this answer your question? [Passing array to subroutine Perl](https://stackoverflow.com/questions/72380485/passing-array-to-subroutine-perl) – tobyink Aug 30 '23 at 12:58
  • `files=>@array` would results in `files=>'Import.cfg', 'Export.cfg'=>undef` – ikegami Aug 30 '23 at 13:39

1 Answers1

3

The files parameter is not an array; it is a reference to an array.
The File::Modified POD says:

Files, which takes an array reference to the filenames to watch.

This is why you can not simply pass an array variable like that.

The square brackets create a reference to an array, which is different from an array.

As the perlref document shows, one way to take a reference to an array is to use the backslash:

my $d = File::Modified->new(files => \@array);
toolic
  • 57,801
  • 17
  • 75
  • 117