The characteristics of the return of the result reverse from the function in Perl



  • For the foreword, we're doing a test code that translates the line into the top register, a little combination of the means of summoning and:

    #!/usr/bin/perl
    $str="Abc";
    $s1=a($str);  $s2=b(a($str));  $s3=c($str);  $s4=b(c($str));
    print "str=$str;  a()=$s1; b(a())=$s2; c()=$s3; b(c())=$s4\n";
    

    sub b { return $[0]; }
    sub a { my($tmp)=$
    [0]; return uc($tmp); }
    sub c { my($tmp)=$_[0]; return $tmp=uc($tmp); }

    The result of the work is perfectly predictable:

    str=abc; a()=ABC; b(a())=ABC; c()=ABC; b(c())=ABC

    And now it's exactly the same thing that we're gonna do to turn your ass on the front, using the function. reverse instead uc:

    #!/usr/bin/perl
    $str="Abc";
    $s1=a($str); $s2=b(a($str)); $s3=c($str); $s4=b(c($str));
    print "str=$str; a()=$s1; b(a())=$s2; c()=$s3; b(c())=$s4\n";

    sub b { return $[0]; }
    sub a { my($tmp)=$
    [0]; return reverse($tmp); }
    sub c { my($tmp)=$_[0]; return $tmp=reverse($tmp); }

    And then we get a completely unexpected result:

    str=Abc; a()=cbA; b(a)=Abc; c()=cbA; b(c)=cbA

    When the output is transferred, the function aas a parameter, as a function b (who just returns the value) we get the wrong line out. However, practically identical c Behave as it was. Actually, this is an award. $tmp=reverse($tmp) at return I figured out when I was struggling with the unexpected return behavior after reverse♪ When I first saw it, I made a seal in the function. band it's exactly the same value as it returns and which is printed as a result.

    UPD: Check. print b(reverse($str)); returns Abc !

    Adding that the result on Perl v5.12.4 was not checked in other versions.

    Question: why when the value came back reverse in case of direct use, the expected behaviour and when this value is transferred as a parameter to another function, the result is that no reverse Not implemented? Is that a gluc or a pitch?



  • The result is absolutely expected and predictable. The reverse function works in a different scalary and scheduling context. In a scheduling context, she turns the list, in a scallar line.

    Now the question. If we have a list of one line, what's going to reverse it? Of course it is.

    On call. b(a()) Perl is trying to understand the context. Function b simply reverts to its first argument and receives a list at the entrance (the same function and the arguments were not specific). Therefore, function a should return the list. From here on, return is returning the list. Which means the contest is a list. In the scheduling context, the argument is considered to be a list and naturally, the line is a list with one element.

    There's no such - there's clearly a polar context.

    P.S. code is terrible. Don't write that. At least use it. use strict; use warnings and my


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2