Operating System - Linux
1820475 Members
3254 Online
109624 Solutions
New Discussion юеВ

using structure on perl scripts

 
SOLVED
Go to solution
itai weisman
Super Advisor

using structure on perl scripts

Hi everyone,
I'm trying to write a perl script using structure. (using Class::Struct)
the structure is defined as follows:
struct DiskAllocation =>
[
Disk => '$',
Fa => '@',
];

I'm using the following method to update the array (see also the function below)-
first I create another array,temporary, and then when I finish to fill it out with values, I copy the temporary array into the structure array.
I have two problems (so far...)
1. I cannot 'copy' the temporary array into the structure array, it ask me to use pointers instead. since it's a temporary array (and I want to use it afterwards), it's a problem
2. when attempting to reach data in the structure (from the 'Disk' field, which is an integer) I received the following warning (I still recieve the requested data)

Can't call method "Disk" on unblessed reference at DrpMan.pl line 219.

3. when attempting to read data from the Fa field (which is an array) I keep receving syntax errors, no matter how I try to read it.
this is the function I use to read/write the structure -

attched the section from the script, with the struct defintion and function
13 REPLIES 13
itai weisman
Super Advisor

Re: using structure on perl scripts

I managed to solve some of the problem,
but the biggest problem remains -
I can't assign an Array to structure array -
(by $Struct->new (array => @another_array
only by
$Struct->array=\@another_array)
which is not good!! since I need to clean '@another_array' from values, using refrences is not good for me...
Help me ppl!


itai weisman
Super Advisor

Re: using structure on perl scripts

I managed to solve some of the problems,
but the biggest problem remains -
I can't assign an Array to structure array -
(by $Struct->new (array => @another_array
only by
$Struct->array=\@another_array)
which is not good!! since I need to clean '@another_array' from values, using refrences is not good for me...
Help me ppl!


H.Merijn Brand (procura
Honored Contributor

Re: using structure on perl scripts

$Struct->array = [ @another_array ]

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
itai weisman
Super Advisor

Re: using structure on perl scripts

it doesn't work :(
I receive the following error -
root@troya: perl DrpMan.pl
Can't modify non-lvalue subroutine call at DrpMan.pl line 219, line 149
.
and line 219 -
$returnArray[$DiskCount]->Fa=[@Fa];
can I insert scalars values using the same syntax?
H.Merijn Brand (procura
Honored Contributor

Re: using structure on perl scripts

I don't know what that struct module does, and why you would need it.

if you have an array

my @disks = qw( /dev/dsk/c1t2d0 /media/usbdisk );

putting @disks in square brackets creates an array reference to a list of copies of the elements in @disks

lt09:/home/merijn 101 > perl -MData::Dumper -e'@x=qw(foo bar);print Dumper@x'
$VAR1 = 'foo';
$VAR2 = 'bar';
lt09:/home/merijn 102 > perl -MData::Dumper -e'@x=qw(foo bar);print Dumper\@x'
$VAR1 = [
'foo',
'bar'
];
lt09:/home/merijn 103 >

You could play with Data::Dumper and see what it expects

Still, why the need for struct?

my $ref = (
Array => [ qw( foo bar ) ],
Count => 2,
Info => {
Author => "Tux",
Company => "PROCURA",
ID => "BR41967",
Number => 42,
List => [ 1 .. 16 ],
},
Valid => 1,
);

if ($ref->{Info}{Author} eq "Tux" &&
$ref->{Array}[1] eq "bar" &&
$ref=>{Info}{List}[2] == 2) {
print "See, it's that simple!
$ref->{Extra_Info}{Age}[24] = 42;
}
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
itai weisman
Super Advisor

Re: using structure on perl scripts

thanks,
I need to design a utility to hold a lot of data on real time (not on any kind of third party database) regrading to my storage enviorment. I need to make some queires against this 'database' (we'll use that on a crisis, when we'll have to establish a DRP site ). I started writing this proggram and I came out with some complex data holders such as three dimensional arrays of hashes, I'm looking for a better way, that is easier to manage.
I didn't understand if the soultion u've wrote is an explantion of how using structure or if it's an alternative soultion.
thanks!
itai
itai weisman
Super Advisor

Re: using structure on perl scripts

what is MData::Dumper by the way? I couldn't find a module named that way...
H.Merijn Brand (procura
Honored Contributor

Re: using structure on perl scripts

Data::Dumper is a core module that shows you the real contrent of data structures. See 'man Data::Dumper' for more details.

the M comes from the command line option -M, which is a shorthand for "use Module"
see "man perlrun" for more details.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Marvin Strong
Honored Contributor

Re: using structure on perl scripts

He showed you that you don't need the struct module(whatever it does) for what you are trying to use to accomplish.

By putting your array in [ ] creates an array reference list of copies of your arrays elements.

It sounds confusing buts its really not.



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