Perl是否有类似于PHP的常量()的东西?

Chr*_*anz 5 php variables perl constants

我已经通过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
Run Code Online (Sandbox Code Playgroud)

如果Readonly是最好的方法,那么我将使用它.

Axe*_*man 15

怎么了Readonly

如果它太慢,你可以补充它Readonly:XS.但如果你不喜欢Readonly,总会有更老的constant.

use constant PI => 3.14159265;
Run Code Online (Sandbox Code Playgroud)

只记得

  1. 它们像subs一样工作,因此它们不会在没有工作的情况下插入.
  2. 如果要在一个语句中创建多个常量,则需要传递哈希引用.

    use constant { PI => 3.14159265
                 , E  => 2.71828183
                 };
    
    Run Code Online (Sandbox Code Playgroud)

从你的例子:

从你的例子来看,没有理由说readonly hash不能做同样的事情.

Readonly::Hash my %field_example => { L => 25, O => 345 };
Run Code Online (Sandbox Code Playgroud)

然后你可以在任何你想要拼凑常数的地方使用它:

print "The example is $field_example{$var}\n";
Run Code Online (Sandbox Code Playgroud)

或者你可以这样做:

Readonly::Hash my %field 
    => { example => { L => 25,     O => 345 }
       , name    => { L => 'Lion', O => 'ocelot' }
       };
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

$field{$var}{L};
Run Code Online (Sandbox Code Playgroud)

你可以获得很多里程,不要试图让语言做得更好,支持以另一种方式做.

同源到PHP constant

但是,如果你想这样做,那么我的建议是以下子是一种做同样的方法(并避免使用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->();
}
Run Code Online (Sandbox Code Playgroud)

你这样称呼它:

$value = read_constant( 'FIELD_', $var, 'L' );
Run Code Online (Sandbox Code Playgroud)

最后一件事是,您甚至可以在前面进行测试,以确保它只是一个全部上限字符串:

Carp::croak "Invalid constant name '$name'" if $name =~ /[^\p{UpperCase}_]/;
Run Code Online (Sandbox Code Playgroud)


Bra*_*ert 10

你可以用constant.

use constant PI    => 4 * atan2(1, 1);
use constant DEBUG => 0;

print "Pi equals ", PI, "...\n" if DEBUG;

use constant {
    SEC   => 0,
    MIN   => 1,
    HOUR  => 2,
    MDAY  => 3,
    MON   => 4,
    YEAR  => 5,
    WDAY  => 6,
    YDAY  => 7,
    ISDST => 8,
};

use constant WEEKDAYS => qw(
    Sunday Monday Tuesday Wednesday Thursday Friday Saturday
);

print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\n";
Run Code Online (Sandbox Code Playgroud)

或者你可以使用Readonly.

use Readonly;

# Read-only scalar
Readonly::Scalar     $sca => $initial_value;
Readonly::Scalar  my $sca => $initial_value;

# Read-only array
Readonly::Array      @arr => @values;
Readonly::Array   my @arr => @values;

# Read-only hash
Readonly::Hash       %has => (key => value, key => value, ...);
Readonly::Hash    my %has => (key => value, key => value, ...);
# or:
Readonly::Hash       %has => {key => value, key => value, ...};

# You can use the read-only variables like any regular variables:
print $sca;
$something = $sca + $arr[2];
next if $has{$some_key};

# But if you try to modify a value, your program will die:
$sca = 7;
push @arr, 'seven';
delete $has{key};
# The error message is "Modification of a read-only value attempted"
Run Code Online (Sandbox Code Playgroud)