my $ref= \\%pkg; for my $key ( split /::/, $pkg ) { $ref= \$$ref->{$key}; } $$ref= $pkg;