From mark at purdue.edu Tue Oct 2 07:41:18 2018 From: mark at purdue.edu (Mark Senn) Date: Tue, 02 Oct 2018 10:41:18 -0400 Subject: [Purdue-pm] learning Perl 6 Message-ID: <9468.1538491278@pier.ecn.purdue.edu> I contacted Purdue Libraries yesterday to see if they will add _Learning Perl 6_ to their collection. See http://shop.oreilly.com/product/0636920062776.do for more information about _Learning Perl 6_. No word back yet. Perl 6 is a much better programming language than Perl 5 in my opinion. To use Perl 6 effectively and efficiently you'll need to know how Perl 6 signatures work. I suggest first reading, _An Introduction to how subroutine signatures work in Perl 6_ at https://opensource.com/article/18/9/signatures-perl-6 (The # Perl 5 frobnicate( bar => 42 ); it will pass two values, "foo" and 42, typo has already been reported.) Let your brain rest, and then skim _class Signature_ at https://docs.perl6.org/type/Signature Don't worry if you only "get" a tiny fraction of this on the first reading. Note the sub f(Real $x where { $x > 0 }, Real $y where { $y >= $x }) { } line, one can also define sub f(Real $x where { $x <= 0 }, Real $y where { $y >= $x }) { } to deal with cases where $x <= 0. Using multi-methods to deal with different types or values of arguements is much nicer than having to write code to deal with all the cases in my opinion. It's clear to me that programming in Perl 6 is going to save time and produce better, self-documenting code than in Perl 5. Rhetorical question: how long do you want to wait before saving time? -mark From mark at purdue.edu Fri Oct 5 10:10:55 2018 From: mark at purdue.edu (Mark Senn) Date: Fri, 05 Oct 2018 13:10:55 -0400 Subject: [Purdue-pm] Perl 5's '{' .. '~' "broken" Message-ID: <5553.1538759455@pier.ecn.purdue.edu> #!/usr/new/bin/perl # In Perl 5 # my @s = ('{'..'~', 'a'..'c'); # print "@s\n"; # prints # { a b c # instead of # { | } ~ a b c # # _Modern Perl: 2011--2012_ edition at # http://modernperlbooks.com/books/modern_perl/chapter_04.html # states # The infix range operator (..) produces a list of items in list context: # my @cards = ( 2 .. 10, 'J', 'Q', 'K', 'A' ); # It can produce simple, incrementing ranges (both as integers or strings), # but it cannot intuit patterns or more complex ranges. # # Apparently '{'..'~' is a range that Perl 5 does not understand. # # I needed a way to get the ordinal character values (a is 65, b is 66, etc.) # for some character ranges and came up with the following. Let me know if # you have any improvements. use feature 'say'; sub Ranges { # The return value. my @r = (); (@_ == 0) and die 'Ranges called with no arguments'; (@_ % 2) and die 'Ranges called with an odd number of arguements'; # Process a character range. for (my $i = 0; $i < @_; $i += 2) { push @r, (ord $_[$i] .. ord $_[$i+1]); } return @r; } my @a = Ranges qw+{ ~ a c+; say "\@a is @a"; my @b = Ranges qw+A Z a z+; say "\@b is @b"; my @c = Ranges qw+! / 0 9 : @ [ ` { ~+; say "\@c is @c"; my @d = Ranges qw+0 9 A Z a z+; say "\@d is @d"; # -mark From gizmo at purdue.edu Fri Oct 5 10:46:25 2018 From: gizmo at purdue.edu (Joe Kline) Date: Fri, 5 Oct 2018 13:46:25 -0400 Subject: [Purdue-pm] Perl 5's '{' .. '~' "broken" In-Reply-To: <5553.1538759455@pier.ecn.purdue.edu> References: <5553.1538759455@pier.ecn.purdue.edu> Message-ID: <3b66a37a-ed73-c7cd-a3b9-eaf4e1953261@purdue.edu> Broken or working as documented? http://perldoc.perl.org/perlop.html#Range-Operators "If the initial value specified isn't part of a magical increment sequence (that is, a non-empty string matching /^[a-zA-Z]*[0-9]*\z/ ), only the initial value will be returned." Also, looking at the example in perldoc if you change your code from: push @r, (ord $_[$i] .. ord $_[$i+1]); to push @r, (ord "$_[$i]" .. ord "$_[$i+1]" ); It will work as intended I think. joe -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 198 bytes Desc: OpenPGP digital signature URL: From mark at purdue.edu Fri Oct 5 12:32:48 2018 From: mark at purdue.edu (Mark Senn) Date: Fri, 05 Oct 2018 15:32:48 -0400 Subject: [Purdue-pm] Perl 5's '{' .. '~' "broken" In-Reply-To: <3b66a37a-ed73-c7cd-a3b9-eaf4e1953261@purdue.edu> References: <5553.1538759455@pier.ecn.purdue.edu> <3b66a37a-ed73-c7cd-a3b9-eaf4e1953261@purdue.edu> Message-ID: <46316.1538767968@pier.ecn.purdue.edu> Joe Kline wrote on 2018-10-05 at 1346 -04: | Broken or working as documented? | | | http://perldoc.perl.org/perlop.html#Range-Operators | | "If the initial value specified isn't part of a magical increment | sequence (that is, a non-empty string matching /^[a-zA-Z]*[0-9]*\z/ ), | only the initial value will be returned." | | Also, looking at the example in perldoc if you change your code from: | | push @r, (ord $_[$i] .. ord $_[$i+1]); | | to | | push @r, (ord "$_[$i]" .. ord "$_[$i+1]" ); | | It will work as intended I think. | | joe Hi Joe, I put broken in quotes because it violates the priciple of least astonishment in my opinion. See https://en.wikipedia.org/wiki/Principle_of_least_astonishment for more info on that. I saw the "If the initial value ..." text when "reading the fine print" trying to figure out why the simple way of doing this didn't work. The code I wrote works with push @r, (ord $_[$i] .. ord $_[$i+1]); or push @r, (ord "$_[$i]" .. ord "$_[$i+1]" ); with Perl 5.28.0. I skipped the quotes because I didn't want the extra clutter. I changed some of the comments in the code to make it more clear that the top part didn't work as expected and that's why I wrote the code in the bottom part. -mark p.s. here's the new program (same code, slightly different comments as written about above) #!/usr/new/bin/perl # In Perl 5 # my @s = ('{'..'~', 'a'..'c'); # print "@s\n"; # prints # { a b c # instead of # { | } ~ a b c # # _Modern Perl: 2011--2012_ edition at # http://modernperlbooks.com/books/modern_perl/chapter_04.html # states # The infix range operator (..) produces a list of items in list context: # my @cards = ( 2 .. 10, 'J', 'Q', 'K', 'A' ); # It can produce simple, incrementing ranges (both as integers or strings), # but it cannot intuit patterns or more complex ranges. # # Apparently '{'..'~' is a range that Perl 5 does not understand. # # Because the easy way ('{' .. '~') to do this didn't work for me I # wrote the following code below to get the ordinal character values # needed. I didn't use charnames (https://perldoc.perl.org/charnames.html) # becaue I wanted to be absolutely sure what characters I was dealing with. use feature 'say'; sub Ranges { # The return value. my @r = (); (@_ == 0) and die 'Ranges called with no arguments'; (@_ % 2) and die 'Ranges called with an odd number of arguements'; # Process a character range. for (my $i = 0; $i < @_; $i += 2) { push @r, (ord $_[$i] .. ord $_[$i+1]); } return @r; } my @a = Ranges qw+{ ~ a c+; say "\@a is @a"; my @b = Ranges qw+A Z a z+; say "\@b is @b"; my @c = Ranges qw+! / 0 9 : @ [ ` { ~+; say "\@c is @c"; my @d = Ranges qw+0 9 A Z a z+; say "\@d is @d"; # -mark