For no particular reason other than a lazy desire to type less, I want to design a routine that modifies an array in place. The following code is close, but no cigar!!
#!/usr/bin/perl -w
# test.pl --
use strict;
use warnings;
use diagnostics;
my @a = (
'one',
'two',
'three'
);
print join("\n",@a),"\n__________\n";
setmode('add_test',@a);
print join("\n",@a);
sub setmode {
my $val = shift(@_);
my @a = @_;
splice(@a,1,0,$val);
$_[$_] = $a[$_] for (0..@a);
}
Which produces:
C:>test
one
two
three
__________
one
add_test
two
Obviously @_ is an array of aliases and that portion of the direct hands on works as expected. And I've no expectation that there would be anyway to effect what I want short of references. But since TIMTOWTDI, I thought I'd ask if anyone knew a method that would allow me to have my cake and eat it too. Clearly this would work:
sub setmode {
my($val,$ref) = @_;
splice(@$ref,1,0,$val);
}
but I was looking for a little magic here...
--hsm
"Never try to teach a pig to sing...it wastes your time and it annoys the pig."