开发者

What happens to private vars at the end of a sub routine?

开发者 https://www.devze.com 2023-01-27 01:22 出处:网络
I have a script that reads of a global config file using Config::Tiny. Based on selection variable my script chooses a sub routine for either SQLite, MySQL or PgSQL.

I have a script that reads of a global config file using Config::Tiny. Based on selection variable my script chooses a sub routine for either SQLite, MySQL or PgSQL.

Right now the config file is read at the top of th开发者_如何学JAVAe script and variables are set outside of the sub routines. But considering the MySQL vars have nothing to do with PgSQL or SQLite I wanted to set private vars in the sub routines. The only thing I am wondering is what happens to the vars once the sub routine has ran its course? Do they get removed?


Since Perl 5 has no such thing as "private vars", I am going to assume you mean lexical variables declared with the my function. At the end of the scope in which a lexical variable was declared, the reference count for that chunk of memory is decremented. If this means the reference count goes to zero, then it gets garbage collected. Since functions provide scope, then so long as you are not passing references to those variables to something that holds on to them, or using them in a closure, then they should be garbage collected.


Variables declared in subroutines will not exist after the subroutine returns. Their memory will be released, and any referenced values will be eligible for garbage collection (assuming that nothing else references those values).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号