Skip to content

Instantly share code, notes, and snippets.

@worthmine
Last active October 20, 2020 14:37
Show Gist options
  • Save worthmine/aa05d5a40cef2216afa09a17b15eaf92 to your computer and use it in GitHub Desktop.
Save worthmine/aa05d5a40cef2216afa09a17b15eaf92 to your computer and use it in GitHub Desktop.
In Perl, what is omitted precisely when you declare variables with omitting default values?
use strict;
use warnings;
use feature qw(say);
say "## scalars ##"; # スカラは通る
say "---- omitted ----", my $empty; # 省略
if ( defined $empty ) {
die "it's defined! You are Liar!";
} else {
say "is's not defined";
}
say "---- with () ----", $empty = (); # ()で宣言
if ( defined $empty ) {
die "it's defined! You are Liar!";
} else {
say "is's not defined";
}
say "---- with undef ----", $empty = undef; # undefで宣言
if ( defined $empty ) {
die "it's defined! You are Liar!";
} else {
say "is's not defined";
}
say "## Arrays ##"; # 配列は
say "---- omitted ----", my @empties; # 省略
if (@empties) {
die "it's defined! You are Liar!";
} else {
say "is's not defined";
}
say "---- with () ----", @empties = (); # ()で宣言
if (@empties) {
die "it's defined! You are Liar!";
} else {
say "is's not defined";
}
say "---- with undef ----", @empties = undef; # undefで宣言すると
# @empties = (undef); # の省略と見做されるので
if (@empties) {
die "it's defined! You are Liar!"; # ここで止まる
} else {
say "is's not defined";
}
say "Tests passed";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment