gpt4 book ai didi

perl - 我们如何在 Perl 中为 "use"命令使用变量?

转载 作者:行者123 更新时间:2023-12-03 23:33:59 25 4
gpt4 key购买 nike

我正在尝试从用户那里获取输入并在我的运行中包含一些库。

“使用”后可以使用变量吗?

$filepath = "abc/xyz.pm";
$module = "xyz";

use $module;

我收到以下错误。

./abc.pl 第 4 行,“使用 $module”附近的语法错误./abc.pl 的执行由于编译错误而中止。

最佳答案

这是不可能的,但您可以使用 require 解决它:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

BEGIN {
my $module = 'JSON::XS'; # Used as an example
my $path = $module =~ s{::}{/}gr . '.pm';
require $path;
$module->import();
}

say encode_json({a=>12});

更常用的是if但在类似的情况下。

use if $^O eq 'MSWin' => 'Win32::GUI';

关于perl - 我们如何在 Perl 中为 "use"命令使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63843887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com