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

Discussion in 'Perl Misc' started by Sophie Miron, Nov 11, 2003.

  1. Sophie Miron

    Sophie Miron Guest

    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
     
    Sophie Miron, Nov 11, 2003
    #1
    1. Advertising

  2. Sophie Miron

    Jay Tilton Guest

    (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.
     
    Jay Tilton, Nov 11, 2003
    #2
    1. Advertising

  3. Sophie Miron

    Sophie Miron Guest

    Thanks Jay. That was exactly the problem.

    (Jay Tilton) wrote:
    > 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 )
     
    Sophie Miron, Nov 12, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp

    What went wrong here?

    Philipp, Feb 6, 2005, in forum: VHDL
    Replies:
    3
    Views:
    467
    Mike Treseler
    Feb 6, 2005
  2. Kenny
    Replies:
    1
    Views:
    352
    Daniel M. Hendricks
    Dec 13, 2004
  3. tshad
    Replies:
    5
    Views:
    352
    =?Utf-8?B?U2NvdHQgU2ltb25z?=
    Jan 14, 2005
  4. jonny
    Replies:
    1
    Views:
    471
    Michael Nemtsev
    Oct 6, 2007
  5. pui ming Wong

    went to 5.8.1 and now my while statement fails

    pui ming Wong, Nov 27, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    81
    Tore Aursand
    Nov 27, 2003
Loading...

Share This Page