Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '\' mean in Perl?

Tags:

perl

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

$ra = \$a;  
like image 997
Roy Avatar asked Nov 13 '10 16:11

Roy


People also ask

What is the meaning of $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }

What does @_ mean in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.

What does $@ meaning in Perl?

The variables are shown ordered by the "distance" between the subsystem which reported the error and the Perl process...$@ is set if the string to be eval-ed did not compile (this may happen if open or close were imported with bad prototypes), or if Perl code executed during evaluation die()d.

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".


2 Answers

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)

like image 186
tchrist Avatar answered Oct 05 '22 04:10

tchrist


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.

like image 32
Eugene Yarmash Avatar answered Oct 05 '22 06:10

Eugene Yarmash