我可以在 perl 中自动装箱单个变量吗?

Can I autobox a single variable in perl?

我在这样的任意变量上使用 autobox as it allows me to do use JSON::Path

use autobox::JSON::Path;
use Mojo::Util qw/dumper/;

my $w = { foo => 'bar', baz => [ 1, 2, 3 ] };

print dumper $w->get('$.baz');

其中 autobox::JSON::Pathhere(仍然是早期代码)。

这意味着自动装箱会影响所有哈希引用和数组引用。

有没有办法让它只影响特定的变量?或者它实际上和做

一样吗
my $w = autobox::JSON::Path::Get->new({ foo => 'bar', baz => [ 1, 2, 3 ] })

就这样了?

这里是如何使用 autobox in combination with JSON::Path 的示例。您可以将 autobox 的使用限制在特定范围内,从而限制在特定变量内,例如:

use strict;
use warnings;
use JSON::Path;
use Mojo::Util qw/dumper/;

sub HASH::get {
    my ($hash, $arg) = @_;
    my $path = JSON::Path->new($arg);
    return $path->get($hash);
}

my $w = { foo => 'bar', baz => [ 1, 2, 3 ] };

{
    use autobox;
    print dumper $w->get('$.baz');
}

输出:

[
  1,
  2,
  3
]