Re: Is typeglob feature really useful? (typeglob references)
by eyepopslikeamosquito (Archbishop) on Jan 10, 2021 at 02:59 UTC
|
Please use typeglob not glob in future to avoid confusion with the glob function.
Perl typeglobs have a notoriously obscure syntax. They are rarely used in high-level user code.
They are often used in low level system code and when building tools.
Some background and references:
Though I've used typeglobs occasionally, for example in an automated testing hack to verify a file being used by a module contained the expected content, their syntax always does my head in. That I'm no typeglob expert is proved by not finding Ton's famous winning typeglob hack, despite hacking away for a whole week in Christmas 2006. :)
References added later:
- The Symbol Table Describes Itself: examples comparing Perl, Raku and Python (especially task 2). Note that typeglobs do not exist in Raku because the sigil is part of the name stored in a symbol table, while in Perl 5 the name is stored without the sigil.
- Test::More - this core module uses a cute typeglob alias (sub isnt { ... } *isn't = \&isnt) allowing you to write isn't in your tests, as an alternative to isnt
- Dollar Bracket Star ${*...} by snoopyjc (2021)
Updated: Changed title from glob to typeglob in step with xiaoyafeng. Added more examples of typeglobs long after original reply was made.
| [reply] [d/l] [select] |
|
Thanks for your reply and sharing. I realize that what I post is not saying typeglob is bad feature, but the current implementation for it decrese performance. maybe that's why raku abandon this feature?
I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
| [reply] |
|
I don't know why typeglobs were removed from raku and
my google-fu wasn't good enough to find a clear explanation of Larry's rationale for this.
However, I did find How naming of variables works in Perl 6 by Elizabeth Mattijsen which states:
As you may have noticed, Perl 6 does not have a * sigil nor the concept of typeglobs.
If you don't know what typeglobs are, you don't have to worry about this.
In Perl 6, the sigil is part of the name stored in a symbol table, whereas in Perl 5 the name is stored without the sigil.
For example, in Perl 5, if you reference $foo in your program, the compiler will look up foo (without sigil),
then fetch the associated information (which is an array), and look up what it needs at the index for the $ sigil.
In Perl 6, if you reference $foo, the compiler will look up $foo and directly use the information associated with that key.
Please do not confuse the * used to indicate slurpiness of parameters in Perl 6 with the typeglob sigil in Perl 5 --
they have nothing to do with each other.
To make any sort of case for their removal from Perl 5, you'll need to do more than assert that
"the current implementation for it decrease performance".
You'll need to prove it by publishing some benchmarks.
Given the backward compatibility nightmare that would be unleashed if typeglobs were removed from Perl 5,
it seems preferable to me to leave them in the language and instead try to improve their performance.
Admittedly, that's not for me to say, I don't have the skills or desire to be a Perl 5 pumpkin.
If you feel strongly enough about it, I suggest you publish some benchmarks and discuss on P5P.
| [reply] |
Re: Is glob feature really useful?
by LanX (Saint) on Jan 09, 2021 at 18:32 UTC
|
(I moved this to meditations)
First of all your wording is dubious, (type)globs are only used with our package variables.
Private variables with my don't have any type-globs. (which leads to some break in orthogonality)
The origin of type-globs (i.e. "things" with sigil * ) lies IMHO in Lisp, which has similar Symbol Tables.
Obviously it's possible to have separated name-spaces with sigils.
So are you asking to abandon sigils?
It's true that many think that using the same name aka symbol for different types is bad style.
But I see no way to stop using sigils in Perl, since all the context mechanics would stop working.
Any changes on that level would lead to a Perl6'ish compatibility mess (well probably even worse).
And * is needed for exporting functions into other namespaces.
HTH! :)
PS: Though I could imagine a pragma which automatically declares $xxx = \@xxx after a my @xxx (analogous for $yyy = \%yyy ), this would facilitate reference mechanics a lot.
This pragma would automatically forbid name reusing (mostly) and referencing and dereferencing with \ and -> would become mostly unnecessary.
update
I forgot to mention, every fancy feature comes with a price. In languages like JS or Python name-collisions are a quite common trap.
For instance if you assign to a variable list when the function/method list already exists.
And equally named nouns and verbs are quite common in English due to it's simplicity of grammar.° (e.g. compare noun Liste vs verb listen in German)
This is never a problem in Perl! $list will never collide with &list ....
°) a funnier example Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo | [reply] [d/l] [select] |
|
So are you asking to abandon sigils?
ehh no, sigil is the spirit of perl. no sigils no perl. what I mean is perl allows a same name but different type. and It's also a good feature but perl sacrificed much performance for it.
for support this feature, perl have to create a 6elments hash to store one name. see below:
like a package scalar variable: $FOO = '123';
actually store:
*Foo{SCALAR => '123', HASH => '', ARRAY =>'', GLOB => '', PACKAGE => '
+', NAME =>''},
If we discard above feature, we could store a scalar on STASH directly. To me it's more understandable and maybe faster.
I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
| [reply] [d/l] |
|
| [reply] |
Re: Is typeglob feature really useful?
by Tux (Canon) on Jan 10, 2021 at 13:42 UTC
|
I use it all the time. I know (some) people think it is bad practice, but in my mind it fits. It nicely groups thing that are equal. $cow is the current cow to work with @cow is the heard of available cows and %cow is nicely collecting the properties of each cow, so $cow{$cow}{weight} makes perfect sense to me. YMMV.
I sometimes doubt to use @cows (which works well in foreach my $cow (@cows) {) or @cow (which works better with milk ($cow[$_]) for 2, 5, 7..9;).
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
Re: Is glob feature really useful?
by shmem (Chancellor) on Jan 09, 2021 at 19:32 UTC
|
we all know, Perl support same name but different type, that means $foo and @foo could be both exist without any violation.
It is singular, plural and named items. Remeber, perl was invented by a linguist. Identifiers are stems from which substantives (singular and plural) and verbs can be derived, etc.
Same as in English e.g. "cow" and "cows" where $cow is one cow and @cow is a herd, %cow are the pet cows callable by name. Well, $cow could contain a cat, @cow could contain a gang of bulls and %cow the members of the cowboy family, but that's meant as freedom for the programmer to introduce bugs.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] [select] |
|
Yeah, I know it's feature, but I mean it is really worth? or maybe the current implementation for this is not good enough. see my above reply.
I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
| [reply] |
|
It is simply one of those things that is so intrinsic to the language that one can never contemplate getting rid of it.
| [reply] |