Perl variable Explanation

Source: Internet
Author: User

http://www.perlmonks.org/?node_id=933450


Use Strict;use Devel::P eek;my $a;D UMP ($a), $a =4;dump ($a), $a eq "All";D UMP ($a), $a + +;D UMP ($a);p rint $a;D UMP ($a); ##</ code><code># #C: \users\ian>perl TEST.PLSV = NULL (0x0) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy) SV = IV (0x1d9ec1 8) at 0x1d9ec1c refcnt = 1 flags = (padmy,iok,piok) IV = 4SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 flags = (padmy , iok,pok,piok,ppok) IV = 4 PV = 0x1dbdf84 "4"/CUR = 1 LEN = 4SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (Padmy,iok,piok) IV = 5 PV = 0x1dbdf84 "4" CUR = 1 LEN = 4SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,pok,piok,p POK) IV = 5 PV = 0x1dbdf84 "5"/CUR = 1 LEN = 45##</code><code># #SV = NULL (0x0) at 0x1d9ec1c refcnt = 1 Flags = (padmy) ##</code><code># #SV = IV (0X1D9EC18) at 0x1d9ec1c refcnt = 1 flags = (padmy,iok,piok) IV = 4 ##</code><code># #SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,pok,piok,ppok) IV = 4 PV = 0x1dbdf84 "4 "CUR = 1 LEN = 4##</code><code># #SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (Padmy,iok,piok) IV = 5 PV = 0x1dbdf84 "4" CUR = 1 LEN = 4##</code><code># #SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,pok,piok,ppok) IV = 5 PV = 0x1dbdf84 "5"/CUR = 1 LEN = 4

Http://blob.perl.org/tpc/1998/Perl_Language_and_Modules/Perl%20Illustrated/ presents some of the details of how Perl stores and keeps track of variables and their values. It's a bit more complex than the simple model presended in Perldata and the commonly used terms "variable" and " Value ". There is several data structures used and linked to each of the more variables. Depending on the history of the variable, there'll be is various bits of memory allocated and with more or less current Val UEs.

You can follow the evolution of $a at each step in your program by adding a few more Dump calls:

Use Strict, use Devel::P eek; my $a; Dump ($a); $a = 4; Dump ($a); $a eq "13"; Dump ($a); $a + +; Dump ($a); Print $a; Dump ($a); [DOWNLOAD]

which produces:

c:\users\ian>perl test.pl sv = NULL (0x0) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy) SV = IV (0x1d9ec18) at 0x1d9ec1c RE FCNT = 1 flags = (Padmy,iok,piok) IV = 4 SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 flags = (Padmy,iok,pok,piok,ppok) IV = 4 PV = 0x1dbdf84 "4" CUR = 1 LEN = 4 SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,piok) IV = 5 PV = 0x1dbdf84 "4" CUR = 1 LEN = 4 SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,pok,piok,ppok) IV = 5 PV = 0x1dbdf84 "5" CUR = 1 LEN = 4 5[DOWNLOAD]

The first Dump produces:

SV = NULL (0x0) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy)[DOWNLOAD]

At this point, the variable is declared and no value has been assigned. There is a root data structure ALLOCATED:SV, but no memory allocated to store a value:the value is undef and a NULL poin ter value in the SV structure are sufficient to record this.

After assigning a integer value to the variable, Dump produces:

SV = IV (0X1D9EC18) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,piok) IV = 4[DOWNLOAD]

Here a IV structure has been allocated and the assigned value, 4, have been stored in it. The pointer in the SV structure now points to the IV structure:it are not a NULL pointer any more. Flags is set to indicate this variable now contains a valid integer value (IOK and Piok).

Next you does a string comparison, for which Perl needs to convert the variable value to a string. After this Dump shows:

SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,pok,piok,ppok) IV = 4 PV = 0x1dbdf84 "4" CUR = 1 LEN = 4[DOWNLOAD]

The string value could is created, used then discarded, but instead Perl updates the stored value, keeping the string Valu E In case it might is used again. The integer value is still correct, so the variable now has both Values:an integer value and a string value. Data structures is used:the original SV and a PVIV structure which contains the integer value and a pointer to the S Tring value. And the flags is updated to indicate that there is valid integer and string values.

Your next operation is to increment $a. A operation on its integer value. In this case, Perl was frugal and does only enough work to record the result. The integer value is changed the string value was not changed. Rather, the flags indicating a valid string value is turned off. After this, Dump produces:

SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,piok) IV = 5 PV = 0x1dbdf84 "4" CUR = 1 LEN = 4 [DOWNLOAD]

Note that Perl does does bother to the change of the PVIV structure back to an IV structure. The latter is smaller but the saving of memory would being tiny-not enough to justify the work. So, the PVIV structure remains, and it still points to the memory allocated the string value, which still have the old, but no longer correct string value of the variable. It's OK that the string value was incorrect because the POK and Ppok flags are off, so the value won't be used. This wastes some memory, but saves some CPU.

Then you print the value of $a. Again, a string value is needed. While the PVIV structure have a pointer to a string value, the POK and Ppok flags in the SV is off, so this value isn ' t us Ed. Instead, Perl again converts the current integer value to a string and prints that correct value. And, again, Perl saves this value in case it might is used again. So, after printing Dump produces:

SV = Pviv (0x1d91c34) at 0x1d9ec1c refcnt = 1 FLAGS = (padmy,iok,pok,piok,ppok) IV = 5 PV = 0x1dbdf84 "5" CUR = 1 LEN = 4[DOWNLOAD]

Again there is both integer and string values, and they is current and consistent. The IOK, POK, Piok and Ppok flags is all set so, and subsequent operations these value might be used.

Note also that the new string value is in the same bit of memory, the original string value was in. No additional memory is needed, so no new memory is allocated. The original memory was re-used. A lot of work have gone into minimizing expensive operations for the sake of performance.

You might try extending your program, assigning a long string value to $a and then dumping it again. If you do, you would see that the same SV and PVIV structures were on use but the IOK and Piok flags were off and the int Eger value in the Pviv remained unchanged and the long string is contained in newly allocated memory, as it wouldn ' t fit The block originally allocated to the previous single character value.

Perl variable Explanation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.