Lexicals are by definition private to the lexical block, marked by { curlies }. You can rely on that they will be destroyed after leaving the block! This "hiddeness" is a quality (!) package-vars do not have. (*)
(Note: The biggest possible block is the containing file, and you could prolong the lifetime by returning references - thats also a matter of garbage collection and closures and ... getting to far)
Package variables OTH have the quality to be as global as possible, if you know the packagename you can access them from everywhere in the program, any file.
So lexicals can't be in the stash associated to a package!!! If you want to inspect them you need to hack with moduls like padwalker.
Saying that lexicals are "much younger", explains some inconsequences / inorthogonalities of perls syntax.
e.g. you can (without extra non-core moduls) only alias a packagevar $a directly with globs * eg *a=$b but not lexical vars ...
... which leads to the problem that you end using hashrefs and arrayrefs as returnvalues
... which as a consequence complicate the sourcecode cluttered with dereferencing syntax
my ($arr_ref)=func(); push @{ $arr_ref->[1][2] }, "elem"; # hate this @{}
... sigh!!! O Perl 6! Where art thou!!!
Cheers LanX
(*) neither Python AFAIK
MENMONIC: because packagevars are as global as possible they are ours, meanwhile lexicals belong privately to the block/sub, they are "my-ne". ; )
In reply to Lexicals vs package vars
by LanX
in thread Indirect variable name
by FreakyGreenLeaky
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |