小编Zyz*_*zyx的帖子

Perl:打开还是[代码块]?

我正在运行脚本 A,它将包含文件路径的 ARGV 提供给 perl 脚本 B。这是由

local @ARGV = ($file, $file2, etc.);
do scriptB.pl or die "scriptB has failed";
Run Code Online (Sandbox Code Playgroud)

脚本 B 然后尝试打开文件:

open( my $fh_file, "<", $file )  
  or die "Could not open file '$file' $!"; 
Run Code Online (Sandbox Code Playgroud)

但是,如果文件丢失,我不会在 B 中的“or die”之后收到消息。而是在 A 中收到 do scriptB.pl or die 消息。如果我从 A 中删除“or die”,脚本将继续在 B 默默地死去之后,好像什么都没发生一样。

我想知道有没有办法让 B 打印它的死亡信息?

更好的是,让 B 在无法打开文件后运行代码块的最佳方法是什么?例如,所述代码将写入一个单独的文件,列出哪些文件丢失,以便用户可以轻松地追踪此类错误。

#something like
open( my $fh_file, "<", $file) or { 
print "the file could not be found";
die;
}
Run Code Online (Sandbox Code Playgroud)

我在网上搜索帮助时发现的唯一一件事是有人提到了“or do {}”,但这给了我奇怪的语法错误,所以我不确定我是否正确使用它。

perl

5
推荐指数
1
解决办法
941
查看次数

在已经工作的线程中调用方法时会发生什么?

在它下面运行我的测试代码似乎在它仍在执行时调用线程方法在线程当前执行之间运行方法.我想知道的是究竟发生了什么?

线程是否暂停while循环,执行方法,然后返回循环?它是在while代码的末尾或其间的任何地方吗?

或者该方法是否在另一个单独的线程中执行?还是别的什么?

package test;

public class main {
    public static threed thred = new threed();

    public static void main(String[] args) throws InterruptedException {
        thred.start();
        Thread.sleep(10000);
        while (true) {
            System.out.println("doing it");
            thred.other();
            Thread.sleep(2000);
            thred.x++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

package test;

public class threed extends Thread {
    public int x;

    public threed() {
        x = 0;
    }

    public void run() {
        while (x < 10) {
            System.out.println(x);          
        }   
    }

    public void other() {
        System.out.println("other " + x);
    }
}
Run Code Online (Sandbox Code Playgroud)

java multithreading loops while-loop

3
推荐指数
1
解决办法
108
查看次数

Perl基于底层键排序键

我想知道是否可以根据底层密钥对哈希中的密钥进行排序?

为了澄清,我将如何按字母顺序排序和打印以下内容:

my %hash;
$hash{1}{b};
$hash{2}{c};
$hash{3}{a};
$hash{4}{d};
$hash{5}{e};
Run Code Online (Sandbox Code Playgroud)

perl hash

1
推荐指数
1
解决办法
53
查看次数

正则表达式$ 1变量干扰另一个变量

我一段时间以来一直在努力处理我的代码部分并且无法弄明白.似乎与如何处理$ 1有关,但我找不到任何相关的东西.

正则表达式找到16640021并将其分配给数组中的位置.

my @one;
my @two;
my $articleregex = qr/\s*\d*\/\s*\d*\|\s*(.*?)\|/p; # $1 = article number
my $row = "   7/  1|        16640021|Taats 3 IP10                       |14-03-03|        |    |        1,0000|st |   | 01|    | N|  0|";

    if ($row =~ /$articleregex/g) {
        $one[0] = $1; 
    }
    if ($row =~ /$articleregex/g) { 
        $two[0] = $1;
    }
print $one[0];
print $two[0];
Run Code Online (Sandbox Code Playgroud)

哪个输出

Use of uninitialized value in print at perltest3.pl line 13.
16640021
Run Code Online (Sandbox Code Playgroud)

似乎$ one [0]的指定会以某种方式干扰$ 2 [0]的指定.这对我来说似乎很奇怪,因为这两个变量及其名称不应该以任何方式进行交互

regex perl

1
推荐指数
1
解决办法
57
查看次数

标签 统计

perl ×3

hash ×1

java ×1

loops ×1

multithreading ×1

regex ×1

while-loop ×1