如何将用户定义的路径保存到变量中?

How to save user define path into a variable?

如何将用户定义的路径保存到 Perl 中的变量中,并在脚本的其他地方使用该变量,这样如果用户提供新路径,它将在每个使用该变量的地方更新路径。

您在这里描述的是变量的标准行为。所以我想知道我是否遗漏了什么。

my $default_path = '/the/default/path';

print "Enter your path [$default_path]: ";
chomp(my $user_path = <STDIN>);

my $path = $user_path // $default_path;

print "The path we'll use is: $path";

# And then, much later in the program...

my $filename = 'somefile.txt';
open my $fh, '>', "$path/$filename"
  or die "Cannot open $path/$filename: $!";