当前位置:网站首页>Perl: 将要执行的外部命令拆分为多行
Perl: 将要执行的外部命令拆分为多行
2022-07-27 06:10:00 【TechForGeek】
注: 文章中的示例代码都在 Linux 平台下运行。
在 Perl 中,可以通过 `` 或 qx// 函数方便的执行一些外部命令,并获取到命令的执行结果,例如下面的 Perl 代码片段可以获取到当前的时间:
#!/usr/bin/perl
use strict;
use warnings;
my $now = `date`;
print "now: $now";输出结果示例:
now: 2022年 04月 09日 星期六 09:09:00 CST有时候,要执行的外部命令参数比较多,都写在一行的话,可读性会比较差;在 Bash 中,我们可以通过在行尾使用反斜杠的方法,把命令拆分为多行,增加可读性:
#!/bin/bash
echo line1 \
line2 \
line3
# 输出结果: line1 line2 line3但是这样的方法,直接用在 Perl 中会有报错,例如下面的代码:
#!/usr/bin/perl
use strict;
use warnings;
my $res = `echo line1 \
line2 \
line3`;
print "$res";在执行后,会有如下报错信息:
sh: 2: line2: not found
sh: 3: line3: not found
line1这是因为 `` 操作符就像是双引号一样,其中的变量、\t 、\n 等会在执行命令前被展开或转义,因此要想写入一个反斜杠 \,就要在代码中写两个反斜杠 \\。示例如下:
#!/usr/bin/perl
use strict;
use warnings;
my $res = `echo line1 \\
line2 \\
line3`;
print "$res";上面的代码执行时就会得到正确的输出:
line1 line2 line3
一种更简单的方法
当你要执行的命令中不需要包含变量或转义字符时,有一种更简单的方法可以将命令拆分为多行,就是使用 q'',参考 qx 的帮助手册:
Using single-quote as a delimiter protects the command from Perl's
double-quote interpolation, passing it on to the shell instead:
$perl_info = qx(ps $$); # that's Perl's $$
$shell_info = qx'ps $$'; # that's the new shell's $$
perldoc -f qx当使用 q'' 来执行外部命令时,将命令拆分为多行,只需要写一个反斜杠就可以了。示例如下:
#!/usr/bin/perl
use strict;
use warnings;
my $res = qx'echo line1 \
line2 \
line3';
print "$res";
# 输出结果: line1 line2 line3一如既往,如果你对文章中的内容有任何疑问,或者是发现文章中有任何错误,都可以通过留言告诉我;如果你喜欢我的文章,欢迎关注我的微信公众号。

边栏推荐
- Pytorch notes: td3
- How MySQL executes query statements
- MySQL quickly compares database table data
- Codeforces Round #787 (Div. 3)(7/7)
- PHP defines the array using commas,
- How to learn C language? This article gives you the complete answer
- How does golang assign values to empty structures
- 高级IO提纲
- The qualities that a technical manager should have (guess)
- Drools (5): drools basic syntax (3)
猜你喜欢

Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort

Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene

C4D动画如何提交云渲染农场快速渲染?

docker安装MySQL8.0.28

把Excel转换成CSV/CSV UTF-8

Digital image processing - Chapter 6 color image processing

火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。

指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟

整体二分?

Working principle analysis of deepsort
随机推荐
零号培训平台课程-1、SQL注入基础
36 - new promise method: allsettled & any & race
? Experiment 7 implementation of PHP management system based on MySQL
Interpretation of deepsort source code (6)
PHP defines the array using commas,
Summary of APP launch in vivo application market
Error in running code: libboost_ filesystem.so.1.58.0: cannot open shared object file: No such file or directory
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
2021 interview questions for php+go of Zhongda factory (2)
【QT】无法在QT创建者中打开包含文件pcap.h(C1083)
Convert Excel to csv/csv UTF-8
Ci framework learning of PHP
二叉树--天然的查找语义(1)基础篇
[Vani has a date] tail on rainy days
py2exe qt界面风格变成了win98解决方案
Word wrap: break word line feed is compatible with browsers
Vscode connection remote server development
Digital image processing - Chapter 6 color image processing
VIM editor deletes all file contents
word-wrap:break-word 换行在各浏览器兼容的问题