Last active
October 22, 2020 06:01
-
-
Save worthmine/0dbcb368c73de68c0518ba8edcd9f4c9 to your computer and use it in GitHub Desktop.
In Perl, what is omitted precisely when the variables are declared with omission?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use feature qw(say); | |
say "Firstly, what is returned with defined()?"; | |
say "At line ", __LINE__ + 1, ", if it is defined, ", | |
( local $_ = defined 1 ) ? "true: '$_'" : "false '$_'"; | |
say "At line ", __LINE__ + 1, ", if it is undef, ", | |
( local $_ = defined undef ) ? "true: '$_'" : "false '$_'"; | |
say "At line ", __LINE__ + 1, ", but this false is 'defined false', so 'defined defined undef' is ", | |
( local $_ = defined $_ ) ? "true: '$_'" : "false '$_'"; | |
say "At line ", __LINE__ + 1, ", What is 'defined false'?\nIt is the values that is NOT true but ", | |
defined( local $_ = !1 ) ? "defined as: '$_'" : "undefined"; | |
say '----'; | |
say "Secondly, what is 'undefined'?"; | |
say "At line ", __LINE__ + 1, ", of cource undef is ", | |
defined( local $_ = undef ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", also () is ", # comment for tidied code | |
defined( local $_ = () ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", the nested () is also ", | |
defined( local $_ = ( ( ( () ) ) ) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", the list of empty list is also ", | |
defined( local $_ = ( (), (), () ) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", No matter how complicated, the empty lists is ", | |
defined( local $_ = ( ( () ), ( (), () ), () ) ) ? "defined as: '$_'" : "undefined"; | |
say '----'; | |
say "Thirdly, how does scalar() work?"; | |
my @defined = qw(foo bar); | |
my @omitted; | |
my @definedEmpty = (); | |
say "At line ", __LINE__ + 1, ", the undefined values with scalar() is ", | |
defined( local $_ = scalar(undef) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", even if that was a list of undef, scalar returns ", | |
defined( local $_ = scalar( (undef) ) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", it's also ", | |
defined( local $_ = scalar( undef, undef, undef ) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", No matter how complicated, the scalar() returns ", | |
defined( local $_ = scalar( undef, ( (), undef ), () ) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 2, | |
", because the list with scalar context returned the last value in itself ", | |
defined( local $_ = scalar( 1, undef, ( (), undef ), () ) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 2, | |
", if defined variables were set, it returns how many does it contain in the array.\nSo ", | |
defined( local $_ = scalar(@defined) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", if omitted, it returns ", | |
defined( local $_ = scalar(@omitted) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", it's also ", | |
defined( local $_ = scalar(@definedEmpty) ) ? "defined as: '$_'" : "undefined"; | |
say "At line ", __LINE__ + 1, ", HOWEVER, it returns ", | |
defined( local $_ = scalar( () ) ) ? "defined as: '$_'" : "undefined"; | |
say "Therefore, scalar() knows the value is defined or not"; | |
say '----'; | |
say "Before the conclusion, we have to confirm how do declares work."; | |
sub catch { | |
my $line = ( caller() )[2]; | |
my @list = @_; | |
do { | |
local $_ = shift @list; | |
say "At line $line, the variable is ", defined($_) | |
? "defined as: '$_'" | |
: "undefined"; | |
} while (@list); | |
return wantarray ? @_ : scalar @_; | |
} | |
say "Now we can catch the content of declares"; | |
catch my $defined0 = 'defined'; | |
catch my $defined1 = @defined; | |
catch my $empty = (); | |
catch my $undef = undef; | |
catch my $omitted; | |
catch my @array0 = @defined; | |
catch my @array1 = @omitted; | |
catch my @empty = (); | |
catch my @undef = undef; | |
catch my @omitted1; | |
say '----'; | |
say "Finally, how does my() work?"; | |
say "So, it returns ", ( local $_ = catch my $scalar0 = 'defined' ) ? "'$_'" : "undef"; | |
say "So, it returns ", ( local $_ = catch my $scalar1 = @defined ) ? "'$_'" : "undef"; | |
say "So, it returns ", ( local $_ = catch my $scalar2 = () ) ? "'$_'" : "undef"; | |
say "So, it returns ", ( local $_ = catch my $scalar3 = undef ) ? "'$_'" : "undef"; | |
say "So, it returns ", ( local $_ = catch my $scalar4 ) ? "'$_'" : "undef"; | |
say '----'; | |
say "So, it returns ", ( local $_ = catch my @array2 = @defined ) ? "'$_'" : "undef"; | |
say "So, it returns ", ( local $_ = catch my @array3 = @omitted ) ? "'$_'" : "undef"; | |
say "So, it returns ", ( local $_ = catch my @array4 = () ) ? "'$_'" : "undef"; | |
say "But, it returns ", # Because it's a defined array with one undef. | |
( local $_ = catch my @array6 = undef ) ? "'$_'" : "undef"; | |
say "so, it returns ", ( local $_ = catch my @array5 ) ? "'$_'" : "undef"; | |
say '----'; | |
use Data::Dumper qw(Dumper); | |
say "The Dump of \$scalar2 is ", Dumper \$scalar2; | |
say "The Dump of \$scalar3 is ", Dumper \$scalar3; | |
say "The Dump of \$scalar4 is ", Dumper \$scalar4; | |
say "So, are they whole SAME?"; | |
say '----'; | |
use Devel::Peek; | |
say "I can say Yes! because with using Devel::Peek"; | |
Dump $scalar2; | |
Dump $scalar3; | |
Dump $scalar4; | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment