- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Perl 的 do EXPR
函数作为穷人的配置解析器,使用仅返回列表作为配置信息的第二个 .pl 文件。 (我认为这可能是 do
的理想用法,尤其是因为我可以在我的代码中编写“do or die
”。)这是一个示例:
main.pl
# Go read the config file
my %config = do './config.pl';
# do something with it
$web_object->login($config{username}, $config{password});
config.pl
# Configuration file for main script
(
username => "username",
password => "none_of_your_business",
favorite_color => "0x0000FF",
);
阅读 Perldoc for do
给出了很多关于相对路径的有用建议——搜索@INC 和修改 %INC,关于 5.26 不搜索“.”的特别警告。等等。但它也有这些位:
# load the exact specified file (./ and ../ special-cased)...
Using do with a relative path (except for ./ and ../), like...
然后它实际上从来没有费心去解释“./”或“../”的特殊情况路径处理——一个重要的遗漏!
所以我的问题都是关于“当你做'./file.pl';
时真正会发生什么”的所有变体?例如……
欢迎任何见解。
最佳答案
好的,所以 - 首先,我不确定你的 config.pl
是否真的是正确的方法 - 对于初学者来说它不是 perl
,因为它不是编译。无论哪种方式,尝试评估内容以“解析配置”通常都不是一个好计划 - 它很容易出现令人不快的故障和安全漏洞,因此应该在需要时保留。
我会敦促您通过以下任一方式以不同的方式进行操作:
像这样:
package MyConfig;
# Configuration file for main script
our %config = (
username => "username",
password => "none_of_your_business",
favorite_color => "0x0000FF",
);
然后您可以在主脚本中:
use MyConfig; #note - the file needs to be the same name, and in @INC
并将其访问为:
print $MyConfig::config{username},"\n";
如果您不能将其放入现有的 @INC
- 这可能有您不能的原因,FindBin
允许您使用相对于脚本位置的路径:
use FindBin;
use lib "$FindBin::Bin";
use MyConfig;
YAML
对于配置文件来说非常可靠,尤其是:
use YAML::XS;
open ( my $config_file, '<', 'config.yml' ) or die $!;
my $config = Load ( do { local $/; <$config_file> });
print $config -> {username};
你的配置文件看起来像:
username: "username"
password: "password_here"
favourite_color: "green"
air_speed_of_unladen_swallow: "african_or_european?"
(YAML 还支持多维数据结构、数组等,不过你似乎不需要这些。)
JSON
看起来很相似,只是输入是:
{
"username": "username",
"password": "password_here",
"favourite_color": "green",
"air_speed_of_unladen_swallow": "african_or_european?"
}
你阅读它:
use JSON;
open ( my $config_file, '<', 'config.json' ) or die $!;
my $config = from_json ( do { local $/; <$config_file> });
您根本不必担心@INC
。您可以简单地使用基于相对路径...但更好的选择是不这样做,而是使用 FindBin
- 它允许您指定“相对于我的脚本路径”并且更强大.
use FindBin;
open ( my $config_file, '<', "$FindBin::Bin/config.yml" ) or die $!;
然后您就会知道您正在读取与您的脚本位于同一目录中的脚本,无论它是从何处调用的。
From whose perspective is "./" anyway: the Perl binary, the Perl script executed, CWD from the user's shell, or something else?
当前工作目录通过进程向下传递。所以默认情况下用户的 shell,除非 perl 脚本执行 chdir
Are there security risks to be aware of?
任何时候你“评估”一些东西就好像它是可执行代码(并且 EXPR
可以)都存在安全风险。它可能并不大,因为脚本将作为用户运行,而用户是可以篡改 CWD
的人。核心风险是:
rm -rf/*
)。也许在 /tmp
中有一个他们不小心“运行”的“config.pl”?
评估
的东西有错字,并且以古怪和意想不到的方式破坏了脚本。 (例如,它可能会重新定义 $[
并从此以后以难以调试的方式扰乱程序逻辑)root
还是其他特权用户。 Is this better or worse than modifying @INC and just using a base filename?
更糟糕的海事组织。实际上根本不修改@INC
,而是使用完整路径,或使用FindBin
的相对路径。并且不要在不必要的时候eval
东西。
关于Perl "do",相对路径以 "."或 ".."开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49787980/
如果我的 Perl 程序使用 Perl 模块,它将如何确定在哪里可以找到包含模块代码的文件? 例如,如果程序包含: use MyModule1; # Example 1 us
我在一个文件中有一些不同格式的数字:8.3、0.001、9e-18。我正在寻找一种简单的方法来读取它们并存储它们而不会损失任何精度。这在 AWK 中很容易,但在 Perl 中是如何完成的呢?我只愿意使
我在一个文件中有一些不同格式的数字:8.3、0.001、9e-18。我正在寻找一种简单的方法来读取它们并存储它们而不会损失任何精度。这在 AWK 中很容易,但在 Perl 中是如何完成的呢?我只愿意使
我正在自学 Perl,并且在我的 Windows 8 64 位系统上安装了 Strawberry。 Strawberry 命令行似乎工作正常,我在 C 驱动器上的 Strawberry 文件夹中创建了
我在 Perl 模块 IO::Socket::SSL 中发现了一个错误,我可能会修复它,但是,我担心测试修复。我从 Debian 下载了源码包(因为我打算为它制作一个 Debian 包或补丁)并查看了
我有一个 perl 文件,它使用了两个 perl 模块 A.pm 和 B.pm。 但是在 B.pm 中我需要调用 A.pm 的子程序。即使我在 A.pm 中使用并尝试使用它,我仍然遇到未定义的错误。
有没有办法在 Perl 运行时加载整个模块?我原以为我用 autouse 找到了一个很好的解决方案,但以下代码无法编译: package tryAutouse2; use autouse 'tryAu
过去,我编写过许多 perl 模块,以及不止一些独立的 perl 程序,但我之前从未发布过多文件 perl 程序。 我有一个几乎处于 beta 阶段的 perl 程序,它将被开源发布。它需要一些数据文
我有 1 个 perl 脚本,我们在其中编写了几个子例程。例子: # Try_1.pl main(); sub main{ --- --- check(); } check { -- --} 现在,
似乎 CPAN 上的一些(很多?)模块部分是使用 XS 在 C 中实现的,如果需要,可以回退到纯 perl 实现。虽然这很聪明,但它显然会损害性能,我想知道它是否会发生,以便我可以解决问题。 有没有一
我对 perl 很陌生。我希望我可以从 perl 安装一些软件包,我这样做是这样的: perl -MCPAN -e 'install VM::EC2' 我猜它由于依赖而失败,它显示: Result:
给定一个 Perl 包 Foo.pm,例如 package Foo; use strict; sub bar { # some code here } sub baz { # more
我有一个用 Perl 编写的测试生成器。它生成连接到模拟器的测试。这些测试本身是用 Perl 编写的,并通过其 API 连接到模拟器。我希望生成的代码是人类可读的,这意味着我希望它能够正确缩进和格式化
我正在学习 Perl,非常新的用户。我可以知道这些 Perl 代码之间有什么区别吗? #!/usr/bin/perl & #!/usr/bin/perl -w 最佳答案 那不是 perl 代码,它是
我不认为这是一个重复的问题。这专门针对 Perl 模块附带的脚本。 通常,在安装多个 Perl 版本时,您可以将 perl 可执行文件标记为版本号 (perl5.32),这样它们就可以在 /whate
我有一个在文件中使用 Blowfish 加密的程序和第二个 perl 程序,它提示输入用于将其解密为字符串的密码,我希望不必将解密的源代码写入硬盘驱动器,尽管将它放在内存中并不是真正的问题,因为运行程
有没有人为 Perl 中的惰性求值列表找到了一个好的解决方案?我尝试了很多方法来改变类似的东西 for my $item ( map { ... } @list ) { } 进入懒惰的评估——例如,通
我安装了多个版本的 Perl。 我已经指定了要使用的版本。但是为了验证,我想从 .pl 脚本本身输出 Perl 的版本。 这可能吗? 在 Perl 脚本中解析“perl --version”的输出似乎
人们还经常问“我怎样才能编译 Perl?”而他们真正想要的是创建一个可以在机器上运行的可执行文件,即使他们没有安装 Perl。 我知道有几种解决方案: perl2exe靛蓝之星 它是商业的。我从未尝试
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
我是一名优秀的程序员,十分优秀!