TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An experiment-driven guide to Perl

55 pointsby sea6earover 11 years ago

10 comments

Mithalduover 11 years ago
Full disclosure: I&#x27;m the guy who made <a href="http://perl-tutorial.org" rel="nofollow">http:&#x2F;&#x2F;perl-tutorial.org</a> a few years ago because the top google result for &quot;perl tutorial&quot; was a perl 4 tutorial. I have looked at many tutorials and have a vested interest in getting quality tutorials in people&#x27;s hands to avoid them writing shitty perl.<p>That said, this tutorial is terrible on a number of points, since it teaches outdated things that have long been known to be dangerous and are only kept around for the sake of backwards compatibility. Reading it is wholly a waste of your time, unless you already know perl like the back of your hand and wish to get enraged; or have the masochistic desire to learn perl in a manner that will punish both yourself and others for your mistake of reading this tutorial.<p>If you truly wish to learn about Perl in a whirlwind tour, read either the very short free book Modern Perl [1] or any other short tutorial linked on the site i mentioned first.<p>If you&#x27;re the author of this tutorial, i applaud you for the effort, but wish you&#x27;d have spoken to any part of the community before publishing. If you feel like it, #perl-help on irc.perl.org is a great place to start. And if you meant this as a troll, 10&#x2F;10, would rage anytime.<p>[1] <a href="http://onyxneon.com/books/modern_perl/" rel="nofollow">http:&#x2F;&#x2F;onyxneon.com&#x2F;books&#x2F;modern_perl&#x2F;</a>
评论 #7220940 未加载
评论 #7220812 未加载
评论 #7221626 未加载
评论 #7220674 未加载
j_m_bover 11 years ago
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 &quot;I have all of these computers, now how do I use them to their maximum potential?&quot; 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 &#x27;s&#x2F;this&#x2F;that&#x2F;&#x27; at the command line and as a alternative to bash scripting.<p>A merry Tim Toady to you all! Also, an obligatory xkcd: <a href="https://xkcd.com/224/" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;224&#x2F;</a>
maxlybbertover 11 years ago
As a long time Perl programmer, I am a little disappointed with the lack of comments on this post.<p>I generally follow Matt Might&#x27;s blog, and I am impressed by previous posts. Unfortunately, this tutorial is likely to leave beginners more confused than when they started. I have to encourage people to look at chromatic&#x27;s free book &quot;Modern Perl&quot; instead.
theOnliestover 11 years ago
&gt; In Perl, there are three contexts in which an expression may be evaluated.<p>&gt; 1. scalar<p>&gt; 2. array<p>&gt; 3. void<p>There&#x27;s actually no such thing as &quot;array context&quot; in Perl; instead there&#x27;s &quot;list context&quot;. An array is a list that&#x27;s been stored in a variable (this is a fairly common mistake).<p>See <a href="http://friedo.com/blog/2013/07/arrays-vs-lists-in-perl" rel="nofollow">http:&#x2F;&#x2F;friedo.com&#x2F;blog&#x2F;2013&#x2F;07&#x2F;arrays-vs-lists-in-perl</a> and <a href="http://perlmaven.com/scalar-and-list-context-in-perl" rel="nofollow">http:&#x2F;&#x2F;perlmaven.com&#x2F;scalar-and-list-context-in-perl</a> for good examples&#x2F;discussion.<p>EDIT:<p>Posted this before I finished the article. Understanding the difference between arrays and lists makes the following potential WTFs a lot clearer:<p><pre><code> 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 </code></pre> The second doesn&#x27;t work because the prototyped function takes <i>array</i> references, not lists. It would work if you called it like this:<p><pre><code> take_two_arrs ([1,2],[3,4]) </code></pre> I&#x27;ll admit that this is baffling.<p><pre><code> sub what_are (++) { print $_[0], &quot; &quot;, $_[1] ; } what_are ((1,2),(3,4)) # prints 2, then 4 </code></pre> (This is part of the reason that Perl programmers don&#x27;t use prototypes very often.) perlsub warns:<p>&gt; When using the + prototype, your function must check that the argument is of an acceptable type.<p>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:<p><pre><code> what_are ([1,2],[3,4]); # prints ARRAY(0xAddr), ARRAY(0xAddr)</code></pre>
评论 #7221489 未加载
chromaticover 11 years ago
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:<p><i>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><p>I wrote an explanation of context in Perl which is hopefully clearer:<p><a href="http://modernperlbooks.com/books/modern_perl/chapter_01.html#context" rel="nofollow">http:&#x2F;&#x2F;modernperlbooks.com&#x2F;books&#x2F;modern_perl&#x2F;chapter_01.html...</a>
ceronmanover 11 years ago
I loved this article.<p>I started writing Perl nine months ago because of my new job. I learned it with the Modern Perl book, which is really nice and goes directly to the best practices. However, I&#x27;ve found that real life Perl code is full of the old&#x2F;deprecated&#x2F;insane ways of doing things as well. And Perl developers really take the TIMTOWTDI principle to the limit.<p>This article helped me to understand Perl more. And specially to understand real life Perl code better. I also liked the language designer perspective and the semantic analysis. Thanks for writing it!
pasbesoinover 11 years ago
&quot;alarming brevity&quot; :-)<p>Looking forward to reading further into this.
评论 #7220866 未加载
raiphover 11 years ago
Hi Matt,<p>I&#x27;ve been exploring your blog. Great stuff! I was particularly appreciative of the parsing articles.<p>Larry Wall (the Perl designer) has been designing and helping develop a new language for years. (He claims he began thinking about this new language before Perl 5 shipped 20 years ago.) Arguably it addresses the same sort of audience as Scala and Haskell. Have you taken a look at it?
评论 #7290077 未加载
slashdotaccountover 11 years ago
I&#x27;ll concentrate on the mistakes. If I were to criticise all the other many weird formulations and expressions in this guide which make it hard to unambiguously understand what the author meant, I would still sit and be typing here tomorrow.<p>----<p>&gt; A code comment in Perl begins with a hash #<p>Hashes are already something different in Perl. Avoid ambiguity, use the common name of that character: number sign.<p>&gt; procedure<p>This word is used through-out, but the official Perl documentation does not mention it. Use the word subroutine (or just sub for short) instead.<p>&gt; The $ prefix references a variable as a scalar<p>&gt; Array variables use the prefix @<p>This is the wrong explanation. The sigil denotes the mode of access, @ indicating the expression evaluating to a list value, $ indicating a single value. This becomes clear when one examines slices of a compound data structure.<p><pre><code> @arr = (&quot;foo&quot;,&quot;bar&quot;,&quot;baz&quot;); $arr[1]; # &quot;bar&quot; @arr[2,3]; # (&quot;bar&quot;, &quot;baz&quot;) %hash = (&quot;foo&quot;, 1, &quot;bar&quot;, 2); $hash{&quot;foo&quot;}; # 1 @hash{&quot;foo&quot;, &quot;bar&quot;}; # (1, 2) </code></pre> The guide mentions the change from @ to $ or from % to $ only in passing without explanation, and does not mention slices at all.<p>&gt; Hash variables expect an array for initialization.<p>No, a list.<p>&gt; three contexts in which an expression may be evaluated:<p>&gt; 1. scalar<p>&gt; 2. array<p>&gt; 3. void<p>No, the second is list context.<p>&gt; Is localtime() returning a scalar, or an array?<p>No, a scalar or a list.<p>&gt; 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.<p>&gt; It seems that the function call still flattened out the arrays (and hashes) when making the call.<p>This is completely misleading. A sub takes always a list. What is described here has nothing to do with arguments, but is the consequence of the specifics of how values are evaluated into a list. This also happens, for example, on list assignment.<p>&gt; all of the following are equivalent procedure calls:<p>&gt; print3 (1,2,3) ;<p>&gt; &amp;print3 (1,2,3) ;<p>This is wrong, there is a difference, it just did not show up in the example.<p>&gt; In fact, the argument isn’t even hash, despite what the specifier says<p>Refer to the documentation: when not backslashed, % is defined to behave like @.<p>&gt; sub use_hash (%) {<p>&gt; print $_[0]{&quot;foo&quot;} ;<p>&gt; print $_{&quot;foo&quot;} ;<p>&gt; print @_{&quot;foo&quot;} ;<p>&gt; }<p>&gt; use_hash (&quot;foo&quot; =&gt; 1701) ; # prints nothing<p>No wonder. The code is broken.<p>@_ contains a plain list value. To access it with a hash subscript, turn it into a hashref first.<p><pre><code> print +{ @_ }-&gt;{&quot;foo&quot;}; print ${ {@_} }{&quot;foo&quot;}; </code></pre> &gt; The specifier &amp; expects to receive a function<p>Not function, coderef is the appropriate word.<p>&gt; To accept a bareword filehandle as an argument, it becomes necessary to use the rarely used * prototype specifier<p>Simply passing *F is also possible, no prototype involved.<p>&gt; The repetition operator x repeats a string or an array,<p>No, it repeats single or list values. Scalars are coerced into their string representation, and lists are simply repeated unchanged.<p>----<p>Closing words: This is amateur hour, not worthy of a professor. Advice for next time: consult domain experts and have them proof-read before publishing, and also always give your documents a last-modification date and version history, or at least a version identifier.
durrrrrrrover 11 years ago
&quot;Every programmer needs Perl in their arsenal.&quot;<p>I stopped reading at this point.