In Perl, context is the door that lets you enter the land of understanding
German text below.
In Perl, context is the door that lets you enter the land of understanding
German text below.
It’s probably not quite that dramatic. And there are certainly far more difficult concepts in Perl. But at the beginning of the week, I had a strong feeling that I really needed to understand this, otherwise I’d be (further) held back in all subsequent topics.
After spending almost a day working on this, I thought I’d grasped it. I came across an article by Randal L. Schwartz, titled “It’s all about context (June 1),” in which he uses an unpleasant incident to illustrate the subtle but important difference between scalar and list context.
“So,” I thought, “now I understand.” Or so I thought… But when I took a closer look at the crucial part, confusion grabbed me from behind and brutally knocked me to the ground.
my($f) = fortune;
when what he should have done was:
my $f = fortune;
The issue is that the output of a program should be stored in a variable. Ideally, a scalar variable, because the code writer wanted the entire output. The problem was, he confused list variables with list context. The result: only the first line of the output is used.
What I kept stumbling over was that $f is actually a scalar, not a list. I kept wondering why the context suddenly turns a scalar into a list. Until this morning on my way to work, it suddenly dawned on me. Or, to quote Captain Dathon: “Sokath, his eyes uncovered!” The way I have felt this morning, that’s how that old Greek must have felt when he shouted “Eureka!”
The context doesn’t change the variable $f; it determines how an action — in this case, assigning the output of fortune — handles the variable $f.
In other words:
$f IS a scalar. With parentheses, the input is processed like a list, so only the first line is read. Without parentheses, the input is processed like a scalar, so the entire input is read. But always INTO A SCALAR VARIABLE.
Do you know how it feels when you take the same wrong turn over and over for days and never get where you want to go? And then, one day, without thinking much about it, you turn in the other direction and BANG!: You’ve reached your destination. And now that you’ve reached it, it’s suddenly hard to understand why you kept taking the wrong turn.
So, be kind to those who don’t immediately grasp such obvious things. And give them time to figure it out for themselves. The experience is 10 times, 100 times, nay, 1000 times better than if someone explains it to you and you still don’t really understand how it works. … As a further aid, I’ve compiled a few links on this topic:
PerlMonth — “Coding with Style”
Finally, a quote from the “Camel book”:
“You will be miserable until you learn the difference between scalar and list context”
— Programming Perl, 3rd ed., page 69
Deutscher Text:
Wahrscheinlich ist es nicht ganz so dramatisch. Und sicher gibt es in Perl noch weit schwierigere Konzepte. Aber ich hatte Anfang der Woche den starken Eindruck, dass ich das zuerst wirklich verstehen muss, sonst werde ich bei allen folgenden Themen (zusätzlich) ausgebremst.
Nach einem knappen Tag der Beschäftigung mit diesem Thema dachte ich, ich hätte es verstanden. Ich stiess dabei auf einen Artikel von Randal L. Schwartz mit dem Titel: It’s all about context (June 1), in dem er anhand einer unschönen Begebenheit auf den feinen, aber wichtigen Unterschied zwischen skalarem und Listen Kontext eingeht.
“So”, dachte ich, “jetzt habe ich es aber verstanden”. Dachte ich … Aber als ich mir den entscheidenden Teil genauer ansah, packte mich die Verwirrung von hinten und schlug mich brutal zu Boden.
my($f) = fortune;
when what he should have done was:
my $f = fortune;
Es geht dabei darum, dass der Output eines Programms in eine Variable übernommen werden soll. Sinnvollerweise in eine skalare Variable, weil der Schreiber des Codes den gesamten Output haben wollte. Das dumme war, er verwechselte Listenvariable mit Listen Kontext. Die Folge davon: Es wird nur die erste Zeile des Outputs verwendet.
Worüber ich nun die ganze Zeit stolperte, war, dass $f doch ein Skalar ist, und keine Liste. Ich fragte mich immer wieder, warum der Kontext aus einem Skalar plötzlich eine Liste macht. Bis es mir dann heute morgen auf dem Weg zur Arbeit wie Schuppen von den Augen fiel. Oder um es mit den Worten von Captain Dathon zu sagen: “Sokath, seine Augen unbedeckt!” So wie ich heute morgen muss sich dieser alte Grieche gefühlt haben, als er “Heureka!” rief.
Der Kontext verändert nicht die Variable $f, er bestimmt die Art, wie eine Aktion — in diesem Fall die Zuweisung des Outputs von fortune — mit der Variablen $f umgeht.
Mit anderen Worten:
$f IST ein Skalar. Mit Klammern wird der Input wie eine Liste verarbeitet, also wird nur die erste Zeile eingelesen. Ohne Klammern wird der Input wie ein Skalar verarbeitet, also wird der gesamte Input eingelesen. Aber immer IN EINE SKALARE VARIABLE.
Wisst ihr, wie man sich fühlt, wenn man während mehreren Tagen immer wieder die gleiche falsche Abzweigung nimmt und nicht dort ankommt, wo man hin will? Und dann, eines Tages, ohne gross zu überlegen, biegt in die andere Richtung ab und PENG!: Man hat sein Ziel erreicht. Und nun, da man es erreicht hat, fällt es einem plötzlich schwer nachzuvollziehen, warum man davor immer falsch abgebogen ist.
Also, seid gnädig mit denen, die soooooo offensichtliche Dinge nicht auf Anhieb verstehen. Und lasst ihnen die Zeit, es selbst herauszufinden. Das Erlebnis ist 10 mal, 100 mal, ach was, 1000 mal besser als wenn es einem jemand erklärt und man dann doch nicht so richtig weiss, wie es eigentlich funktioniert.
Als keine Hilfestellung habe ich hier noch ein paar Links zu dem Thema zusammengetragen:
PerlMonth — “Coding with Style”
Zum Schluss noch ein Satz aus dem Kamel Buch:
“You will be miserable until you learn the difference between scalar and list context”
— Programming Perl 3rd ed, page 69
메타데이터
- post_id
- 91415bd4b4e7
- slug
- in-perl-context-is-the-door-that-lets-you-enter-the-land-of-understanding-91415bd4b4e7
- url
- https://medium.com/@mp.maurer/in-perl-context-is-the-door-that-lets-you-enter-the-land-of-understanding-91415bd4b4e7
- canonical_url
- https://medium.com/@mp.maurer/in-perl-context-is-the-door-that-lets-you-enter-the-land-of-understanding-91415bd4b4e7
- author_url
- https://medium.com/@mp.maurer
- status
- ok
- fetched_at
- 2026-06-20 20:29:01