YouTip LogoYouTip

Perl References

# Perl References A reference is a pointer. A Perl reference is a scalar type that can point to variables, arrays, hashes (also called associative arrays), or even subroutines. It can be used anywhere in a program. * * * ## Creating References When defining a variable, you can get a reference to that variable by adding a backslash `` before the variable name. For example: ```perl $scalarref = $foo; # Reference to a scalar variable $arrayref = @ARGV; # Reference to a list $hashref = %ENV; # Reference to a hash $coderef = &handler; # Reference to a subroutine $globref = *foo; # Reference to a GLOB handle In arrays, we can use anonymous array references, defined using `[]`: ```perl $aref = [ 1, "foo", undef, 13 ]; Elements of an anonymous array can themselves be anonymous arrays, so we can use this method to construct arrays of arrays, creating arrays of any dimension. ```perl my $aref = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; In hashes, we can use anonymous hash references, defined using `{}`: ```perl $href = { APR => 4, AUG => 8 }; We can also create an anonymous subroutine reference without a subroutine name: ```perl $coderef = sub { print "Tutorial!n" }; * * * ## Dereferencing Dereferencing can be done using `$`, `@`, or `%` depending on the type. Examples are as follows: ## Example ```perl #!/usr/bin/perl $var = 10; # $r is a reference to $var scalar $r = $var; # Print the value of the variable stored in $r print "$var is : ", $$r, "n"; @var = (1, 2, 3); # $r is a reference to @var array $r = @var; # Print the value of the variable stored in $r print "@var is: ", @$r, "n"; %var = ('key1' => 10, 'key2' => 20); # $r is a reference to %var hash $r = %var; # Print the value of the variable stored in $r print "%var is : ", %$r, "n"; The execution result of the above example is: 10 is : 10 1 2 3 is: 1 2 3 %var is : key110key220 If you are unsure of the variable type, you can use **ref** to determine it. The return value list is as follows; if none of these values are returned, it returns false: SCALAR ARRAY HASH CODE GLOB REF Example: ## Example ```perl #!/usr/bin/perl $var = 10; $r = $var; print "r Reference Types : ", ref($r), "n"; @var = (1, 2, 3); $r = @var; print "r Reference Types : ", ref($r), "n"; %var = ('key1' => 10, 'key2' => 20); $r = %var; print "r Reference Types : ", ref($r), "n"; The execution result of the above example is: r Reference Types : SCALAR r Reference Types : ARRAY r Reference Types : HASH * * * ## Circular References Circular references occur when two references contain each other. You need to use them carefully, otherwise it can lead to memory leaks, as shown in the following example: ## Example ```perl #!/usr/bin/perl my $foo = 100; $foo = $foo; print "Value of foo is : ", $$foo, "n"; The execution result of the above example is: Value of foo is : REF(0x9aae38) * * * ## Reference to Functions Function reference format: `&` Calling a referenced function format: `&` + the created reference name. Example: ## Example ```perl #!/usr/bin/perl # Function definition sub PrintHash { my (%hash) = @_; foreach $item (%hash) { print "Elements : $itemn"; } } %hash = ('name' => 'tutorial', 'age' => 3); # Create a reference to the function $cref = &PrintHash; # Call the function using the reference &$cref(%hash); The execution result of the above example is: Elements : age Elements : 3 Elements : name Elements : tutorial
← Perl FilesPerl Date Time β†’