Calling a "scalar" based on the results of a range operator (..) in Perl

So, I think this has something to do with the difference between arrays and lists, but I don't understand what is going on here. Can someone explain how and why Perl treats an expression of a type (1..4)differently than (1, 2, 3, 4)and @{[1..4]}?

$ perl -de1

Loading DB routines from perl5db.pl version 1.31
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 1
  DB<1> x scalar (1,2,3,4)
0  4
  DB<2> x scalar (1..2,3,4)
0  4
  DB<3> x scalar (1,2..3,4)
0  4
  DB<4> x scalar (1,2,3..4)
0  ''
  DB<5> sub foo { (1..4) } # (the actual problem case, except 4 would be a variable)
  DB<6> x scalar foo()
0  ''
  DB<7> sub bar { @{[1..4]} } # (the workaround)
  DB<8> x scalar bar()
0  4
+3
source share
1 answer

" ", , . .. - "--". -- false, false , , true, , false . ,

while (<>) {
  print if /BEGIN/ .. /END/;
}

.. false, . /BEGIN/ , , , /END/. , "END", . , ( , ) "BEGIN" "END".

$., , . -- perldoc perlop.

+11

Source: https://habr.com/ru/post/1757231/


All Articles