我已经通过 perldoc 和 O'Reilly 书籍进行了一些挖掘,但还没有找到任何方法来做到这一点。我是否只能使用类似 Readonly 的东西? ?
更新:
我不反对 Readonly。我只是想能够做一些类似于 PHP 的常量 () 的事情。
如果 Perl 有 constant() 的例子:
use constant {
FIELD_EXAMPLE_O => 345,
FIELD_EXAMPLE_L => 25
};
my $var = 'EXAMPLE';
my $c = 'FIELD_' . $var . '_L';
my $value = constant($c);
# $value is 25
如果 Readonly 是最好的方式,那么我将使用它。
最佳答案
Readonly 有什么问题? ?
如果太慢,可以补充Readonly:XS .但是如果你不喜欢 Readonly , 总是有旧的 constant .
use constant PI => 3.14159265;
记住
如果您想在一条语句中创建多个常量,您需要传递一个散列引用。
use constant { PI => 3.14159265
, E => 2.71828183
};
从您的示例来看,只读哈希 没有理由不能做同样的事情。
Readonly::Hash my %field_example => { L => 25, O => 345 };
然后你可以在任何你想拼凑常量的地方使用它:
print "The example is $field_example{$var}\n";
或者你可以这样做:
Readonly::Hash my %field
=> { example => { L => 25, O => 345 }
, name => { L => 'Lion', O => 'ocelot' }
};
这样调用它:
$field{$var}{L};
如果不尝试让一种语言做它更支持以另一种方式做的事情,你可以获得很多里程。
常量但是,如果你想那样做,那么我的建议是下面的 sub 是做同样事情的一种方式(并且避免 eval ):
sub read_constant {
use Symbol qw<qualify_to_ref>;
my $name = join( '', @_ ); # no need to concatenate before passing
my $constant;
# use the first that works: calling package and then "" (main)
for my $pkg ( scalar( caller ), "" ) {
# get symbol reference
my $symb_ref = qualify_to_ref( $name, $pkg );
# get the code slot
$constant = *{$symb_ref}{CODE};
last if $constant;
}
return unless $constant;
# call the sub named
return $constant->();
}
你可以这样调用它:
$value = read_constant( 'FIELD_', $var, 'L' );
最后一件事,您甚至可以在前面进行测试以确保它只是一个全大写字符串:
Carp::croak "Invalid constant name '$name'" if $name =~ /[^\p{UpperCase}_]/;
关于php - Perl 有类似于 PHP 的 constant() 的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1526859/
我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby常量:Content2而不是content2。Aconstantnamestart
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
Ruby是否有逐步调试器,类似于Perl的“perl-d”? 最佳答案 ruby-debug(对于ruby1.8),debugger(对于ruby1.9),byebug(对于ruby2.0)以及trepanning系列都有一个-x或--trace选项。在调试器内部,命令setlinetrace将打开或关闭线路跟踪。这是themanualforruby-debug原来的答案已经修改,因为数据噪声文章的链接,唉,不再有效了。还添加了ruby-debug的后继者 关于ruby-Ruby
假设您在Ruby中执行此操作:ar=[1,2]x,y=ar然后,x==1和y==2。是否有一种方法可以在我自己的类中定义,从而产生相同的效果?例如rb=AllYourCode.newx,y=rb到目前为止,对于这样的赋值,我所能做的就是使x==rb和y=nil。Python有这样一个特性:>>>classFoo:...def__iter__(self):...returniter([1,2])...>>>x,y=Foo()>>>x1>>>y2 最佳答案 是的。定义#to_ary。这将使您的对象被视为要分配的数组。irb>o=Obje
我使用Jekyll运行博客,并认为我会解决RedcarpetMarkdown解释器,因为它是developedandusedbyGitHub.好吧,我只是碰巧遇到了一个错误,去检查问题,然后foundthis.Maintainersays,"Asyouprobablyhavenoticed(harharharhar)Idon'thavetimetomaintainRedcarpetanymore.It'snotapriorityforme(IfindMarkdownthoroughlyboring)andit'snotapriorityforGitHub,becausewenolong
每次我尝试使用“script/runner-eproductionClassName.run”从我的Rails2.2应用程序的lib目录运行任何类时,我都会收到以下错误:/usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/runner.rb:47:/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:89:in`const_missing':uninitializedconstantClassName(NameError)
关于如何使用git设置类似Dropbox的服务,您有什么建议吗?您认为git是解决此问题的合适工具吗?我在考虑使用git+rush解决方案,你觉得怎么样? 最佳答案 检查这个开源项目:https://github.com/hbons/SparkleShare来自项目的自述文件:Howdoesitwork?SparkleSharecreatesaspecialfolderonyourcomputer.Youcanaddremotelyhostedfolders(or"projects")tothisfolder.Theseprojec