I think the issue is that you aren’t passing the array to the subroutine,  
you’re passing a new flattened list of aliases to the elements of the array.

If you modify a parameter (`$_[3] = ‘xyzzy’;), it changes at the top level,  
but that’s because you’re modifying the aliases parameter.

 From `perlsub`:

> The array @_ is a local array, but its elements are aliases for the  
> actual scalar parameters.

So, you create an array “@list”, which has a bunch of elements. You pass  
that array to the killer functions, but Perl flattens the array, and  
instead of passing “@list”, it passes @_ = ( $list[0], $list[1], $list[2],  
$list[3], …). When you modify @_, you’re not modifying @list, just @_. If  
you change $_[2], you change the aliases item, which changes the value that  
$list[2] points to, so you can change it that way, but you can’t change  
@list itself.

If you need to modify @list, you need to pass the sub a reference to @list  
— `killer(\@list)` — similar to why you had to escape @list to pass it to  
`Dumper`. You would then be able to delete an item with  
`delete($_[0]->[2])`.

HTH,
Ricky

On Dec 19, 2019, at 3:53 PM, Greg London <em...@greglondon.com> wrote:

>         External Email - Use Caution
>
> use Data::Dumper;
>
> sub killer{
>       delete($_[2]);
> }
>
> my @list = qw( alpha bravo charlie delta echo fox);
> print Dumper \@list;
> killer(@list);
> print Dumper \@list;
>
>
> I would expect the second Dumper statement to show one less element.
> Greg
>
> _______________________________________________
> Boston-pm mailing list
> Boston-pm@pm.org
> https://mail.pm.org/mailman/listinfo/boston-pm

_______________________________________________
Boston-pm mailing list
Boston-pm@pm.org
https://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to