私は次のものを持っています:
#! /usr/bin/perl
use strict;
# Declare some meaningful named constants
use constant {FOO => 2,
BAR => 3,
BAM => 4};
# Define paths between the named entities
my %PATHS = (FOO => {BAR => "Foo->Bar",
BAM => "Foo->Bam"},
BAR => {FOO => "Bar->Foo",
BAM => "Bar->Bam"},
BAM => {FOO => "Bam->Foo",
BAR => "Bam->Bar"});
# Printing out PATHS map does more or less what I expect:
foreach my $src (sort keys %PATHS) {
foreach my $dst (sort keys %{ $PATHS{$src} } ) {
print "$src:$dst\t$PATHS{$src}{$dst}\n";
}
}
# I can't use the constants as parameters
sub findPath($$) {
my $src = shift;
my $dst = shift;
print "src:$src\ndst:$dst\n";
my $path = $PATHS{$src}{$dst};
print defined $path ? "path=$path\n" : "UNDEFINED\n";
}
findPath(FOO, BAR);
私の定数はベアワードとして扱われ、暗黙の引用符が HoH PATHS の宣言内に追加されているようです。関数呼び出し全体でシンボリック キーを使用できるようなマップを定義する簡単な方法はありますか?