->
主要用于解引用或调用方法。
# 哈希引用访问 my $hash_ref = {name => 'Alice', age => 25}; print $hash_ref->{name}; # 输出: Alice # 数组引用访问 my $array_ref = [1, 2, 3]; print $array_ref->[1]; # 输出: 2 # 方法调用 $object->method_name();
=>
本质上是逗号的变体,会自动给左边的标识符加引号。
# 在哈希中,自动给键加引号 my %hash = ( name => 'Bob', # 等价于 'name', 'Bob' age => 30, # 等价于 'age', 30 'city' => 'Beijing' # 显式引号也可以 ); # 在函数调用中 some_function( param1 => 'value1', param2 => 'value2' );
::
用于访问包(模块)中的变量、子程序或类方法。
# 访问包变量 $Package::variable = 10; print $Package::variable; # 调用包中的子程序 Package::subroutine(); # 类方法的调用(在面向对象编程中) My::Class->new(); My::Class::method();
use LWP::UserAgent; # :: 用于模块名,-> 用于方法调用,=> 用于参数传递 my $ua = LWP::UserAgent->new( timeout => 10, agent => 'MyApp/1.0' ); # 复杂的例子 $response = $ua->get('http://example.com' => @headers);
这样设计让 Perl 在数据结构操作和面向对象编程方面更加灵活。