简体   繁体   English

如何在Perl中添加斜杠分隔符

[英]how to add slash separator in perl

I have the following command 我有以下命令

how could I add "\\" (path) between v1 and bin in perl to say : c:\\path\\bin as v1 = c:\\path 我如何在v1和perl中的bin之间添加“ \\”(路径),以说: c:\\path\\bin as v1 = c:\\path

system(dirname($v1) . $bin . " " . $trinp);

could someone recommend tutorial for perl? 有人可以推荐Perl教程吗?

Thanks 谢谢

If you are trying to create a path, using File::Spec is a good idea. 如果您尝试创建路径,则使用File::Spec是个好主意。 It is a core module, so it will already be on your system. 它是一个核心模块,因此它将已经在您的系统上。

use strict;
use warnings;
use File::Spec;

my @path = qw(C: path bin);
my $path = File::Spec->catdir(@path);
print $path;

Output in Windows: Windows中的输出:

C:\path\bin

If you wish to emulate another OS, you can select the appropriate module for it, eg 如果要模拟另一个操作系统,则可以为其选择合适的模块,例如

use File::Spec::Win32;
use File::Spec::Unix;

As for myself, if I use the Unix version, I get the output: 至于我自己,如果我使用Unix版本,则会得到输出:

C:/path/bin

(Note for forward slashes) (注意正斜杠)

You have asked a question that has multiple answers. 您提出的问题有多个答案。 To just put a backslash in to form a Windows path, you need to escape the backslash thus, and this will solve your direct problem: 只需添加反斜杠以形成Windows路径,就需要转义反斜杠,这将解决您的直接问题:

system($v1 . "\\" . $bin . " " . $trinp);

However, there are a few refinements. 但是,有一些改进。 Firstly, you can use Perl's interpolation to make it easier to read: 首先,您可以使用Perl的插值法使其更易于阅读:

system("$v1\\$bin $trinp");

You can also use Unix-style forward slashes, as Windows understands these, and prevents so-called "backslashitis": 您也可以使用Unix风格的正斜杠,因为Windows可以理解这些斜杠,并可以防止所谓的“反斜杠”:

system("$v1/$bin $trinp");

A better way however is to use Path::Class to generate paths on any operating system. 但是,更好的方法是使用Path :: Class在任何操作系统上生成路径。

Further, there is a potential security problem in your code that can be caused if $trinp (or any of the other variables) contains shell metacharacters such as | 此外,如果$ trinp(或任何其他变量)包含shell元字符(例如| ),则可能导致代码中存在潜在的安全问题。 to pipe into another command. 传递给另一个命令。 If you provide a list of arguments to the system() function, it will pass those directly to the command instead of the shell. 如果为system()函数提供参数列表,它将参数直接传递给命令而不是shell。 You will probably also want to check if the command was successful, and do something useful (eg die()ing) if it failed: 您可能还需要检查命令是否成功,如果失败,请执行一些有用的操作(例如die()ing):

system("$vi/$bin", $trinp) == 0
   or die "Couldn't run $vi/$bin: $!";

I'll leave it as an exercise for you to convert my final example to use Path::Class. 我将把它留作练习,让您将我的最终示例转换为使用Path :: Class。

[Edited to correct misuse use of system(), which returns the status of the command that was run which is zero for success. [已编辑,以更正对system()的滥用,它返回已运行命令的状态,该状态为0表示成功。 I confused it with more common other Perl functions which return nonzero for success.] 我将其与其他更常见的Perl函数(成功返回非零值)相混淆。

There are a great many very dubious Perl tutorials out there on the internet. 互联网上有很多非常可疑的Perl教程。 The Perl Tutorial Hub is a good place to find better quality ones. Perl Tutorial Hub是查找质量更好的地方的好地方。

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

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