in reply to Help understanding perltooc examples?
the *$datam = sub{#code} part is new to me. This is a data-type glob correct?
Actually, this question has nothing to do with OO.
Yes, you are assigning to what is called a "typeglob", which is sort of a record of all datatypes associated with the name of a global variable (thus $a, @a, %a, ... — all globals, of course, lexicals are different) and yes, that part is magical in that only the slot in the typeglob is affected that agrees with the type of reference that you are assigning to it. In other words, if you assign a scalar reference, only the scalar part of the typeglob will be affected, and array, hash, filehandle... all will keep their old value.
The best explanation that I recall having read, is in the O'Reilly book "Advanced Perl Programming", p.47. The author calls it "selective aliasing".
Witness:
Result:@a = qw(a b c); $foo = "hello"; *a = \$foo; print <<"END"; array: @a scalar: $a END
array: a b c scalar: hello
Not only that, but it's also a way to create variable's aliases:
Result:$foo = "hello"; *a = \$foo; $a = "bye"; print "$foo\n";
byeSo I changed the value of the alias $a, and the original variable $foo changes with it. Both $a and $foo actually refer to the same variable internally.
That would also work if $foo is a lexical (my) variable.
|
|---|