2008/8/11 yitzle <[EMAIL PROTECTED]>:
> On Mon, Aug 11, 2008 at 11:40 AM, Rob Dixon <[EMAIL PROTECTED]> wrote:
>> yitzle wrote:
>>> On Mon, Aug 11, 2008 at 9:52 AM, Dermot <[EMAIL PROTECTED]> wrote:
>>>>
>>>> I am trying to make hash that refers to itself like this
>>>>
>>>> my %_HASH;
>>>> %_HASH = (
>>>> typeOne => {
>>>> root => '/path/to/typeOne',
>>>> logfile => $_HASH{typeOne}->{root}.'/logone.log';
>>>> },
>>>> typeTwo => {
>>>> root => '/path/to/typeTwo',
>>>> logfile => $_HASH{typeTwo}->{root}.'/logtwo.log';
>>>> }
>>>> );
>>>>
>>>>
>>>> But nothing is initialised at this point so $_HASH{typeOne}->{root} is
>>>> an uninitialized value when creating and logfile key.
>>>>
>>>> Is there a way around this?
>>>
>>> Not the way you are doing it. This would work, though:
>>>
>>> my %_HASH;
>>> %_HASH = (
>>> typeOne => {
>>> root => '/path/to/typeOne',
>>> logfile => $_HASH{typeOne}->{root}.'/logone.log';
>>> },
>>> typeTwo => {
>>> root => '/path/to/typeTwo',
>>> logfile => $_HASH{typeTwo}->{root}.'/logtwo.log';
>>> }
>>> );
>>
>> - You are a frequent enough poster to know that bottom-posting is the
>> preferred
>> style on this group
>>
>> - What you have posted is identical to the code that the OP said didn't work
>>
>> - Your code won't even compile as it has semicolons where there should be
>> commas, so you cannot possibly have tested it
>>
>> Rob
>>
>
> Sorry for the top-post.
> I did write working code and tested it with Data::Dumper but my
> copy/paste must have somehow screwed up. The code I have in my t.pl
> file is the same as Mr Shawn posted:
>
> my %_HASH = (
> typeOne => {
> root => '/path/to/typeOne',
> },
> typeTwo => {
> root => '/path/to/typeTwo',
> }
> );
>
> $_HASH{ 'typeOne' }->{ 'logfile' } = $_HASH{ 'typeOne' }->{ 'root' } .
> '/logone.log';
> $_HASH{ 'typeTwo' }->{ 'logfile' } = $_HASH{ 'typeTwo' }->{ 'root' } .
> '/logtwo.log';
Phew.
I thought I was loosing my marbles there. I was staring at the screen
for ages trying to find a difference. To be fair, I think the
semi-colon were my balls-up :-/
Yes Rob I am trying to do some OO. It might make sense for what I'm
after (upload different type of data to a remote server). I am making
my way but I think I'll be using the list a bit over the next few
days.
I think I have harnessed some auto vivification as per Shawn email.
I'll show my hand now in case anyone has comments on how to improve my efforts.
In particular I wouldn't mind finding a way to have the 'type'
subroutine/method be able to return (get) $self's type (EG: typeOne,
typeTwo...etc) or set $self's type. Or should I separate the getting
and setting out?
TiA,
Dp.
my %_HASH = (
typeOne => {
root => '/path/to/typeOne',
},
typeTwo => {
root => '/path/to/typeTwo',
}
);
sub new {
my $class = shift;
my $self = [];
$self->[0] = {};
bless($self, $class);
return $self;
}
sub type {
my $self = shift;
if (@_) {
# Setting the type
if (exists $_HASH{$_[0]}) {
$self->[0]->{type} = $_HASH{$_[0]};
$self->[0]->{logfile} =
$_HASH{$_[0]}->{root}.'/'.$_[0].'.log'; # Logfile
}
else {
croak "The type arg \"$_[0]\" is not in the known
types: ", join ' ', keys %_HASH,"\n";
}
}
else {
# Getting the type
$self->[0]->{type};
}
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/