CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Records in Perl



Mar 10, 2000, 10:46 PM

Post #1 of 3 (1647 views)
Records in Perl Can't Post

Hi and thanks for any help I may be given in advance Smile

Does Perl have any data structure similar to records in C and many other languages?

For example, imagine I wished to store a list of details about a number of people in which case I may have a record structure 'Person' and an instance of the structure 'Timothy'. This would allow me to use such commands as:

Timothy.age = 24;
print Timothy.maritalStatus;

Furthermore I could have an array of 'Persons'...yet I am unsure how to do this in Perl.

If this is not supported, what is the best way of emulating the structure? Can you point me in the direction of any appropriate reference material?

Many thanks, all help appreciated.


Mar 13, 2000, 9:39 AM

Post #2 of 3 (1647 views)
Re: Records in Perl [In reply to] Can't Post

This is no longer a "beginner" topic. Smile You want to look at modules (or "classes"), or use a simple hash structure. Here are two examples for you to look at. For further instruction, I'll give resources.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

package Person;
sub new {
my ($class,$name) = @_;
return bless { NAME => $name }, $class;
sub age {
my $self = shift;
return $self->{AGE} if !@_;
return $self->{AGE} = shift;
sub name {
my $self = shift;
return $self->{NAME};

package main;
$foo = new Person "Joe";
print $foo->name, " is ", $foo->age;

For more on classes and objects and methods (object-oriented programming in general), look at the following Perl documentation: perlobj, perltoot, perlbot. Also look at: "perldoc -q struct" for more pointers to these docs.

To use a hash reference, it's very simple:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$person = {};
$person->{NAME} = "Joe";
$person->{AGE} = 18;
print "$person->{NAME} is $person->{AGE}";


Mar 13, 2000, 1:38 PM

Post #3 of 3 (1647 views)
Re: Records in Perl [In reply to] Can't Post

Thanks immensely japhy Smile


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives