Scalar variable reference
A scalar variable reference points to a scalar variable.
Creating a reference for a scalar variable
To create a scalar variable reference:
my $str = "Hello"; my $str_ref = \$str;
Dereference a scalar variable reference
Use "${}" to dereference the scalar variable reference.
my $str = ${$str_ref};
Simple variable can be dereferenced by simply prefixing them with "$".
my $str = $$str_ref;
Is it worth using a scalar variable reference?
I used a reference when passing a large string to subroutine I think it's faster.
However, in Perl, strings are copied using a technique called copy-on-write, so they are not copied until the strings are actually modified.
So don't worry about large strings, just pass them as subroutine arguments.
Getopt::Long
When programming yourself, you rarely have the opportunity to use a scalar variable reference, but if your library uses them, you need to remember how to use them.
A frequently used module is Getopt::Long.
# Handling command line argument options with GetOptions # Set default value my $enable_cache; my $max_clients = 5; my $type = 'prefork'; # Optional processing GetOptions( 'enable_cache' => \$enable_cache, 'max_clients = i'=> \$max_clients, 'type = s'=> \$type ); Receives non-option arguments my $conf_file = shift;