What does '\' mean in Perl?

I am currently learning about 'dereferencing' in Perl and need your help in understanding what the '\' means in the line below..

$ra = \$a; 

Answers


See perlop.

Unary "\" creates a reference to whatever follows it. See perlreftut and perlref. Do not confuse this behavior with the behavior of backslash within a string, although both forms do convey the notion of protecting the next thing from interpolation.


As I have elsewhere previously written…

Unary \ creates a reference to whatever follows it. Used on a list, it creates a list of references.

Do not confuse this behavior with the behavior of backslash within a string, although both forms do convey the vaguely negational notion of protecting the next thing from interpretation. This resemblance is not entirely accidental.

You can create a reference to any named variable or subroutine with a backslash. You may also use it on an anonymous scalar value like 7 or "camel", although you won’t often need to. This operator works like the & (address-of) operator in C or C⁺⁺— at least at first glance.

Here are some examples:

$scalarref = \$foo;
$constref  = \186_282.42;
$arrayref  = \@ARGV;
$hashref   = \%ENV;
$coderef   = \&handler;
$globref   = \*STDOUT;

The backslash operator can do more than produce a single reference. It will generate a whole list of references if applied to a list.

As mentioned earlier, the backslash operator is usually used on a single referent to generate a single reference, but it doesn’t have to be. When used on a list of referents, it produces a list of corresponding references. The second line of the following example does the same thing as the first line, since the backslash is automatically distributed throughout the whole list.

@reflist = (\$s, \@a, \%h, \&f);     # List of four references
@reflist = \($s,  @a   %h,  &f);     # Same thing

If a parenthesized list contains exactly one array or hash, then all of its values are interpolated and references to each returned:

@reflist = \(@x);                    # Interpolate array, then get refs
@reflist = map { \$_ } @x;           # Same thing

This also occurs when there are internal parentheses:

@reflist = \(@x, (@y));              # But only single aggregates expand
@reflist = (\@x, map { \$_ } @y);    # Same thing

If you try this with a hash, the result will contain references to the values (as you’d expect), but references to copies of the keys (as you might not expect).

Because array and hash slices are really just lists, you can backslash a slice of either of these to get a list of references. Each of the next four lines does exactly the same thing:

@envrefs = \@ENV{"HOME", "TERM"};         # Backslashing a slice
@envrefs = \@ENV{ qw<HOME TERM> };        # Backslashing a slice
@envrefs = \( $ENV{HOME},  $ENV{TERM} );  # Backslashing a list
@envrefs = ( \$ENV{HOME}, \$ENV{TERM} );  # A list of two references

Since functions can return lists, you can apply a backslash to them. If you have more than one function to call, first interpolate each function’s return values into a larger list and then backslash the whole thing:

@reflist = \fx();
@reflist = map { \$_ } fx();                # Same thing

@reflist = \( fx(), fy(), fz() );
@reflist = ( \fx(), \fy(), \fz() );         # Same thing
@reflist = map { \$_ } fx(), fy(), fz();    # Same thing

The backslash operator always supplies a list context to its operand, so those functions are all called in list context. If the backslash is itself in scalar context, you’ll end up with a reference to the last value of the list returned by the function:

@reflist = \localtime();      # Ref to each of nine time elements
$lastref = \localtime();      # Ref to whether it’s daylight savings time

In this regard, the backslash behaves like the named Perl list operators, such as print, reverse, and sort, which always supply a list context on their right no matter what might be happening on their left. As with named list operators, use an explicit scalarto force what follows into scalar context:

$dateref = \scalar localtime();    # \"Sat Nov 13 10:41:30 2010"

And now you know… the rest of the story.

                         (with apologies to the late Paul Harvey)


The Perl documentation is great and thorough, use it early and often. May I recommend perldoc perlreftut, the perl references tutorial, or perldoc perlref for more. Afterwards perldoc perlintro and perldoc perltoc or perldoc perl to see where to go from there.

And yes it creates a reference to the variable $a and stores it in the variable $ra.


It creates a reference to the variable on the right hand side; kind of like a pointer in C (without the pointer arithmetic) or a pointer in Java.

See perldoc perlref for a lot more information on the subject.

Dereferentiation is done via $$a for scalars, @$a for arrays, etc.


Need Your Help

WPF Color Picker Implementation

c# wpf

I have to create a color picker in my WPF application. When I click on any color, the code of that color should come in a textbox. I googled a lot but found nothing matching my requirement. Please ...