Skip to content
JayBeeDe edited this page Oct 2, 2025 · 2 revisions

Perl Cheatsheet

Perl Cheatsheet

define vs exists

Case A: undefined

$params{lol}="blabla"

if ($params{raid}) { print("oui"); }

if (exists($params{raid})) { print("oui"); }

if (defined($params{raid})) { print("oui"); }
print($params{raid} // "default")
default
print($params{raid} || "default")
default

Case B: undef

$params{raid}=undef

if ($params{raid}) { print("oui"); }

if (exists($params{raid})) { print("oui"); }
oui
if (defined($params{raid})) { print("oui"); }
print($params{raid} // "default")
default
print($params{raid} || "default")
default

Case C: 0 value

$params{raid}=0
# same thing for $params{raid}=q{} or $params{raid}=""

if ($params{raid}) { print("oui"); }

if (exists($params{raid})) { print("oui"); }
oui
if (defined($params{raid})) { print("oui"); }
oui
print($params{raid} // "default")
0
print($params{raid} || "default")
default

Case D: empty string

$params{raid}=q{}

if ($params{raid}) { print("oui"); }

if (exists($params{raid})) { print("oui"); }
oui
if (defined($params{raid})) { print("oui"); }
oui
print($params{raid} // "default")

print($params{raid} || "default")
default

Case E: string

$params{raid}="colique"

if ($params{raid}) { print("oui"); }
oui
if (exists($params{raid})) { print("oui"); }
oui
if (defined($params{raid})) { print("oui"); }
oui
print($params{raid} // "default")
colique
print($params{raid} || "default")
colique

Hash vs Scalar

All three of the examples you present behave differently:

Case A: scalar

my $hash = {
    foo => "bar",
};

It creates a hash reference with a single key foo and value bar and assigns it to a scalar named $hash. Values in hash references are accessed by using the arrow operator (->) followed by curly braces and the name of the key; e.g. $hash->{foo}; # bar

Case B: hash & brackets

my %hash = (
   foo => "bar",
);

It creates a hash with single key foo and value bar. Values in hashes are accessed with curly braces and the name of the key; e.g. $hash{foo}; # bar

Case C: hash & curly brackets

my %hash = {
   foo => "bar",
}

It attempts to assign a hash reference to a hash, which effectively makes the anonymous hash reference the key. Since all hash keys in Perl are strings, the key will be something like HASH(0x7f82948e1e18) with a value of undef. If you have use warnings; enabled (as you should), you would see the following warning when this line is executed: Reference found where even-sized list expected at test.pl line [line-number].

Hash References

Initialization

Initialize a hash

Assigning an empty list is the fastest way.

my %hash = ();

Initialize a hash reference

For a hash reference (aka hash_ref or href), assign a reference to an empty hash.

my $hash_ref = {};  # ref will return HASH

The great thing about this is that if before performing an actual assignment, you want to determine (using the ref operator) the type of thingy that a reference is pointing to, you can!... and you can expect it to be a HASH built-in type, because that is what the line above initializes it to be.

If you treat the variable just as any scalar variable; and use the my declaration alone, or assign a value, ref will return the empty string.

my $hash_ref;      # not a hash ref!... just a scalar
my $hash_ref = 0;  # zero

Usage

Use hash references

sub foo {
    my $hash_ref;

    $hash_ref->{key1} = "value1";
    $hash_ref->{key2} = "value2";
    $hash_ref->{key3} = "value3";

    return $hash_ref;
}

my $hash_ref = foo();

print "the keys... ", sort keys %$hash_ref, "...\n";

Create a hash of hashes; via references

The following two solutions are equivalent, except for the way the look. In my opinion the second approach is clearer.

Solution 1
$requiredPatches_href->{$patch}->{os}   = $os;
$requiredPatches_href->{$patch}->{arch} = $arch;
$requiredPatches_href->{$patch}->{info} = $info;
Solution 2
$requiredPatches_href->{$patch}->{os}   = $os;
$requiredPatches_href->{$patch}->{arch} = $arch;
$requiredPatches_href->{$patch}->{info} = $info;

Sources

It might be useful to read the documentation for Perl references: perldoc perlref

https://stackoverflow.com/questions/29493089/assigning-an-hash-structure-to-a-scalar-variable#29493341

https://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/

Clone this wiki locally