in reply to Do subroutine variables get destroyed?

Everything in perl is on the heap (*). Variables are created when necessary (my variables are created when the my statement executes), and cleaned up as soon as there are no references left to them.

This allows you to create new hashes, arrays, etc., in your sub, and then return references to them and have them still work.

Compared to C, this is basically perl calling malloc for you for each variable declaration, and then calling free automatically not when the variable goes out of scope, but when there are no more references to that variable.

(*) Not quite true. But close enough for our purposes.

  • Comment on Re: Do subroutine variables get destroyed?