我知道如何读取命令行参数,但是我在从管道读取命令输出时遇到了困难.
使用管道连接将数据输出到Rust程序的程序(A):
A | R
Run Code Online (Sandbox Code Playgroud)程序应该逐行消耗数据.
$ pwd | cargo run应打印pwd输出.
要么
$ find . | cargo run应该输出find超过1行的命令输出.
我有这个代码目前在我的perl脚本的一部分(工作正常):
try {
no strict 'refs';
require_module $modules{$action_name};
if ( &{"FFTG::${actiontype}::run"}( $action, $fs, $log ) ) {
$log->res("SUCCESS");
} else {
if ( $action->{mandatory}[0] == 1 ) {
$log->res("FAIL, exiting.");
last ACTION;
} else {
$log->res("FAIL, but not mandatory, continuing..");
}
}
}
catch {
$log->res("MODULE NOT FOUND");
print "no module found for action '$actiontype', unable to process this action\n";
#warn $_;
};
Run Code Online (Sandbox Code Playgroud)
但不幸的是,脚本的输出在输出中间显示了这3个错误(所有行以Exiting子例程开头):
starting transfer ..
no module found for action 'Transfer', unable to process this action …Run Code Online (Sandbox Code Playgroud) 问题:
我正在尝试打印$self对象以了解包含的数据结构,如下面的行并获取错误:
use Data::Dumper;
my $self = shift;
print Dumper($self);
Run Code Online (Sandbox Code Playgroud)
错误:
Can't locate object method "FIRSTKEY" via package "Memoize::Expire" at /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Data/Dumper.pm line 158
Run Code Online (Sandbox Code Playgroud)
版本信息:
$ perl -MData::Dumper -le 'print Data::Dumper->VERSION';
2.121
$ perl -MMemoize::Expire -le 'print Memoize::Expire->VERSION';
1.03
$ perl -V
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
Platform:
osname=linux, osvers=2.6.18-53.1.14.el5pae, archname=i386-linux-thread-multi
uname='linux builder16.centos.org 2.6.18-53.1.14.el5pae #1 smp wed mar 5 12:07:47 est 2008 i686 athlon i386 gnulinux '
config_args='-des -Doptimize=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 …Run Code Online (Sandbox Code Playgroud) 我想替换/test/test1用TEST1:.这就是我的开始:
extern crate regex; // 1.0.1
use regex::Regex;
fn main() {
let regex_path_without_dot = Regex::new(r#"/test/(\w+)/"#).unwrap();
let input = "/test/test1/test2/";
// Results in "test1:test2/"
let result = regex_path_without_dot.replace_all(input, "$1:");
}
Run Code Online (Sandbox Code Playgroud)
我试过用
let result = regex_path_without_dot.replace_all(&input, "$1:".to_uppercase());
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
error[E0277]: the trait bound `for<'r, 's> std::string::String: std::ops::FnMut<(&'r regex::Captures<'s>,)>` is not satisfied
--> src/main.rs:10:41
|
10 | let result = regex_path_without_dot.replace_all(&input, "$1:".to_uppercase());
| ^^^^^^^^^^^ the trait `for<'r, 's> std::ops::FnMut<(&'r regex::Captures<'s>,)>` is not implemented for `std::string::String`
|
= note: required because …Run Code Online (Sandbox Code Playgroud) 我试图在perl中读取一个文件.
我只想打印每行文件的名称players.txt.
我正在使用此代码:
#!/usr/local/bin/perl
open (MYFILE, 'players.txt');
while () {
chomp;
print "$_\n";
}
close (MYFILE);
Run Code Online (Sandbox Code Playgroud)
这会产生此错误:
./program5.pl: line 2: syntax error near unexpected token `MYFILE,'
./program5.pl: line 2: ` open (MYFILE, 'players.txt');'
Run Code Online (Sandbox Code Playgroud)
我正在使用Unix操作系统来执行此操作.我找不到适用于在Perl中读取文件的指南.
我试图检查具有Perl的UNIX系统中是否存在目录.
while (my @row = $sth->fetchrow_array) {
my $id = $row[0];
my $hash = $row[1];
my $direction = '/home/users/' . $hash
if(-d $direction){
print "$direction exists";
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
全局符号"$ direction"需要在Perl.pl第31行显式包名.在Perl.pl第31行的语法错误,""{"语法错误在Perl.pl第35行,靠近"}"执行Perl.pl中止由于编译错误.
在这种情况下,第31行是:
if(-d $direction)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想要做的是比较两个哈希的键,如果hash1中存在hash1的键,则将hash1中的相应值插入hash2的值,并检索修改后的值.
我试图通过从数组中的hash2中分割值(所述值是制表符分隔的字符串)并拼接数组来实现.
我希望输出看起来像这样:
foo bar baz qux
Run Code Online (Sandbox Code Playgroud)
但它会在拼接后生成一个不需要的换行符:
foo bar modified
qux
Run Code Online (Sandbox Code Playgroud)
我怎样才能避免这个换行符?
这是相应的代码:
foreach my $hash1key ( keys %hash1 ) {
if ( exists $hash2{($hash1key)} ) {
my $line = $hash2{$hash1key};
my @results = split /\t/, $line;
splice @results, 2, 1, $hash1{$hash1key};
for ( @results ){
print OUT $_."\t";
}
}
}
Run Code Online (Sandbox Code Playgroud)