B
bjlockie
I'm trying to pass a reference to an array so I can modify it in a function and see the changes outside the function.
I tried:
#!/bin/perl
use strict;
use warnings;
my @myarray = ();
&loadFile( '/etc/resolv.conf', \@myarray );
print "Loaded $#myarray\n";
sub loadFile
{
my $filename = $_[0];
# input as a reference
my (@inputArray) = @{$_[1]};
open( FILE, "<$filename" ) or die "Can't open file ($filename): $!\n";
@inputArray = <FILE>;
close( FILE );
print "Loaded $#inputArray from $filename\n";
}
I tried:
#!/bin/perl
use strict;
use warnings;
my @myarray = ();
&loadFile( '/etc/resolv.conf', \@myarray );
print "Loaded $#myarray\n";
sub loadFile
{
my $filename = $_[0];
# input as a reference
my (@inputArray) = @{$_[1]};
open( FILE, "<$filename" ) or die "Can't open file ($filename): $!\n";
@inputArray = <FILE>;
close( FILE );
print "Loaded $#inputArray from $filename\n";
}