Backslash before subroutine call

Since I understood the difference between [] and \ in links, I used both the subroutine and the previous one, but when I tried later I thought it should give an error, but below is the program in perl

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @b;
for my $i ( 0 .. 10 ) {
    $b[$i] = \somefunc($i);

}
print Dumper( \@b );

sub somefunc {
    my $n = shift;
    my ( @a, $k );
    for my $j ( 11 .. 13 ) {
        $k = $n * $j;
        push( @a, $k );
    }
    print "a: @a \n";
    return @a;
}

      

gives the result as:

a: 0 0 0 
a: 11 12 13 
a: 22 24 26 
a: 33 36 39 
a: 44 48 52 
a: 55 60 65 
a: 66 72 78 
a: 77 84 91 
a: 88 96 104 
a: 99 108 117 
a: 110 120 130 
$VAR1 = [
      \0,
      \13,
      \26,
      \39,
      \52,
      \65,
      \78,
      \91,
      \104,
      \117,
      \130
    ];

      

I was unable to figure out the way out. Give an explanation.

+3


source to share


1 answer


What's going on here:

You are returning an array from somefunc

.

But you assign it to a scalar. What it does effectively, therefore, is simply putting the last value in the array into a scalar value.

my $value =  ( 110, 120, 130 );
print $value;

      

When you do, $ value is set to the last value in the array. This is what actually happens in your code. See for example perldata

:

List values ​​are denoted by separating individual values ​​with commas (and enclosing the list in parentheses where precedence requires it):    (LIST)

In a context that does not require a list value, the value of what is represented by a list literal is simply the end element value, as for the C-comma operator. For example,

@foo = ('cc', '-E', $bar);

assigns the entire list value to the array @foo

, but

foo = ('cc', '-E', $bar);

assigns the value of the variable $ bar to the scalar variable $ foo. Note that the value of the actual array in scalar context is the length of the array; the following assigns a value between 3 and $ foo:

@foo = ('cc', '-E', $bar);

   $foo = @foo; # $foo gets 3

This is the last case that often occurs because it is a list in a scalar context.

And in your example - the backslash prefix denotes a reference to "- it's pretty much pointless because it's a number reference.

But for a scalar, it might be more meaningful:



my $newvalue = "fish"; 
my $value =  ( 110, 120, 130, \$newvalue );
print Dumper $value;

$newvalue = 'barg'; 
print Dumper $value;

      

gives:

$VAR1 = \'fish';
$VAR1 = \'barg';

      

This is why you get results. The slash prefix means you are getting a result link, not a sub link. The link 130

doesn't really matter.

Usually, when you do the assignment above, you will get a warning about Useless use of a constant (110) in void context

, but this is not the case with the return of the subroutine.

If you want to insert a helper link, you need to add &

, but if you just want to insert the returned array by reference, you either need to:

$b[$i] = [somefunc($i)]

      

Or:

return \@a;

      

+2


source







All Articles