perl function ref
httww//:spw.theitroad.com
The ref
function in Perl is used to return a string containing the type of the referenced object.
Here's an example:
my $scalar = "Hello, world!"; my @array = (1, 2, 3); my %hash = ('key1' => 'value1', 'key2' => 'value2'); my $ref_scalar = \$scalar; my $ref_array = \@array; my $ref_hash = \%hash; print ref($ref_scalar), "\n"; # prints "SCALAR" print ref($ref_array), "\n"; # prints "ARRAY" print ref($ref_hash), "\n"; # prints "HASH"
In this example, the ref
function is used to determine the type of the referenced object. The ref_scalar
variable is a reference to a scalar, so ref($ref_scalar)
returns "SCALAR". The ref_array
variable is a reference to an array, so ref($ref_array)
returns "ARRAY". The ref_hash
variable is a reference to a hash, so ref($ref_hash)
returns "HASH".