Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Behind the Scenes - Perl Variables

by kidd (Curate)
on Apr 19, 2005 at 18:40 UTC ( #449384=perlquestion: print w/ replies, xml ) Need Help??
kidd has asked for the wisdom of the Perl Monks concerning the following question:

Hello Mongers:

Recently I've been learning Java, and right now im looking at how Java variables work.

For example, when you initialize a variable in Java you must first define what kind of values that variable can hold:

int i = 10; short s = 24000;

When you initialize a variable, Java makes a block of space in the memory for that variable knowing how much space it can hold.

But in perl, we have a "universal" variable, for example a string ($) can hold ints, strings, floats, almost anykind of data. So you can do something like this:

my $stringa = 1234; $stringb = "Hello"; $stringa = $stringb; #No cast problem

Knowing this, my question is: How does perl work with variables?

I've been thinking that when perl makes some space for a variables, it still has to keep in mind that the variable might change in size.

I tried to find some article on the web, but without success.

THANKS

If you're a spanish spoken programmer go to my site: Perl en Espaņol

Comment on Behind the Scenes - Perl Variables
Select or Download Code
Re: Behind the Scenes - Perl Variables
by sasikumar (Monk) on Apr 19, 2005 at 18:59 UTC
    Hi

    Ever wondered a beautiful compiler that thinks abt you and cares for you instead of we caring for it. Thats perl. The wonder lies in the data structure that perl uses to alocate the variables. By default for a $variable (i mean a scalar variable) there are two entries that perl has one is the memory location and other is the flag. The memory location can only grow in size but never shrinks.(Dont worry abt the memory management problems thie is the efficient way to do it) So when ever u initialize the variable perl alocates the memeory space to hold the data and also sets the flag. This magic flag has the details abt the type of data. So this is how perl could handle the variables.

    Update: There is one more in the scalar variables data structure thats reference count. This is the value that perl's garbasge collector uses to free up the unwanted space.

    Thanks
    SasiKumar
Re: Behind the Scenes - Perl Variables
by Ovid (Cardinal) on Apr 19, 2005 at 18:59 UTC
Re: Behind the Scenes - Perl Variables
by dragonchild (Archbishop) on Apr 19, 2005 at 19:00 UTC
    Perl uses double indirection. Simply put, a Perl variable actually has two variables - the thing that can hold a number and the thing that can hold a string. (It's actually possible to have the two of them be out of sync internally, though you'll never see it unless you deliberately peek behind the curtain.) So, when you do a "stringy" operation, the variable will act like a string. If you do a "nummy" operation, it will act like a number. It does the itoa() and atoi() conversions for you, grows for you (with realloc() and similar functions), etc etc etc.
Re: Behind the Scenes - Perl Variables
by bofh_of_oz (Hermit) on Apr 19, 2005 at 19:20 UTC
    A really good question it is...

    I couldn't find a definite answer either, but let's think about all the different things one can do with variables in Perl: you can do typecasting with little care what data types you are working with; you can assign references to arrays and hashes to variables, and so on...
    Coming from C background, I can only think of one way to achieve this kind of functionality: allocate memory for the largest type, use a pointer to a variable, and dynamically expand memory later if needed, similarly to a linked list.

    And I am pretty sure there's a better way to do this... probably the way it really is done in Perl :)

Re: Behind the Scenes - Perl Variables
by johnnywang (Priest) on Apr 19, 2005 at 19:27 UTC
    I'm also in the process of learning some perl internals. Besides perlguts, perlxs, the book "Extending and Embedding perl" is a good read. One thing I learnt is the module Devel::Peek, using it you can see some internals of perl variables, for example
    # devel.pl use Devel::Peek; my $a = "test"; Dump $a; my $b = 5; Dump $b; my $c = "123"; Dump $c;
    will give you:
    SV = PV(0x22548c) at 0x18244e8 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x22cadc "test"\0 CUR = 4 LEN = 5 SV = IV(0x1828e5c) at 0x182450c REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 5 SV = PV(0x2254a4) at 0x18244dc REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x22b26c "123"\0 CUR = 3 LEN = 4
    Besides showing the ref count, the magic flags, you can roughly see how the in-memory structure looks like.
Re: Behind the Scenes - Perl Variables
by Joost (Canon) on Apr 19, 2005 at 19:31 UTC
Re: Behind the Scenes - Perl Variables
by Anonymous Monk on Aug 27, 2008 at 17:15 UTC
    Hello Gurus of PERL: I have a similar question regarding PERL scalar variables. I have a program that is processing email headers. It connects to a POP server, pull the email header info, and the aim it to write it to mySQL. Using an email client like thunderbird, all the content is in the message. However, when I use my program to retrieve and display the messages, no matter what the header content is, only about the first 770 characters stored and display. Is there a limit on the number of characters in a PERL scalar variable like $headers ? Thanks, Wes

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://449384]
Front-paged by Grygonos
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-10-25 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls