went to 5.8.1 and now I can't access object data dynamically

S

Sophie Miron

My web host just upgraded from 5.6 to 5.8.1 and a series of scripts
I'd written are no longer working.

I'm using Class::Struct qw(struct) in my object modules. A Schedule
object contains 6 Station objects (among other things). A Station
contains 4 CurrItem objects...

These are defined globally:

@days = ('tue', 'wed', 'thu', 'fri');
@stations = ('art', 'outside', 'puzzles', 'cooking', 'science',
'floater');

In many places I use the following type of loop to access the object
data:

foreach my $class ($AM, $PM) {
foreach my $station (@stations) {
foreach my $day (@days) {
print $class->$station->$day->parent() ; # for example
}}}

I'm now getting an error, cannot perform method XXX on an undefined
value.

Is there a way around this problem, or a better way to code it? I'm
thinking of putting in an extra level of indirection (putting the 6
Station objects in a hash in the struct definition and calling "new"
explicitly).

Is what I'm doing using symbolic references? (the books show that as
using a variable to access a method, here I'm wanting it to see the
variable as a string)

I checked the documentation on deprecations in 5.8 and didn't find
what's causing the problem.

Any help for this newbie would be much appreciated.

Sophie Miron
(e-mail address removed)
 
J

Jay Tilton

(e-mail address removed) (Sophie Miron) wrote:

: My web host just upgraded from 5.6 to 5.8.1 and a series of scripts
: I'd written are no longer working.
:
: I'm using Class::Struct qw(struct) in my object modules. A Schedule
: object contains 6 Station objects (among other things). A Station
: contains 4 CurrItem objects...

[snip]

: foreach my $class ($AM, $PM) {
: foreach my $station (@stations) {
: foreach my $day (@days) {
: print $class->$station->$day->parent() ; # for example
: }}}
:
: I'm now getting an error, cannot perform method XXX on an undefined
: value.

So one of the methods is returning undef when you expect it to return an
object.

From the Class::Struct modification history,

Modified by Damian Conway, 2001-09-10, v0.62.
[...]
Also default initializes nested object attributes to
undef, rather than calling object constructor without
args

That's the most likely reason for your old code not to work
anymore--upgrading your Perl also upgraded your Class::Struct module,
and your code relies on Class::Struct to initialize nested objects.

I wonder what the reason was for making that change to Class::Struct.

: Is there a way around this problem, or a better way to code it?

Backpedal to an older Class::Struct version, or have your code
initialized the nested objects itself.

: Is what I'm doing using symbolic references?

Technically, yes, but Perl resolves method names symbolically anyway.
Pay no attention to that man behind the curtain.
 
S

Sophie Miron

Thanks Jay. That was exactly the problem.

So one of the methods is returning undef when you expect it to return an
object. [snip]
I wonder what the reason was for making that change to Class::Struct.

It says that it caused problems when the included class didn't have a
"new" method. Still, that's rare. They should have changed it to
work both ways, IMHO. Now the example in the perltoot tutorial is
incorrect.
Backpedal to an older Class::Struct version, or have your code
initialized the nested objects itself.

Yes. What I did was add an initialize() method and then called it
every time I called new() Here's the code for anyone who has a
similar problem and finds this thread:

sub initialize {
my $self = shift;
foreach my $station (@stations ) {
$self->$station(Station->new());
$self->$station->initialize();

}
}
: Is what I'm doing using symbolic references?

Technically, yes, but Perl resolves method names symbolically anyway.
Pay no attention to that man behind the curtain.

Noone ever pays attention to him until things go wrong...

Thanks again.
Sophie Miron ( smiron..at..mironloeb.org )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top