当我遇到某种情况时,我想在没有错误的情况下退出(我知道断言和失败模块).以下代码退出但失败:
tasks:
- name: Check if there is something to upgrade
shell: if apt-get --dry-run upgrade | grep -q "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded"; then echo "no"; else echo "yes"; fi
register: upgrading
- name: Exit if nothing to upgrade
fail: msg="Nothing to upgrade"
when: upgrading.stdout == "no"
Run Code Online (Sandbox Code Playgroud) 我有两个数组,我想找到一个数组但不是另一个数组的元素:
例如:
@array1 = ("abc", "cde", "fgh", "ijk", "lmn")
@array2 = ("abc", "fgh", "lmn")
Run Code Online (Sandbox Code Playgroud)
我需要最终得到:
@array3 = ("cde", "ijk")
Run Code Online (Sandbox Code Playgroud) 在Perl中检查引用类型时,为什么使用常量 pragma 更好:
use constant HASH => ref {};
die "I expect a hashref" unless $ref_type eq HASH;
Run Code Online (Sandbox Code Playgroud)
而不是硬编码的字符串:
die "I expect a hashref" unless $ref_type eq 'HASH';
Run Code Online (Sandbox Code Playgroud)
有什么优点和缺点(如果有的话)?
我正在尝试测试一段代码($code),它应该确保一次只运行一个程序实例:
#!/usr/bin/perl
# test_lock
use strict;
use warnings;
( my $code = <<'CODE') =~ s/^\s+//gm;
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw(:flock);
# Make sure only one instance of the program is running at a time.
open our $Lock, '<', $0 or die "Can't lock myself $0: $!";
flock $Lock, LOCK_EX | LOCK_NB
or die "Another instance of $0 is already running. Exiting ...\n";
sleep(2);
CODE
my $progfile = '/tmp/x';
open my $fh, '>', $progfile or die …Run Code Online (Sandbox Code Playgroud) 为什么length()说这是4个逻辑字符(我希望它说1):
$ perl -lwe 'print length("")'
4
Run Code Online (Sandbox Code Playgroud)
我想我的期望有些不对劲.:-) 它是什么?
我理解shell变量是当前shell的本地变量,而环境变量(exported 变量)传递给shell分叉的子进程.
当我在双引号内运行Perl单行程序时,我可以从forked perl进程访问(本地)shell变量:
$ FOO=bar
$ perl -we "print qx'echo $FOO'"
bar
Run Code Online (Sandbox Code Playgroud)
这是为什么?