Live data from Hacker News

An experiment-driven guide to Perl

matt.might.net

21–30 of 44 posts

Re: An experiment-driven guide to Perl

#21
The core documentation is not always clear about the difference between lists and arrays--and much credit to the author for identifying the comma operator as an operator--but this is really confused:

By default, the arguments to a procedure are in the array context, which means that the comma operator expects both of its operands to be arrays. It promotes them to single-element arrays if they are scalars. In Perl, comma (,) can mean cons, append, flatten all at once.

I wrote an explanation of context in Perl which is hopefully clearer:

http://modernperlbooks.com/books/modern_perl/chapter_01.html...

Re: An experiment-driven guide to Perl

#22
> In Perl, there are three contexts in which an expression may be evaluated.

> 1. scalar

> 2. array

> 3. void

There's actually no such thing as "array context" in Perl; instead there's "list context". An array is a list that's been stored in a variable (this is a fairly common mistake).

See http://friedo.com/blog/2013/07/arrays-vs-lists-in-perl and http://perlmaven.com/scalar-and-list-context-in-perl for good examples/discussion.

EDIT:

Posted this before I finished the article. Understanding the difference between arrays and lists makes the following potential WTFs a lot clearer:

    sub take_two_arrs (\@\@) {
      print $_[0], $_[1] ;
    }

    take_two_arrs @a1, @b1 ;         # prints ARRAY(0xAddr) ARRAY(0xAddr)

    take_two_arrs ((1,2),(3,4)) ;    # error: arrays must be named
The second doesn't work because the prototyped function takes array references, not lists. It would work if you called it like this:

    take_two_arrs ([1,2],[3,4])
  
I'll admit that this is baffling.

    sub what_are (++) {
      print $_[0], " ", $_[1] ;
    }

    what_are ((1,2),(3,4)) # prints 2, then 4
