2

为什么LEN从过去的Dump80 年来到这里?

#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.1;
use Devel::Peek;

my $a = 'a';
Dump $a;

print 'Enter an "a": ';
$a = <>;
chomp $a;
Dump $a;

my $b = 'a';
Dump $b;

print 'Enter an "a": ';
my $c = <>;
chomp $c;
Dump $c;

输出:

SV = PV(0x125b090) at 0x127e018
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x126df80 "a"\0
CUR = 1
LEN = 16
Enter an "a": a
SV = PV(0x125b090) at 0x127e018
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x126df80 "a"\0
CUR = 1
LEN = 16
SV = PV(0x125b190) at 0x127e0d8
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x1290da0 "a"\0
CUR = 1
LEN = 16
Enter an "a": a
SV = PV(0x125b1a0) at 0x128f260
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x1285c70 "a"\0
CUR = 1
LEN = 80
4

1 回答 1

3

因为在您的定义中$c,您从 STDIN 设置了它,解释器可能为该操作预先分配了一些空间,以减少以后必须分配更多空间的可能性。

一般来说,Perl 像喝醉的水手一样消耗内存,而不是潜在地损害性能。一旦分配了一些内存,除非您的操作系统内存不足,否则它永远不会被释放。

于 2013-01-17T11:02:15.863 回答