For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2004 > Problem with objects...hashes and arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with objects...hashes and arrays
Holger Schell

2004-03-26, 11:15 pm

Hi,
please can somebody help me and show me the way to access the single attributes for my object...?
I do not have any clue how to handle the array in a hash and so on.
If u want to ask me why I am using it, also I cannot handle it....do not ask...now really I want to use this structure cause it represents to original datarelationship.
But I would be happy for any help provided by u.

Here is my code example:
sub new
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {
Name => undef,
URLS => [],
Switch => undef,
CodeLine => [
CodeLineName => undef,
Project => [
ProjectURL => undef,
ProjectName =>undef,
Logfile =>undef
]
]
};
bless($self, $class); # but see below
return $self;
}



Best regards,

Holger Schell

SAP AG,
68789 Rot (Germany)
Non-ABAP Production Services
SAP Production
Student

Phone: +49 6227 7- 66814
Fax: +49 6227 78-38366
ROT15, CU.01


Wiggins D Anconia

2004-03-26, 11:15 pm

>
> Hi,
> please can somebody help me and show me the way to access the single

attributes for my object...?
> I do not have any clue how to handle the array in a hash and so on.
> If u want to ask me why I am using it, also I cannot handle it....do

not ask...now really I want to use this structure cause it represents to
original datarelationship.
> But I would be happy for any help provided by u.
>
> Here is my code example:
> sub new
> {
> my $proto = shift;
> my $class = ref($proto) || $proto;
> my $self = {
> Name => undef,
> URLS => [],
> Switch => undef,
> CodeLine => [
> CodeLineName => undef,
> Project => [
> ProjectURL => undef,
> ProjectName =>undef,
> Logfile =>undef
> ]


This last structure, 'Project' doesn't likely do what you want. This
should probably be {} instead of [].

> ]
> };
> bless($self, $class); # but see below
> return $self;
> }
>


Generally it helps if you can help us...

I would suggest doing a lot of reading,

perldoc perldsc
perldoc perllol
perldoc perlreftut
perldoc perlref
perldoc perlboot
perldoc perltoot

Essentially the attributes of your object are referenced through the
object like any other hash reference. So the above docs will get you
started on the learning path for references, then objects. If you
prefer book form the LPORM book from ORA is excellent.

I would also suggest becoming acquainted with the Data::Dumper module to
help you see visually what your structure really looks like.

http://danconia.org
James Edward Gray II

2004-03-26, 11:15 pm

On Mar 26, 2004, at 9:20 AM, Schell, Holger wrote:

> Hi,


Howdy.

> please can somebody help me and show me the way to access the single
> attributes for my object...?


I'll try.

> I do not have any clue how to handle the array in a hash and so on.


You my see a lot of benefit looking over some Perl Object documentation.

> If u want to ask me why I am using it, also I cannot handle it....do
> not ask...now really I want to use this structure cause it represents
> to original datarelationship.
> But I would be happy for any help provided by u.
>
> Here is my code example:
> sub new
> {
> my $proto = shift;
> my $class = ref($proto) || $proto;
> my $self = {
> Name => undef,
> URLS => [],
> Switch => undef,
> CodeLine => [
> CodeLineName => undef,
> Project => [
> ProjectURL => undef,
> ProjectName =>undef,
> Logfile =>undef
> ]
> ]
> };
> bless($self, $class); # but see below
> return $self;
> }


Let's simplify that a bit:

sub new
{
my $class = shift;
my $self = {
Name => undef,
URLS => [],
Switch => undef,
CodeLine => [
CodeLineName => undef,
Project => [
ProjectURL => undef,
ProjectName =>undef,
Logfile =>undef
]
]
};
return bless $self, $class;
}

# below is a sample accessor method which can get or set Name

sub name {
my $shelf = shift;
$self->{Name} = shift if @_;
return $self->{Name};
}

On a side note, all your nested array references look like you meant
them to be hashes. If you did, you'll want to change the [ ]s to { }s
in the constructor.

James

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com