简体   繁体   English

判断Perl是否在-e上运行的最佳方法是什么?

[英]What's the best way to tell if your perl's running on -e?

The question is not how to tell in a oneliner. 问题不在于如何用一个单一的线来区分。 If you're writing the code in a one-liner, you know you are. 如果您是单行编写代码, 那么您就知道自己是。 But how does a module, included by -MMy::Module::Name know that it all started from a oneliner. 但是-MMy::Module::Name包含的模块如何知道它们都是从oneliner开始的。

This is mine. 这是我的。 It's non-portable though and relies on UNIX standard commands (although, it can be made portable more or less.) 它是不可移植的,并且依赖于UNIX标准命令(尽管可以或多或少地使其可移植。)

my $process_info = `ps $$ | tail -1`;
my $is_oneliner  
    = $process_info =~ m/perl.*?\s+-[^\P{IsLower}e]*e[^\P{IsLower}e]*\s+/m
    ;

And if you have a snazzier regex, feel free to improve upon mine. 而且,如果您有一个时髦的正则表达式,请随时进行改进。


A couple of people have asked why I would want to do this. 几个人问我为什么要这样做。 brian correctly guessed that I wanted to change export behavior based on whether it's a script, which we can assume has had some amount of design, or whether it's a oneliner where the user is trying to do as much as possible in a single command line. brian正确地猜测到我想基于它是一个脚本(我们可以假设它已经进行了一些设计)还是一个用户试图在单个命令行中尽可能多地执行的oneliner来更改导出行为。

This sounds bad, because there's this credo that exporters should respect other packages--sometimes known as " @EXPORT is EVIL !" 这听起来很糟糕,因为出口商有一个信条,即出口商应该尊重其他包裹-有时被称为“ @EXPORT is EVIL !” But it seems to me that it's a foolish consistency when applied to oneliners. 但在我看来,将其应用于oneliners时,这是愚蠢的一致性。 After all perl itself goes out of it's way to violate the structure of its language and give you easy loops if you ask for them on the command line, I simply want to extend that idea for my operational/business domain. 在所有perl本身都摆脱了它的语言结构,并且如果您在命令行中要求它们时,给了您方便的循环,我只是想将其扩展到我的运营/业务领域。 I even want to apply source filters ( gasp! ) if it helps. 如果有帮助,我什至希望应用源过滤器( 喘气! )。

But this question also suggests that I might want to be a good citizen of Perl as well, because I only to break the community guidelines in certain cases. 但是这个问题也暗示我可能也想成为Perl的好公民,因为我只是在某些情况下违反了社区准则。 It is quite awesome to be able to create major business-level actions just by changing the command line in a batch scheduler rather than writing a whole new module. 仅通过在批处理调度程序中更改命令行,而不是编写一个全新的模块,就能够创建主要的业务级操作,这真是太棒了。 The test cycle is much compressed. 测试周期被大大压缩。

如果从-e运行,则将$0设置为"-e"

Why are you trying to find out if the module was included from the command line? 您为什么要查找是否从命令行包含了该模块? Is there some situation you have where it matters? 在某些情况下,您有什么要紧的吗? Are you doing something odd with imports? 您对进口商品是否有些奇怪? Tell us what you're trying to do and I can probably come up with a better way to do it :) 告诉我们您要做什么,我可能可以想出一种更好的方法:)


Okay, you're asking about exporting. 好的,您正在询问有关导出的问题。 What's the problem you're trying to solve? 您要解决的问题是什么? Which way do you want it? 您想要哪种方式? Extra or less default exports from the command line? 从命令行额外或更少的默认导出? Do you know that you can specify an import list with -M, including an export tag (so, something from %EXPORT_TAGS)? 您是否知道可以使用-M指定导入列表,包括导出标签(因此,%EXPORT_TAGS中的内容)? And if you want an empty export list, you can use -m (lowercase m) instead. 而且,如果您想要一个空的导出列表,则可以使用-m(小写的m)代替。 See the entry for -M/-m in perlrun . 请参阅perlrun中的 -M / -m 条目

You might also be interested in the "modulino" trick where a module file can be both a module and a script. 您可能还对模块模块既可以是模块又可以是脚本的“ modulino”技巧感兴趣。 You can either use it as a regular module, in which case you have access to all of its methods, or call it as a script, in which case it runs. 您可以将其用作常规模块(在这种情况下您可以访问其所有方法),也可以将其作为脚本调用(在这种情况下它可以运行)。 I describe it in my "Scripts as Modules" article for The Perl Journal as well as "How a Script Becomes a Module" on Perlmonks. 我在 Perl Journal 》的“脚本作为模块”一文以及Perlmonks的“脚本如何成为模块”一文中对此进行了描述。

In your import() , the line number returned by caller() will be 0 if your module was loaded via -M . 在您的import() ,如果您的模块是通过-M加载的,则由caller()返回的行号将为0 This is true whenever -M is used (with -e or not) but I think it is the only case where the line number is 0 . 每当使用 -M时(或不使用-e)都是如此,但我认为这是唯一的行号为0

If you want different export behavior, the "clean" way to do it would be using a different module name. 如果您希望使用不同的导出行为,则使用不同的模块名称是“干净”的方法。 If you really expect to do a lot of one-liner use, you can even give it a short name. 如果您真的希望使用很多单线,甚至可以给它起一个简短的名字。 Eg MMN.pm: 例如MMN.pm:

package MMN;
use My::Module::Name '/./';
use Exporter ();
@ISA = 'Exporter';
@EXPORT = @My::Module::Name::EXPORT_OK;
1;

Note that Exporter has a little known regex feature; 请注意,Exporter具有鲜为人知的正则表达式功能。 you may just want to do 你可能只想做

perl -MMy::Module::Name=/./ -e ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM