Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Is typeglob feature really useful?

by xiaoyafeng (Deacon)
on Jan 09, 2021 at 17:48 UTC ( #11126662=perlmeditation: print w/replies, xml ) Need Help??

As we all know, Perl support same name but different type, that means $foo and @foo could be both exist without any violation.

This sounds a smart and nature feature, but from time to time, I suspect if it's useful. During my life, I've never defined 2 different type of variables with same name. Considering perl have to define a special(or maybe huge and complex ) data struct to store glob, if we discard this feature, maybe perl could be faster and simpler?

Just a thought jump into my head, Please enlighten me. Thanks

EDIT: modify title as LanX point out.




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: Is glob feature really useful?
by LanX (Cardinal) 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.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    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

      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

        That's only the case with our variables and subs.

        Package variables are the exception and sub lookup is optimized AFAIK.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: Is typeglob feature really useful?
by Tux (Abbot) 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
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'
      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

        It is simply one of those things that is so intrinsic to the language that one can never contemplate getting rid of it.
Re: Is typeglob feature really useful?
by eyepopslikeamosquito (Bishop) on Jan 10, 2021 at 02:59 UTC
      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

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://11126662]
Approved by LanX
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2021-01-22 00:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?