Variable cannot be used in print, but seems to keep reference, what happens.

huangapple go评论55阅读模式
英文:

re.pl: Variable cannot be used in print, but seems to keep reference, what happens

问题

使用 re.pl 来自 Devel::REPL:

eddie@MYPC0:~$ re.pl
$ $a++
0
$ $a++
1
$ $a++
2
$ $a++
3
$ $a++
4
$ $a++
5
$ $a++
6
$ print $a
1
$ $a++
7

print $a 打印出 1,无论我是否初始化变量。看起来它不能打印出 a,它打印出另一种类型的引用,我也不明白如果它失去了引用,为什么打印的值是 1 而不是 0

另一个测试:

eddie@MYPC0:~$ re.pl
$ $a =1
1
$ print $a
1
$ $a++
1
$ print $a
1
$ $a++
2
$ $a++
3
$ print $a
1
$ $a++
4
$ print $a
1
$ $a++
5
$ $a
6
$ print $a
1

还有另一个:

eddie@MYPC0:~$ re.pl
$ my $a=0
0
$ print $a
1
$
$ $a
0
$
英文:

Using re.pl from Devel::REPL:

eddie@MYPC0:~$ re.pl
$ $a++
0
$ $a++
1
$ $a++
2
$ $a++
3
$ $a++
4
$ $a++
5
$ $a++
6
$ print $a
1
$ $a++
7

print $a prints 1 whether I initialize the variable or not. It seems it cannot print $a and it prints another sort of reference, also I don't understand if it loses the reference why is the value printed 1 instead of 0

Another test:

eddie@MYPC0:~$ re.pl
$ $a =1
1
$ print $a
1
$ $a++
1
$ print $a
1
$ $a++
2
$ $a++
3
$ print $a
1
$ $a++
4
$ print $a
1
$ $a++
5
$ $a
6
$ print $a
1

And yet another

eddie@MYPC0:~$ re.pl
$ my $a=0
0
$ print $a
1
$
$ $a
0
$

答案1

得分: 5

re.pl正在显示*print的返回值*。如果成功打印了内容,print内建函数会“返回true”(即1)。

andrew@goron:~$ re.pl
$ $a = 42;
42
$ print $a;
1
$ print 0;
1
$ print "banana";
1

你看不到print本身的任何输出,原因是Devel::REPL会缓冲输出直到获得换行符:

andrew@goron:~$ re.pl
$ print "Hello";
1
$ print ", World!\n";
Hello, World!
1
英文:

re.pl is showing you the return value of print. The print builtin "returns true" (i.e. 1) if it was able to successfully print something.

andrew@goron:~$ re.pl
$ $a = 42;
42
$ print $a;
1
$ print 0;
1
$ print "banana";
1

The reason you don't see any output from print itself is because Devel::REPL buffers output until it gets a newline:

andrew@goron:~$ re.pl
$ print "Hello";
1
$ print ", World!\n";
Hello, World!
1

huangapple
  • 本文由 发表于 2023年3月20日 22:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定