Operating System - Linux
1753311 Members
6662 Online
108792 Solutions
New Discussion юеВ

Re: using structure on perl scripts

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: using structure on perl scripts

Hi:

use Data::Dumper

...its a module in your core distribution used to "dump" (show) data.

# perldoc Data::Dumper

...will show you details.

The use of:

# perl -MData::Dumper -e ' ... '

...says to use the (M)odule whose name follows, just like 'use Data::Dumper;'.

Regards!

...JRF...
itai weisman
Super Advisor

Re: using structure on perl scripts

thanks ppl,
I would love not to use that complicated module, but I still don't see how can I do that otherwise.(I understood how can I copy a whole array, but I need more than that)
by 'sturct' I mean Data Structure, like what we had on C, before we had C++/Java objects -
in C - for instnace - If I want to describe a person, that has a name,an age and an ID -
typedef struct person
{
char* name;
float age;
int id;
}PERSON;

and then acessing to data for read and write is very easy using the '->' operator, it's like a primitive object. I can also make an array of structures)
I just need a solution for complex data structures, variables with fixed attributs, like forms. (varibles that can be consist of strings, arrays and hashes, the same time to be a value of one array cell, I can acehive it by doing arrays of hashes, but it's not that easy to manage)

thanks
Itai.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: using structure on perl scripts

Those are all builtin perl features. No need for a module there.

What you /can/ do is build your own module, and give it an OO look, so you can do exactly as you say.
If you are seriously going to convert C++ to perl on a larger scale, I'd advice you to read Damien Conway's "Object Oriented Programming in Perl" from Manning books.

For the shor term, perl comes with quite a lot of on-line documentation about this.

Data Structure Cookbook:
# perldoc perldsc

Perl Objects
# perldoc perlobj

Beginner's Object-Oriented Tutorial
# perldoc perlboot

Tom's object-oriented tutorial for perl
# perldoc perltoot

Tom's OO Tutorial for Class Data in Perl
# perldsc perltooc

You can also use man on most systems
# man perldsc

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: using structure on perl scripts

If it's only a C like struct that you want
then there's really no need for the module you chose to import.
Since the transition from Perl4 to Perl5 Perl got references which let you build up even deeply nested LoLs on the fly.
Besides, refernces are the main feature how Perl implements OO.
Unlike other languages Perl's has no formal OO constructs (the widespread "new" for a class method or constructor is merely a convention to make life easier for C++ hackers).
In Perl you only need to "bless" any type of variable reference (it even works with filehandles) into a package (which is only the class'es namespace).
Because of this liberty (which follows Perl's TIMTOWTDI motto) it can make it a bit difficult for programmers comming from more formal OO languages, to whom it may look quite chaotic.
That's why there are modules like Class::Struct.
Although I have never used this module (and haven't read its POD yet) I assume that it merely acts as a nicer interface to creating class data than the rugged Perlish way of creating and initiating a hasref (usually called $self, but that's only another liberal convention) in the class constructor.
If you don't plan to write an OO module than I think Class::Struct isn't of much use for you.
A simple C struct translates to a hash in Perl.
If you need a nested struct then the values of the hash simply need to become references to lists, either arrayrefs (designated by angular brackets []), or hashrefs (designated by curly brackets {}).
You should have a deko at the perldocs procura gave you.
As far as OO programming in Perl is concerned I would aggree that probably the best reference material (apart from perldocs like perlboot, perltoot, perltooc) is the mentioned book by Damian Conway.
Also looking at the implementation of modules by CPAN authors like Damian is very instructive.
Madness, thy name is system administration