4 ms·
> Except the resulting code will look nothing like “the humans say it”, Actually, I find it maps rather directly to how a lot of people explain things. For ex
by kbenson 8y ago
> Except the resulting code will look nothing like “the humans say it”,
Actually, I find it maps rather directly to how a lot of people explain things. For example, "Examine each number, and as long as it's greater than 10 and odd, then pass it to the the work function."
for my $number ( @numbers ) {
next unless $number > 10;
next unless $number % 2;
work($number);
}
Perl generally allows you to structure your code as you would explain how to accomplish something to another person. This can be beneficial or detrimental depending on how deep you go with this concept. A lot of becoming a good Perl programmer is learning where a sane line is for this, but each language usually has a similar feature which the user needs to decide where the sane point to stop is before taking it to an extreme that is detrimental.
There are definitely aspects of Perl that I think are better left untouched, and thankfully the community has pretty much come to consensus about the most problematic missteps the language allows.
- chombier 8y agoOne can write pretty much the same code in python/c++11/whatnot and it also feels quite natural to read: for number in numbers: if number <= 10: continue work(number) Do you have a better example where the Perl syntax would shine?
- kbenson 8y agoNote: This is all in the context of Perl 5. You can assume the following works with little or no changes in Perl 6, along with probably 3+ other new ways to do it (which is actually one of my peeves about Perl 6). There's a few things. Full word but lower precedence boolean operators (and, or, not) which allows for combining statements in a way that &&, || and ! doesn't always facilitate, but really shine in making boolean statements more readable. e.g. die "some problem" unless defined $some_var and not $some_var eq "foo"; But I think you somewhat missed my point. What you've done in your example, is easy to read, but you've either unconsciously or consciously switched the order of some statements so they are valid in the context of Python. This is trivial and easy with simple conditionals, but if they are complex it can hide the point of the statement, which is that this is a shortcut to the next loop iteration. I find putting the "verb" first can make for much easier understanding of what's going on when it gets more complex. A comparison of that might be something like the following: for my $item ( @items ) { # Standard barrage of tests next if test1($item) or test2($item) or test3($item) or test4($item) or test5($item); # If it's a special item, do a couple more tests next if test_guard($item) and ( subtest1($item) or subtest2($item) ); # If it's an end marker, skip the rest last if is_last_processable($item); ... } Even though the actions are condensed, I quickly know what the point of each one is, and what it does. Condensing them also allows me to comment each without feeling I'm cluttering the logic and making it more verbose than needed (too verbose, and you raise the likelihood related code will be off-screen or not as easily visually associated, which hampers comprehension), but event without the comments having the defining action start the line helps clarify intent. I imagine in Python, the continue statement would either be far to the right from larger conditionals, or moved to a more traditional scope underneath taking up an extra line, or split into multiple if statements. This is also a good example in Perl of where the community has come to a fairly good consensus that while you can make very complex postconditional statements, don't, as it defeats the purpose (just split it into multiple simple ones or use a traditional precontitional if formatted to be more readable, or some other technique such as computing portions of the condition separately and comparing them later). Also, regarding if pre or post positioning, I feel that Perl allows the statements to be expressed as you might say them or think of them. That's the point of postconditionals in Perl. Also, like how you would express them verbally to someone, a postconditional only works on a single statement (it does not support blocks through braces). You wouldn't normally say "do X, do Y, do Z if A is true" and expect it to apply unambiguously to tasks X, Y and Z. On the flip side, a preconditional not only supports braces, it reqiures them, since the simple one statement case is easily handled through the postconditional. e.g. # Valid if ( $A ) { do_something(); } if ( $A ) { do_something(); } do_something() if $A; # Invalid if ( $A ) do_something(); { do_something(); } if $A; This is an example of one of the places Perl not only allows you to use natural expressions to express yourself, but guides you away from odd or problematic ways of doing so (i.e. ambiguous speech patterns). As for sigils, some people really dislike them, but I find the useful. I see them a little signifiers of the nouns in any statement (and the type of noun, of which there are only a few). Finally, there's "context", which has a specific meaning in Perl, and is probably the most interesting and unique thing about the language. It can be very useful, but the complexities can sometimes cause interesting behavior that was unintended or non-obvious initially. That said, its also what makes a lot of the "magic" (really just the rules put into action) of the array and hash data types work, so it's not something that could feasibly be removed (and nor would I want it to be). Instead, knowledge of the problematic spots is acknowledged by the community, so we try to avoid those problems, and for the most part they are old problems (this is usually a function that may produce multiple values being using within the definition of a hash or array in list context, exemplified by decades old projects based on CGI.pm which has been deprecated).
- ricardobeat 8y agoLots of other languages have postfix conditionals. On the readable boolean ops, all the Perl people recommend shying away from them except for a few specific constructs (do or die, eval or do, etc), since the weaker precedence rules make it really easy to shoot yourself on the foot: $a = $b or $c; Where $a will never be assigned the value of $c.
- kbenson 8y ago> Lots of other languages have postfix conditionals. I didn't make a case as Perl being the only language, just that it's an interesting aspect of Perl. That said, I would be interested in what languages you are referring to. I haven't seen many. > On the readable boolean ops, all the Perl people recommend shying away from them except for a few specific constructs (do or die, eval or do, etc), since the weaker precedence rules make it really easy to shoot yourself on the foot: They are perfectly acceptable in a boolean statement. You only need to shy away from them when mixing them with assignments or higher precedence boolean constructs. The only case where you would have to worry is in non-simple boolean statements with assignment in the statement, or functions that omit parens for arguments in some cases, or mathematical operations without grouping parens, all of which I would argue should include their own grouping parenthesis for clarity anyway. That said, I devoted space in both of my comments in this thread to noting that there are definitely places where you might shoot yourself in the foot with some of these features, and you'll note the examples I used them in are exactly as you've described, where they are either after a postconditional (in which case it's kept to a simple boolean statement which has no ambiguity), or to affect control flow (e.g. return or die), in which that assignment doesn't matter anyway. > Where $a will never be assigned the value of $c. Which I never used as an example for that reason, but that's the point of those operators. Being lower precedence allows for some alternate constructs than is possible in many other languages without them. Someone asked for examples of constructs that help Perl match mental models of how might think of an algorithm, so I decided to explain a few. The purpose isn't to point out how Perl is better than Python or some other language, but how it's different in an interesting way. A lot of that was planned, but sometimes that came out to its benefit, and sometimes to its detriment. Like most interesting new things (because at the time it was), it's not entirely one thing or the other.
- raiph 8y agoYou have clearly been able to read and translate the GP's code and confidently assert you've written "pretty much the same code". > Do you have a better example where the Perl syntax would shine? Unless you have "a ton of knowledge about Perl" your reply shows that it was a perfect example in response to the GGP.
- mmirate 8y agoThat ought to be a oneliner in any sane language today: numbers.into_iter().filter(|n| n>10 && n%2!=0).for_each(work); map work . filter ((/=0) . (`mod` 2)) . filter (>10) $ numbers Insert tirade about how for-loops are free to do so much that they are slower-to-comprehend than specific-purpose iterator/list functions, here.
- bmn__ 8y agoThe language is happy to accommodate any style you like best, including oneliners. sub work($n) { say $n } my @numbers = 1..100; # rubyish @numbers.grep(* > 10 && * %% 2).map(&work); # lol turbo haskal map &work <== grep * %% 2 <== grep * > 10 <== @numbers; # same, but in proper reading direction @numbers ==> grep * > 10 ==> grep * %% 2 ==> map &work; The traditional function composition operator exists, see https://docs.perl6.org/routine/%E2%88%98 https://docs.perl6.org/routine/%E2%88%98
- b2gills 8y agoThe following doesn't do what you think it does * > 10 && * %% 2 That is two WhateverCode objects which each take one argument. Since both are definite it gives you the second one. my &a = * > 10; my &b = * %% 2; my &c = &a && &b; &c === &b; # True If it wasn't split up by the `&&`, it would be a code object that took two arguments. # using multiplication (×) as a boolean and # (it always cooperates in the WhateverCode lambda syntax) my &c = (* > 10) × (* %% 2); say so c(10,2); # True say (1..20).grep(&c); # ((11 12) (13 14) (15 16) (17 18) (19 20)) Note that `grep` is written in terms of `map` (1..20).map({ ($^a,$^b) if ($^a > 10) × ($^b %% 2) }) # ((11 12) (13 14) (15 16) (17 18) (19 20)) If you need to refer to an argument more than once, you (generally) can't do it with the WhateverCode lambda syntax. -> $n { $n > 10 && $n %% 2 } { $^n > 10 && $^n %% 2 } sub ($n){ $n > 10 && $n %% 2 } I say generally because array indexing will give you the number of elements for all the arguments you ask for. @a[ (* × ⅓) .. (* × ⅔) ] @a[ (@a.elems × ⅓) .. (@a.elems × ⅔) ]
- raiph 8y ago
- lizmat 8y agoIn Perl 6 this could almost look the same: for @numbers -> $number { next unless $number > 10; next unless $number % 2; work($number); } Although personally, I wouldn't write it like that: I would probably write it as: for @numbers.grep( { $_ > 10 && $_ % 2 } ) -> $number { work($number) } or use the postfix notation: work($_) for @numbers.grep( { $_ > 10 && $_ % 2 } ); If you want to spread this out over multiple CPUs and you don't care about the order in which the work is done, you only need to add the `.race` method: for @numbers.grep( { $_ > 10 && $_ % 2 } ).race -> $number { work($number) } More info: https://docs.perl6.org/routine/race https://docs.perl6.org/routine/race
- xisukar 8y agoThis would be one way to write in Perl 6: for @numbers -> $number { next unless $number > 10; next if $number %% 2; work($number); }