我很想学习Raku(Perl 6)及其语法。
我的Ubuntu计算机上已经安装了Perl 5。
vinod@ubuntu-s-1vcpu-1gb-nyc1-01:~$ perl -v
This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi
(with 67 registered patches, see perl -V for more detail)
Copyright 1987-2017, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc …Run Code Online (Sandbox Code Playgroud) use constant TESTVERSION => 2;
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我想根据一些检查更改常量的值。如果条件为真,我应该使用相同的 TESTVERSION,如果为假,我必须使用一些不同的版本。perl 是否可以在运行时更新常量值?
我想知道我的理解对于下面的脚本/逻辑是否正确。
我有节点列表,我需要利用我通过对服务器执行 SSH 所拥有的服务器数量在每个节点上运行某个命令,这意味着该过程应该并行发生。
我有node_list.txt包含节点列表的文件:
node1
node2
.
.
node49
node50
Run Code Online (Sandbox Code Playgroud)
我已经定义了一个数组@hosts中的服务器数量,我应该在其中执行 SSH 并通过将可用服务器中node_file.txt的部分(称为$node_list_X.txt)分成相等数量的部分来执行对每个节点的命令。
一旦我有了这些文件(node_list_1.txt, node_list_2.txt, node_list_3.txt, node_list_4.txt),我将登录到每个已经定义的服务器,并通过node_list_X.txt并行传递文件在每个主机上执行某些命令。
为了并行执行,我使用Parallel::ForkManagerPerl 模块。
所以,让我们在每个主机中说 -
192.168.0.1 -> node_list_1.txt (13 nodes)
192.168.0.2 -> node_list_2.txt (13 nodes)
192.168.0.3 -> node_list_3.txt (12 nodes)
192.168.0.4 -> node_list_4.txt (12 nodes)
Run Code Online (Sandbox Code Playgroud)
将并行运行。
脚本如下:
...
my @hosts = ("192.168.0.1", "192.168.0.2", "192.168.0.3","192.168.0.4");
open(my $node_fh, '<', $node_file)
or die "can't open $node_file: $!";
my @lines …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,当我用变量检查它时,它没有给我预期的结果。通过使用模式匹配机制,它工作正常。
这是 Perl 中的预期行为吗?如果没有,如何在不使用模式匹配的情况下解决此问题。
代码如下:
#!/usr/bin/perl
use strict;
use warnings;
my $day_of_week = "Monday";
if (($day_of_week ne "Monday") or ($day_of_week ne "Tuesday")){
print "In If. Today is $day_of_week.\n";
} else {
print "In else part\n";
}
Run Code Online (Sandbox Code Playgroud)
这应该给出输出:
In else part
Run Code Online (Sandbox Code Playgroud)
但它给了我:
In If. Today is Monday.
Run Code Online (Sandbox Code Playgroud)
当我if使用如下模式匹配更改条件时,它工作正常。
if ($day_of_week !~ /Monday|Tuesday) {
Run Code Online (Sandbox Code Playgroud)
我担心的是为什么字符串匹配不能按条件工作。我在这里做错了什么。
我有一个带有某些值的 HoA。
我只需要来自 HoA 的独特元素。
预期结果:
Key:1
Element:ABC#DEF
Key:2
Element:XYZ#RST
Key:3
Element:LMN
Run Code Online (Sandbox Code Playgroud)
下面是我的脚本:
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;
my %Hash = (
'1' => ['ABC', 'DEF', 'ABC'],
'2' => ['XYZ', 'RST', 'RST'],
'3' => ['LMN']
);
print Dumper(\%Hash);
foreach my $key (sort keys %Hash){
print "Key:$key\n";
print "Element:", join('#', uniq(@{$Hash{$key}})), "\n";
}
sub uniq { keys { map { $_ => 1 } @_ } };
Run Code Online (Sandbox Code Playgroud)
该脚本向我抛出以下错误:
Experimental keys on scalar is now forbidden at test.pl line 19. …Run Code Online (Sandbox Code Playgroud) 我有一个哈希值,如下所示。我想要keys基于其排序(升序)的列表values。
如果两个不同的键具有相同的值,那么它应该keys首先打印较小的值。
让我展示一下我的示例中出了什么问题:
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;
my %hash = (
'11' => 1,
'315' => 4,
'27' => 3,
'60' => 4,
'8' => 3,
'4' => 2
);
my @keys = sort { $hash{$a} <=> $hash{$b} } keys(%hash);
print Dumper(\@keys);
Run Code Online (Sandbox Code Playgroud)
电流输出:
$VAR1 = [
'11',
'4',
'27',
'8',
'315',
'60'
];
Run Code Online (Sandbox Code Playgroud)
我希望打印如下:
$VAR1 = [
'11',
'4',
'8',
'27',
'60',
'315'
];
Run Code Online (Sandbox Code Playgroud)
因为60的值是4且315的值也是 …
我有一个 json 文件,我正在使用 perlJSON模块处理它。
一旦我处理它,我想在其中插入一些内容。
这是我的输入 json 文件:
{
"sequence" : [
{
"type" : "event",
"attribute" : {
"contentText" : "Test Content",
"contentNumber" : "11"
}
}
],
"current" : 0,
"next" : 1
}
Run Code Online (Sandbox Code Playgroud)
下面是我的脚本:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use Data::Dumper;
my $needed = 2;
my $filename = "test_file.json";
my $json_text = do {
open(my $json_fh, "<:encoding(UTF-8)", $filename)
or die("Can't open \$filename\": $!\n");
local $/;
<$json_fh>
};
my $json = JSON->new;
my $data …Run Code Online (Sandbox Code Playgroud) 我的 Oracle VM VirtualBox 中安装了 Centos 8(64 位)。
当我在云会议中听到有关 Perl 7 发布的消息时!一个 Perl 和 Raku Conf,渴望在我的 CentOS 中安装它。
我尝试按照CPAN官方网站中提到的命令安装(作为root)。Perl v5.32
wget https://www.cpan.org/src/5.0/perl-5.32.0.tar.gz
tar -xzf perl-5.32.0.tar.gz
cd perl-5.32.0
./Configure -des -Dprefix=$HOME/localperl
make
make test
make install
Run Code Online (Sandbox Code Playgroud)
早些时候我已经Perl v5.26安装在我的系统中。
[vinod@localhost ~]$ perl -v
This is perl 5, version 26, subversion 3 (v5.26.3) built for x86_64-linux-thread-multi
(with 51 registered patches, see perl -V for more detail)
Copyright 1987-2018, Larry Wall
Perl may be copied only under …Run Code Online (Sandbox Code Playgroud) 我有一个在我的本地系统中运行良好的脚本(Windows 10 中的 Cygwin)。
但是当我在 Linux 机器 x86_64 GNU/Linux 上运行相同的时,这显示以下错误:
Bareword found where operator expected at script.pl line 22, near "s/$regex/$1,/rg"
syntax error at script.pl line 22, near "s/$regex/$1,/rg"
Execution of script.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
这是我的脚本:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $site_name = $ARGV[0];
my $type_of_site = $ARGV[1];
chomp($site_name);
chomp($type_of_site);
my $var = `sh shell_script.sh $site_name $type_of_site`;
#The above shell script gives me following data in $var
#"Result data [[The Incident result is shown with …Run Code Online (Sandbox Code Playgroud) 我有一个散列,其中包含如下数据:
my %hash = (
'150' => {
'priority' => 'High',
'node' => 'Node1',
'delta' => '00:05:00'
},
'170' => {
'delta' => '00:00:30',
'node' => 'Node2',
'priority' => 'Medium'
}
);
Run Code Online (Sandbox Code Playgroud)
我正在使用foreach循环迭代它并生成报告 (.txt) 文件。
所以,我需要的格式如下:
EVENTID NODE DELTA PRIORITY
-------------------- -------------------- -------------------- --------------------
150 Node1 00:05:00 High
170 Node2 00:00:30 Medium
Run Code Online (Sandbox Code Playgroud)
下面是我格式化结果的脚本:
...
...
open(my $fh, '>', "report_file.txt") or die "Cannot open a file : $!";
printf $fh("%-20s %-20s %-20s %-20s\n", 'EVENTID', 'NODE', 'DELTA', 'PRIORITY');
printf $fh("%-20s …Run Code Online (Sandbox Code Playgroud)