Skip to main content

Perl - References



Perl - References
A Perl reference is a scalar data type that holds the location of another value which could be scalar, arrays, or hashes. Because of its scalar nature, a reference can be used anywhere a scalar can be used.
You can construct lists containing references to other lists, which can contain references to hashes, and so on. This is how nested data structures are built in Perl.
Create References:
This is simple to create a reference for any variable, subroutine or value by prefixing it with a backslash as follows:
$scalarref = \$foo;
$arrayref  = \@ARGV;
$hashref   = \%ENV;
$coderef   = \&handler;
$globref   = \*foo;
You can not create a reference on an I/O handle (filehandle or dirhandle) using the backslash operator but a reference to an anonymous array can be created using square brackets as follows:
 $arrayref = [1, 2, ['a', 'b', 'c']];
Similar way you can create a reference to an anonymous hash using curly brackets as follows:
$hashref = {
         'Adam'  => 'Eve',
         'Clyde' => 'Bonnie',
};
A reference to an anonymous subroutine can be created by using sub without a subname as follows:
$coderef = sub { print "Boink!\n" };
Dereferencing:
Dereferencing returns the value a reference points to the location. To dereference a reference simply use $, @ or % as prefix of the reference variable depending on whether reference is pointing to a scalar, array, or hash. Following is the example to explain the concept:
#!/usr/bin/perl

$var = 10;

# Now $r has reference to $var scalar.
$r = \$var;

# Print value available at the location stored in $r.
print "Value of $var is : ", $$r, "\n";

@var = (1, 2, 3);
# Now $r has reference to @var array.
$r = \@var;
# Print values available at the location stored in $r.
print "Value of @var is : ",  @$r, "\n";

%var = ('key1' => 10, 'key2' => 20);
# Now $r has reference to %var hash.
$r = \%var;
# Print values available at the location stored in $r.
print "Value of %var is : ", %$r, "\n";
When above program is executed, it produces following result:
Value of 10 is : 10
Value of 1 2 3 is : 123
Value of %var is : key220key110
If you are not sure about a variable type then its easy to know its type using ref, which returns one of the following strings if its argument is a reference. Otherwise, it returns false:
SCALAR
ARRAY
HASH
CODE
GLOB
REF
Let's try following example:
#!/usr/bin/perl

$var = 10;
$r = \$var;
print "Reference type in r : ", ref($r), "\n";

@var = (1, 2, 3);
$r = \@var;
print "Reference type in r : ", ref($r), "\n";

%var = ('key1' => 10, 'key2' => 20);
$r = \%var;
print "Reference type in r : ", ref($r), "\n";
When above program is executed, it produces following result:
Reference type in r : SCALAR
Reference type in r : ARRAY
Reference type in r : HASH
Circular References
A circular reference occurs when two references contain a reference to each other. YOu hace to be careful while creating references otherwise a circular reference can lead to memory leaks. Following is an example:
#!/usr/bin/perl

 my $foo = 100;
 $foo = \$foo;

 print "Value of foo is : ", $$foo, "\n";
When above program is executed, it produces following result:
Value of foo is : REF(0x9aae38)
References to Functions
This might happen if you need to create a signal handler so you can produce a reference to a function by preceding that function name with \& and to dereference that reference you simply need to prefix reference variable using ampersand &. Following is an example:
#!/usr/bin/perl

# Function definition
sub PrintHash{
   my (%hash) = @_;
  
   foreach $item (%hash){
      print "Item : $item\n";
   }
}
%hash = ('name' => 'Tom', 'age' => 19);

# Create a reference to above function.
$cref = \&PrintHash;

# Function call using reference.
&$cref(%hash);
When above program is executed, it produces following result:
Item : name
Item : Tom
Item : age
Item : 19

Comments

Popular posts from this blog

Hard dependency with ip address Oracle RAC Cluster.

Command error out due to hard dependency with ip address [-Node1]/app/grid/oracle/product/11.2.0/grid/bin>./crsctl relocate resource RDBMS_DB -n Node2 CRS-2527: Unable to start 'RDBMS_DB' because it has a 'hard' dependency on 'sDB' CRS-2525: All instances of the resource 'sDB' are already running; relocate is not allowed because the force option was not specified CRS-4000: Command Relocate failed, or completed with errors. [-Node1]/app/grid/oracle/product/11.2.0/grid/bin>./crsctl relocate resource sDB  -n Node2 CRS-2529: Unable to act on 'DB' because that would require stopping or relocating 'LISTENER_DB', but the force option was not specified CRS-4000: Command Relocate failed, or completed with errors. [-Node1]/app/grid/oracle/product/11.2.0/grid/bin>./crsctl relocate resource LISTENER_DB  -n Node2 CRS-2527: Unable to start 'LISTENER_DB' because it has a 'hard' dependency on 'sD...

19C NID ( Rename Database)

 [oracle@localhost ~]$ nid DBNEWID: Release 19.0.0.0.0 - Production on Thu Dec 23 00:05:36 2021 Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved. Keyword     Description                    (Default) ---------------------------------------------------- TARGET      Username/Password              (NONE) DBNAME      New database name              (NONE) LOGFILE     Output Log                     (NONE) REVERT      Revert failed change           NO SETNAME     Set a new database name only   NO APPEND      Append to output log           NO HELP        Displays these messages    ...

ORA-01017/ORA-28000 with AUDIT_TRAIL

With default profile in Oracle 11g, accounts are automatically locked 1 day ( PASSWORD_LOCK_TIME ) after 10 failed login attempt ( FAILED_LOGIN_ATTEMPTS ): SQL > SET lines 200 SQL > SET pages 200 SQL > SELECT * FROM dba_profiles WHERE PROFILE = 'DEFAULT' ORDER BY resource_name; PROFILE                         RESOURCE_NAME                      RESOURCE LIMIT ------------------------------ -------------------------------- -------- ---------------------------------------- DEFAULT                         COMPOSITE_LIMIT                 ...