use warnings; use strict; # a bit of setup for demonstrations sub Ta { return "aaa" } sub T_bb { return "bbb" } # if called as T() or T(undef), return 120, otherwise # if called as T(VALUE), return 123-VALUE sub T { return 123 - (shift//3) } sub bb { return 45 } sub d::T { return "ddd" } sub T::e { return "eee" } sub T::f { return "fff" } # same as: use constant J => 789; sub J () { return 789 } $_ = "some random string ".rand(9999); # now build a hash in which each value is the same as its key my %hash; $hash{Ta} = 'Ta'; # simple string ("bareword"), autoquoted $hash{Ta()} = 'aaa'; # function call Ta() $hash{222} = '222'; # simple integer (not bareword though!) $hash{333.0} = '333'; # not a bareword, numeric literal $hash{0444} = '292'; # not a bareword, octal literal $hash{0x555} = '1365'; # not a bareword, hex literal $hash{T_bb} = 'T_bb'; # simple string, autoquoted $hash{+T_bb} = 'bbb'; # function call T_bb() $hash{"+T_bb"} = '+T_bb'; # string $hash{T.bb} = '12045'; # Perl expression T().bb(), i.e. "120"."45" $hash{T-bb} = '168'; # tricky: Perl expression T(-bb()) $hash{J-bb} = '744'; # parsed as J()-bb() (b/c prototype) $hash{J} = 'J'; # simple string (use J() or +J for func. call) $hash{T-b} = '120'; # tricky: parsed as T(-b $_) (filetest op.) $hash{"T-b"} = 'T-b'; # string $hash{ T_c } = 'T_c'; # simple string, white space ignored $hash{" T_c "} = ' T_c '; # string including whitespace $hash{ T d } = 'ddd'; # parsed as d->T() ("indirect object syntax") $hash{ "T d" } = 'T d'; # string $hash{ T::e } = 'eee'; # function call T::e() $hash{ "T::e" } = 'T::e'; # string $hash{'"T::e"'} = '"T::e"'; # double quotes are chars in the string $hash{ T'f } = 'fff'; # T'f is the old way of writing T::f, function call T::f() $hash{"T'f"} = 'T\'f'; # string my $var1 = 'T::g'; # string $hash{$var1} = 'T::g'; # contents of $var1 is the key my $var2 = 'T::h'; # string $hash{"$var2"} = 'T::h'; # $var2 is interpolated into string which is then the key $hash{'$var2'} = '$var2'; # single quotes don't interpolate $hash{$var1.$var2} # Perl expression = 'T::gT::h'; $hash{"abc$var2"."x"} # interpolating into longer string = 'abcT::hx'; my $var3 = '"T::i"'; # double quotes are chars in the string $hash{$var3} = '"T::i"'; # contents of $var3 is the key my $var4 = 'T::j'; # string $var4 = '"'.$var4.'"'; # adding double quotes around the string $hash{"$var4"} = '"T::j"'; # $var4 is interpolated into string which is then the key my $var5 = q{T::k$/'"[}; # string with "special" chars (q{} doesn't interpolate) $hash{$var5} = $var5; # no special characters take effect here # debug output use Data::Dump; dd \%hash; # double-check that all values are equal to keys use Test::More tests=>32; is $_, $hash{$_}, qq{"$_"} for values %hash; #### use Data::Dump; my $var = '"T::c"'; dd $var; # $var eq "\"T::c\"" $var =~ s/^"(.*)"$/$1/; dd $var; # $var eq "T::c"