Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

[Perl6] Perl 6 discoveries IV, hash access

by holli (Monsignor)
on Oct 04, 2017 at 23:41 UTC ( #1200700=perlmeditation: print w/replies, xml ) Need Help??

Perl 6 has different ways to access hash elements:
use v6; my %hash = :a<A>, :b<B>; say %hash{'a'}; # says A say %hash<b>; # say B
This however leads inevitably to
#most likely not what you want, but prints an undefined warning say %hash<$somekey>;
which is a hard to spot bug since these: <> do not interpolate. That did just cost me quite a while.

Edit: Renamed as per advice


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re: Small Perl 6 discoveries IV, hash access
by stevieb (Abbot) on Oct 05, 2017 at 14:01 UTC

    Periodically, I dip into perl6, but haven't now in quite some time. However, I do vaguely recall being bit by this, and I believe I got the answer on IRC.

    I can't find any clear documentation that states that <> doesn't interpolate, other than a very vague portion of the Class Hash doc:

    The general Subscript rules apply providing shortcuts for lists of lit +eral strings, with and without interpolation. my %h = oranges => 'round', bananas => 'bendy'; dd %h<oranges bananas>; # OUTPUT: ("round", "bendy")&#9252; my $fruit = 'bananas'; dd %horanges "$fruit"; # OUTPUT: ("round", "bendy")&#9252;

    So it appears as though one needs to use <<>> for interpolation if you're using <> notation.

    There's no mention explicitly that <> does not interpolate, unless I'm searching incorrectly.

      On a related note, what's a good editor for typing weird unicode characters that perl6 likes, such as double angle brackets ?
        On a related note, what's a good editor

        Well, of course emacs and vi! ;-)

        for typing weird unicode characters that perl6 likes, such as double angle brackets ?

        Probably the same as you use now. The double angle brackets are part of ISO-8859-1 and its supersets ISO-8859-15, and Windows-1252 (at positions 0xAB and 0xBB), ISO-8859-9 (at the same positions), ISO-8859-13 (also at 0xAB and 0xBB), and ISO-8859-16 (again at the same positions). Mac Roman has them at 0xC7 and 0xC8.

        It's not so much an editor problem, but a keyboard layout problem.

        The commonly used keyboard layouts don't even have mappings for all symbols in the ISO-8859-x series. Keyboard layouts were designed for people writing texts in one or a few local languages, on mechanical typewriters. Computer keyboards have largely copied those layouts. See https://en.wikipedia.org/wiki/Keyboard_layout for the big picture, and the keyboard layouts shown in https://de.wikipedia.org/wiki/Tastaturbelegung for a lot of European layouts.

        Look at the classic US layout. It is good for ASCII, but nothing more. The UK layout has some french accented characters due to the french influences in old times. The extended variant adds some more.

        The standard German layout used in Germany and Austria has umlauts (), the sharp s (), and dead keys for writing French and some other accented languages. But it lacks a key kombination for and , also used in French. There are two extended variants (T2 and T3) that are rarely used and practically not available printed or engraved in key caps. T2 is suitable for all latin-based official languages of europe, T3 adds support for minority languages.

        Guess which layout is used in Switzerland. Right, they have their own layout, optimized for typing German, French, Italian and Romansh on a single keyboard. And you won't find the sharp s () on that keyboard, because Swiss german does not use it.

        Hop through a few more layouts (QWERTZ, AZERTY, QWERTY) and you will see a mess of mappings. Layouts for a larger set of language, e.g. US-International are available, and even actually used (e.g. in the Netherlands). BTW: The US-International keyboard allows typing the double angle brackets.

        But 10 Fingers and "only" about 100 keys make it hard to enter all of Unicode. Unicode currently (v10.0) has 139 scripts using 136755 characters, and there is still room for more. So even in future, keyboard layouts will suck if you need to type "exotic" characters. Perhaps even more than today.

        People keep inventing new keyboard layouts, optimized for something, making them hardly useable for something else. There is no best keyboard layout for everything, like there is no best editor for everything.

        Many people prefer coding using the US or the US-International keyboard, simply because most popular programming languages were designed to use the US keyboard.


        There is a solution from the early days of C, using trigraphs instead of characters not available on all platforms and keyboards: http://codingfox.com/4-5-trigraph-characters-in-c-language/

        Trigraph SequenceEqual character
        ??=#
        ??([
        ??)]
        ??/\
        ??<{
        ??>}
        ??!|
        ??^
        ??-~

        This makes hello world look a little bit funny, but it is still readable:

        ??=include<stdio.h> int main() ??< printf("Hello??/nWorld"); return 0; ??>
        #include<stdio.h> int main() { printf("Hello\nWorld"); return 0; }

        I would expect that Perl6 has a similar feature to express "uncommon" Unicode characters by using replacement ASCII sequences. And of course, a little bit of searching ends at Unicode versus ASCII symbols.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Has more to do with the OS/keyboard support. Most modern editors support whatever you can get out of your keyboard. If you have a Mac, the keyboard shortcuts are intuitive and trivial to learn; as is switching language support on the keyboard. PCs are a bit of hoop for all that but they are there or available for installation. Don't know about Linux keyboard support personally.

        On a related note, what's a good editor for typing weird unicode characters that perl6 likes, such as double angle brackets ?
        Unless I missed it, nobody so far has provided link to the following documentation, which, I think, brings at least some partial answers to your question: https://docs.perl6.org/language/unicode_entry.
        You can use whatever snippet plugin is available for your editor.


        holli

        You can lead your users to water, but alas, you cannot drown them.
Re: [Perl6] Perl 6 discoveries IV, hash access
by b2gills (Initiate) on Oct 17, 2017 at 16:12 UTC

    %hash<b> is short for %hash{<b>} which is short for %hash{qw<b>}
    There is also %hashb / %hash<<b>> which is similarly short for %hash{b} / %hash{qqwwb}

    With that knowledge it should be easy to see why %hash<$somekey> would be the same as %hash{'$somekey'}.

    use v6.c; my %hash = ( :a<A>, :b<B>, 'c d' => 'C~D', 'a b' => 'A~B', '$somekey' => '?' ); my $somekey = 'a b'; say %hash $somekey 'c d' .perl; # ("A", "B", "C~D") say %hash '$somekey' .perl; # "?" say %hash "$somekey" .perl; # "A~B" say %hash< $somekey >.perl; # "?"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1200700]
Approved by beech
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-12-12 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (327 votes). Check out past polls.

    Notices?