« Return to Thread: Perl query

Re: Perl reference, referent ...

by Saifi Khan-3 :: Rate this Message:

Reply to Author | View in Thread

On Thu, anirudh nair wrote:

> Hello Guys
>
> As of now, I've not seen any reference to pointers in Perl.
>
> Cheers
> Anirudh Nair
>

Hi Anirudh:

Nice to see you discuss core aspects of PERL.

You are already aware that PERL provides three fundamental data types:
 . scalars     $
 . arrays      @
 . hashes      %

Now each defined variable has a 'name' and the 'address' of the chunk of memory associated with it.

To create a reference, we need to have a value that stores the location of another value.

So, now we need TYPE information and SYNTAX support for the operation.

The SYNTAX is '\' and you can consider it similar to '&' operator in C.

Hence, if you have

  $name = "Anirudh Nair";

then reference is created by

  \$name;

We'd also need TYPE information, since in PERL we're allowed to create 'reference' to scalar, arrays, hashes, functions and even constants.

Thus,
  \$name;
  \@todo;
  \%capitals;
  \∑
  \3.14159;

are reference to scalar name, reference to array todo, reference to hash capitals, reference to function sum and reference to constant.

So, you can see that combining built-in types with references can produce rich and powerful data structures that can be used to represent anything.

To summarize, here are few points

 . The scalar value that contains the memory address is called a reference
   eg.
     $nameref = \$name;

 . The value at the memory address is called a 'referent'.

 . Referents in PERL are typed.

 . There is no way you can 'type cast' from one reference to another.

 . PERL uses reference counting internally to manage the memory associated with each reference.

 . you can use the ref( ) function and pass it a reference, which returns a string describing its reference.

 . if you call ref( ) on an object, it will return the class the object was blessed into, for example Twincling::Alan .

Blog post on the same at
http://www.twincling.org/node/627

PERL is rather nice and Hyderabad is the city of PERLs. ;-)


thanks
Saifi.

 « Return to Thread: Perl query