I have a function where I want to check if a path exists, and if it does, add that to an array. Here's what I tried:
# If a path exists, adds the canonical version of that path to an array
sub AddPathCandidate {
my (@$target, $path) = $_;
die ('path needed') unless defined($path);
$path = File::Spec->canonpath($path);
if (-e $path) {
push(@{$target}, $path);
}
}
where the caller looks like:
my @exampleDirs = ();
AddPathCandidate(\@exampleDirs, $inDir . 'a');
AddPathCandidate(\@exampleDirs, $inDir . "../b/a/$arch");
AddPathCandidate(\@exampleDirs, $inDir . "../../b/a/$arch");
But the "die" statement always executes; the second parameter of AddPathCandidate isn't getting through somehow.
Is what I'm trying to do here even possible or is there some more "perl-ish way" to accomplish this?
Declare the variable as $target
when unpacking your arguments, and unpack from @_
:
my ($target, $path) = @_;
^ ^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.