(This is part of the reason that Perl programmers don't use prototypes very often.) perlsub warns:

> When using the + prototype, your function must check that the argument is of an acceptable type.

The plus here forces scalar context on the arguments, which are lists (not arrays!), so they return their last elements. This would work how the author probably wants if called like this:

    what_are ([1,2],[3,4]);   # prints ARRAY(0xAddr), ARRAY(0xAddr)

Re: An experiment-driven guide to Perl

#23
Ahh perl... the first language I used to make something non-trivial. The coolest script I wrote was a load balancer that used ssh to submit jobs and monitor the activity of nodes in a cluster via commands remotely executed by ssh. No root access needed, no servers to install, just needed to have an account accessible via ssh on the remote machine and ssh + perl on the machine you were working on. It was the simplest solution to the complex problem of "I have all of these computers, now how do I use them to their maximum potential?" Stuff like Linda existed than, but I found it way too complex in comparison to my simple scripts. Alas, I since moved onto the heady world of lisp, but I still use perl for the occasional $ perl -pe 's/this/that/' at the command line and as a alternative to bash scripting.

A merry Tim Toady to you all! Also, an obligatory xkcd: https://xkcd.com/224/

Re: An experiment-driven guide to Perl

#24
post #3

Full disclosure: I'm the guy who made http://perl-tutorial.org a few years ago because the top google result for "perl tutorial" was a perl 4 tutorial. I have looked at many tutorials and have a vested interest in getting quality tutorials in people's hands to avoid them writing shitty perl. That said, this tutorial is terrible on a number of points, since it teaches outdated things that have long been known to be da…

Article author here. To be clear, this is not a tutorial on writing good, idiomatic Perl. (And, I've strengthened the article's disclaimers to that emphasize that.) It's a semantic excavation of Perl. My goal was to understand how the Perl interpreter thinks, and to answer language design questions like: How are parameters passed--by value, by alias, by name, by reference, by need? How are variables scoped--lexically…

I can dig it. By analogy something like "If you insist on installing deck screws with a hammer instead of a screwdriver, this is what happens with a sledgehammer, this is what happens with a large heavy rock, this is what happens if you use your fist..." It is interesting, rather than terrifying, when seen in that light.

However, Google is going to google, thats its thing, so some victim in the future might think this is the one true answer to using objects in perl, which is not cool.

Re: An experiment-driven guide to Perl

#26
post #12
post #7

Earlier quoted context omitted.

If it's not a tutorial then i don't understand why he explains EXTREMELY basic things in the worst possible way, fails to actually explain the interesting details of subtle and complex things (like the differences between the ways a sub can be called (no, they do NOT all do the same thing)), and managed to produce a document that looks like it contains literally ALL the things from ALL the bad tutorials i've seen in…

Actually, he does appear to explain &foo() later on, he just doesn't bother mentioning up front that it's completely different. I think if you consider it as an academic piece, where you're supposed to read all of it and then think, it wouldn't be a bad introduction to perl as it was written in 2003.

Okay, I've read a few of Might's posts, and they're pretty interesting. But it irks me for some reason that nowhere in the posts can I find a date. Am I missing something? Where did you grab this 2003?

Re: An experiment-driven guide to Perl

#27

> In Perl, there are three contexts in which an expression may be evaluated. > 1. scalar > 2. array > 3. void There's actually no such thing as "array context" in Perl; instead there's "list context". An array is a list that's been stored in a variable (this is a fairly common mistake). See http://friedo.com/blog/2013/07/arrays-vs-lists-in-perl and http://perlmaven.com/scalar-and-list-context-in-perl for good example…

I'll admit that this is baffling.

The inner parentheses force evaluation of the two inner comma operators in the scalar context provided by the function prototype. `1` and `3` get evaluated in void context and discarded, leaving `2` and `4` as the two arguments to the function. (I had to look up `+` in prototypes, however.)

Without a working understanding of lists and context, this example is undoubtedly baffling, but that's why the documentation exists.

Re: An experiment-driven guide to Perl

#28
post #3

Full disclosure: I'm the guy who made http://perl-tutorial.org a few years ago because the top google result for "perl tutorial" was a perl 4 tutorial. I have looked at many tutorials and have a vested interest in getting quality tutorials in people's hands to avoid them writing shitty perl. That said, this tutorial is terrible on a number of points, since it teaches outdated things that have long been known to be da…

Article author here. To be clear, this is not a tutorial on writing good, idiomatic Perl. (And, I've strengthened the article's disclaimers to that emphasize that.) It's a semantic excavation of Perl. My goal was to understand how the Perl interpreter thinks, and to answer language design questions like: How are parameters passed--by value, by alias, by name, by reference, by need? How are variables scoped--lexically…

I haven't read the article completely, but I've enjoyed what I've read so far. I'd like to follow along in more detail with a perl interpreter. I'd love to see an entire book that completely tears down the entire interrupter from the perspective of within.

I purchased Ruby Under a Microscope, but I was kind of put off by the similar issues other commentators are having, that it appears to be a basic introduction to how to write Ruby, but that is just how the task has to be approached (based off your existing assumptions and test to see if they hold up). But when the task is complete, you have knowledge on a better way to write Ruby, by accounting for all the exceptions that you can't "see" in the source code, due to leaky abstractions.

Re: An experiment-driven guide to Perl

#29
post #3

Full disclosure: I'm the guy who made http://perl-tutorial.org a few years ago because the top google result for "perl tutorial" was a perl 4 tutorial. I have looked at many tutorials and have a vested interest in getting quality tutorials in people's hands to avoid them writing shitty perl. That said, this tutorial is terrible on a number of points, since it teaches outdated things that have long been known to be da…

>have a vested interest in getting quality tutorials in people's hands to avoid them writing shitty perl.

Thank you!

I did have the idea of starting a more general site to highlight this problem (wrongtutorial.com) but I couldn't find the right approach.

Good tutorials are hard to find - and the amount of obsolete or just plain wrong information is staggering.

Re: An experiment-driven guide to Perl

#30

> In Perl, there are three contexts in which an expression may be evaluated. > 1. scalar > 2. array > 3. void There's actually no such thing as "array context" in Perl; instead there's "list context". An array is a list that's been stored in a variable (this is a fairly common mistake). See http://friedo.com/blog/2013/07/arrays-vs-lists-in-perl and http://perlmaven.com/scalar-and-list-context-in-perl for good example…

I'll admit that this is baffling. The inner parentheses force evaluation of the two inner comma operators in the scalar context provided by the function prototype. `1` and `3` get evaluated in void context and discarded, leaving `2` and `4` as the two arguments to the function. (I had to look up `+` in prototypes, however.) Without a working understanding of lists and context, this example is undoubtedly baffling, bu…

I guess I really meant "is baffling without a thorough understanding of context." I feel better about looking up the + now that I know you did too, though! I don't think I've ever seen code like that in the wild, and I've never seen a Perl programmer attempt to call a sub with nested parens, like what_are((1,2),(3,4)) before.
Post reply on HN