in reply to Pass array, then clear

Inside clear_it $data is a local variable that you assign a reference to from the parm stack. the uncommented version then assigns a reference to a empty hash to that local variable. As the subroutine exits the internal $data variable containing the reference is destroyed.
The commented version uses the reference contained in $data as an address to an array and assigns the empty array to that reference

Replies are listed 'Best First'.
Re^2: Pass array, then clear
by Anonymous Monk on Jan 15, 2018 at 22:09 UTC
    Thank you for the explanation. Do you think this is intuitive behavior when dealing with references? I'm kind of bummed about it. I'm concerned that this behavior deserves an explanation somewhere. I'm sure it's in the documentation somewhere, but I did not see anything about this in the Perl references documentation.

    Maybe everyone thinks the current behavior is perfect, in that case I need to adjust my thinking.
      Do you think this is intuitive behavior ...? I'm concerned that this behavior deserves an explanation somewhere.

      No, it's not intuitive; however, this is pretty much the way references work in any computer language. Maybe see reference, perlref, perlreftut. Perhaps also take a look at the Monastery Tutorials articles in References, and maybe also at Coping with Scoping and other articles in Variables and Scoping.

      (And yes, you need to adjust your thinking :) (Update: And yes, references and aliasing have bitten me, too — many times, many, many times...)


      Give a man a fish:  <%-{-{-{-<

      I will admit i have gotten bit by it before, but as soon as i realize i know what to fix. It is the same when

      for my $var (@array) { $var++; }
      ends up modifying the array even tho i said my $var.