Challenge 3: The open-or-die Idiom — Possible Solution ==================================================================== use strict; use warnings; open(my $fh, '<', 'this_file_does_not_exist.txt') or die "Can't open this_file_does_not_exist.txt: $!"; # 'or' is used here rather than '||' because of PRECEDENCE, not # meaning — 'or' has deliberately LOW precedence, lower than the # assignment happening inside open()'s own argument list. This # guarantees the whole open(...) call is evaluated completely first, # and only its overall true/false result is what 'or' actually # checks. '||' has much higher precedence and can end up binding # more tightly around part of the surrounding expression than # intended in exactly this pattern, which is why this specific idiom # always uses 'or', not '||'. Output (since the file doesn't exist): Can't open this_file_does_not_exist.txt: No such file or directory WHY THIS WORKS AS AN ANSWER ------------------------------ open(my $fh, '<', 'this_file_does_not_exist.txt') or die "..."; reuses the chapter's own open-or-die idiom exactly, including the $! special variable inside the die message, which the chapter's own example shows being interpolated to report the actual underlying system error (here, the file genuinely not existing). The explanation reuses the chapter's own reasoning almost word for word: 'or's low precedence is what makes this idiom work correctly — per the chapter's own description, it "means the whole open(...) call binds tighter than the or," so this line reads as a single, correctly grouped unit: "(try to open the file) or (die with this message)." Swapping in || instead risks it binding too tightly around pieces of the open() call's own arguments rather than around the whole call's result, exactly the "real gotcha in exactly this pattern" the chapter warns about — which is why real Perl code consistently uses or here, never ||, even though both operators mean the same logical thing.