-
Notifications
You must be signed in to change notification settings - Fork 0
Perl


$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$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$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$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$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")
coliqueAll three of the examples you present behave differently:
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
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
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].
Assigning an empty list is the fastest way.
my %hash = ();For a hash reference (aka hash_ref or href), assign a reference to an empty hash.
my $hash_ref = {}; # ref will return HASHThe 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; # zerosub 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";The following two solutions are equivalent, except for the way the look. In my opinion the second approach is clearer.
$requiredPatches_href->{$patch}->{os} = $os;
$requiredPatches_href->{$patch}->{arch} = $arch;
$requiredPatches_href->{$patch}->{info} = $info;$requiredPatches_href->{$patch}->{os} = $os;
$requiredPatches_href->{$patch}->{arch} = $arch;
$requiredPatches_href->{$patch}->{info} = $info;It might be useful to read the documentation for Perl references: perldoc perlref
https://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
If you find any mistake, do not hesitate to open an issue.