如何在 Perl 中检测操作系统,然后有条件地执行命令?

发布于 2024-10-19 20:59:37 字数 248 浏览 7 评论 0原文

在“如何在 Perl 中检测操作系统中找到了这个问题”——但是我需要检测操作系统的 Perl 片段,然后使用该值来有条件地执行 OS-A 或 OS-B 的代码;在本例中,它们是 Windows7 和 CentOS-5.5。

问题、反馈、请求——只需评论,谢谢!

Found this question on "How can I detect the operating system in Perl" -- but what I need to snippet of Perl that detects the OS, and then uses that value to conditional excute code for OS-A or OS-B; in this case, those being Windows7 and CentOS-5.5.

Questions, feedback, requests -- just comment, thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对岸观火 2024-10-26 20:59:37

您可以使用这样的调度表:

my %callback = (
    MSWin32 => sub { win_code() },
    linux   => sub { linux_code() },
);

# run appropriate code
$callback{$^O}->();

You can use a dispatch table like this:

my %callback = (
    MSWin32 => sub { win_code() },
    linux   => sub { linux_code() },
);

# run appropriate code
$callback{$^O}->();
错爱 2024-10-26 20:59:37

看看 File::Spec 的作用它。

这里。


评论答案:

我是没有说加载 File::Spec。我是说看看它是如何进行“子分类”的。实际上,它有点像这样:

  • 对象具有多态性。
  • 多态性是满足您需要的最简单的方法。
  • 创建一个通用的实现模式,
  • 然后为您将使用它的平台实现它。

File::Spec 尝试完成所有这些任务,因为它是一个 CPAN 模块。但只需看看它是如何做到的,看看它如何将平台折叠到箱子中。因为它是核心且推荐的模块,所以按照该模式您可能不会出错。

如果您的平台不适合此模式,那么您将不得不使用其他方法来嗅探该平台。文件系统差异可能不是您需要担心的唯一问题。

确切地说,我对 FS 实现多态性的方式并不完全 100% 满意。但这一种策略。以及具有多态性的处理平台的示例。

仅供讨论:我更喜欢额外的课程。我会将所有 FS 实现都基于一个抽象类。最重要的是,我会将该行为子类化为实现,并且在此之上,我将放置我最喜欢的模式之一 Facade/Factory 类,它是该类的公共面孔。

Take a look at how File::Spec does it.

Here.


Answer to comment:

I'm not saying to load File::Spec. I'm saying look at how it does its "sub-classing". Really, it's kind of like this:

  • Objects do polymorphism.
  • Polymorphism is the simplest way to do what you need.
  • Create a general pattern of implementation,
  • then implement it for those platforms you will be using it on.

File::Spec tries to do them all, because it's a CPAN module. But simply look at how it does it, look at how it folds platforms into cases. Because it's a core and recommended module, you probably can't go that wrong following the pattern.

If your platforms don't fit this pattern, then you are going to have to use additional methods to sniff out the platform. File-system differences might not be the only ones you need to worry about.

I'm not completely 100% happy with the way that FS implements polymorphism, to be exact. But it is a strategy. And an example of handling platforms with polymorphism.

Just for discussion: I prefer an extra class. I would base all the FS implementations on an abstract class. On top of that I would subclass that behavior into implementations and on top of that I would put one of my favorite patterns Facade/Factory class which is the public face of the class.

沉默的熊 2024-10-26 20:59:37

当然,$^O 是这个问题的明确答案。但您可能想看看 Devel::CheckOS 它提供了一个友好的包装器它。

Of course $^O is the definitive answer to this. But you might want to look at Devel::CheckOS which puts a friendly wrapper around it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文