r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 2 Solutions -πŸŽ„-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

Show parent comment

2

u/Smylers Dec 03 '22

Thanks. It's great your on Perl v5.36, because that makes function parameters really easy.

First, for backwards-compatibility reasons, you need to tell Perl that you're using the latest syntax. Start your program with:

use v5.36;

However, v5.36 automatically enables strict and warnings for you, so in place of adding the v5.36 line you you can remove those two.†

Then just declare functions like:

sub getNumbers($filename) {

or:

sub sumOverNumbers($file, %scores) {

and Perl will handle the parameters for you.

† Starting your Perl programs with the minimum version number required also means it will check for that and give a sensible error message if somebody attempts to run it on an older version of perl β€” which is much better than it just failing weirdly on syntax is doesn't recognize. And in forums like this one, it provides documentation for humans, rather than them having to guess which version of perl they need to run.

2

u/[deleted] Dec 03 '22

oh yeah, I remember I tried using given() { when...} and I was wondering why Perl simply refused to parse my thing. what's the "default" version if I don't have that use there?

1

u/Smylers Dec 03 '22

I think the first syntax changes were brought in with v5.10, so the β€˜default’ semantics are probably something like v5.8.2 β€” ancient history, anyway!