Hi Rob, On Wednesday 07 January 2004 1:48 pm, Rob Dixon wrote: > "Gary Stainburn" <[EMAIL PROTECTED]> wrote in message [snip] > You're trying to dereference $_BLOCKS as a hash reference. Use > > return sort keys %{"_$key"}; > > and it should work. But note that it won't return the keys from > the lexical ('my') hashes that you've used in your code: only > package ('our') variables are accessible by name.
I would have thought that because the 'list' method is in the Trainset class that it would work, as at that point it's in scope, then simply passing back an array of sorted keys. I certainly don't want to extend the scope of the variables. > > Even so it's a horrible thing to be doing, if only because you > can't 'use strict'. This might be better > > sub list { > my $self = shift; > my %hash = ( > BLOCKS => \%_BLOCKS, > TRAINS => \%_TRAINS, > BOXS => \%_BOXS, > ); > my $key = uc shift; > > print "using '_$key' as hash name\n"; > return sort keys %{$hash{$key}}; > } > I had thought about this, and like that fact that I can make the list method more intelligent, using %hash for access rights etc., but initially I wanted to keep it simple. > but I'd rather see this implemented as an object method with > the hashes stored as $object->{_BLOCKS} etc. As it stands your > Trainset class is working as an object, and you can never have > more than one trainset in your program. > I've also thought of this, although like CGI.pm it's not likely that one program will have multiple trainsets, my main reason is again for simplicity. I'm thinking of when I've got multiple blocks and want to start linking them. addlink is a method for a block object, but will need access to the %_BLOCKS which would be an attribute of the root trainset object. At the moment it's a global class variable which would be easily accessible, but how could I do: my $set=Trainset->new('NYMR'); # create holder my $platform1=$set->block('Platform 1','Block'); # create both platforms my $platform2=$set->block('Platform 2','Block'); my $points=$set->block('Shunt','Points'); # create the points $platform1->addlink('Shunt',0); # link to shunt position 0; $platform2->addlink('Shunt',1); # link to shunt position 1; addlink needs to access $set->{_BLOCKS} to convert 'Shunt' to point to the $points object (asuming that I didn't already have $points defined). The only alternative I can see is to have a method of $set that returns an objectref from it's name, which increases the amount of code to run. > HTH, > > Rob -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>