gz 文件上的 perl 符号链接

gz 文件上的 perl 符号链接

我有一个文件 file1.txt.gz,我正在尝试使用 perl 创建一个到常规文件的符号链接,如下所示

symlink ("file1.txt.gz", orig_file1);

但这似乎不起作用,除非我将 .gz 后缀添加到新文件名,即我不想要的 orig_file1.gz 。

答案1

TL;DR - 总是引用一些东西。

始终在 Perl 代码中使用,这将导致裸字(或裸字,这是两个潜在的子例程,其输出应与串联运算符连接)use strict失败。orig_file1orig_file1gz.

% perl -Mstrict -e 'symlink "x", asdf'
Bareword "asdf" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
% perl -Mstrict -e 'symlink "x", asdf.gz'
Bareword "asdf" not allowed while "strict subs" in use at -e line 1.
Bareword "gz" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
% 

解决方法是正确引用所有术语,而不仅仅是第一个:

#!/usr/bin/env perl
use strict;
use warnings;
symlink "file1.txt.gz", "orig_file1";

这种行为可能与 shell(Perl 与 shell 有点相关)形成鲜明对比,后者让您(有时)不必引用某些内容:

#!/bin/sh
ln -s -- file1.txt.gz orig_file1

虽然这些特别是如果它们是变量应该在 shell 中引用(这是一个使用建议,而不是硬性要求),因为 shell 可能会使用不同 shell 中不同的特殊字符来执行意想不到的操作。

有点相关的语言 TCL 可以让你不用引用东西,因为它有一个非常简单的语法:

file link orig_file1 file1.txt.gz

然而,人们可能应该引用一些内容,尤其是当不熟悉 TCL 的人正在查看代码时:

file link "orig_file1" "file1.txt.gz"

相关内容