如何在Perl中将变量限定为const / final? [英] How can I qualify a variable as const/final in Perl?

查看:84
本文介绍了如何在Perl中将变量限定为const / final?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在以下情况下,我不想在初始化后在程序中的任何位置更改 $ infilename 的值。

For example, in situations like below, I do not want to change the value of $infilename anywhere in the program after initialization.

my $infilename = "input_56_12.txt";
open my $fpin, '<', $infilename
    or die $!;

...
print "$infilename has $result matches\n";

close $fpin;

什么是正确的方法来确保 $ infilename 不仅会导致警告,还会导致错误?

What is the correct way to make sure that any change in $infilename results in not just warnings, but errors?

推荐答案

use Readonly;
Readonly my $infilename => "input_56_12.txt";

还是使用更新的 Const :: Fast 模块:

use Const::Fast;
const my $infilename => "input_56_12.txt";

这篇关于如何在Perl中将变量限定为const / final?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