xaphod has asked for the wisdom of the Perl Monks concerning the following question:

While trying to write some OO Perl code I did something that didn't work. I quickly realised my mistake, and used a temporary variable. This irked me somewhat, so I've spent far too long trying to work out an alternative.

I was wondering if some knowlegable Monk may be able to suggest a better way.

Some sample code which demonstrates my problem:

#!/usr/bin/perl use strict; package obj; use vars qw($AUTOLOAD ); sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = { }; bless ($self, $class); return $self; } sub AUTOLOAD { my $self = shift; my $attr = $AUTOLOAD; $attr =~ s/.*:://; return if $attr eq 'DESTROY';··· $self->{uc $attr} = shift if @_; return $self->{uc $attr}; } package main; my $ob1 = obj->new; my $ob2 = obj->new; my $ob3 = obj->new; $ob1->data(100); $ob2->object($ob1); $ob3->name('data'); my $temp=$ob3->name; print $ob2->object->$temp;

What I'd like to know is if is possible to produce the same results without the need for the temporary variable $temp.

If anyone is interested, my initial attempt was:
    $ob2->object->$ob3->name;

--
TTFN, FNORD

xaphod

Replies are listed 'Best First'.
Re: How do I do it without a temporary variable?
by sauoq (Abbot) on Dec 27, 2002 at 20:28 UTC

    I think the most straight forward syntax is probably

    $ob2->object->${\$ob3->name}
    but the question you should really be asking is "should I do this?" I would say no. You are mixing code and data. That's generally a bad idea. I don't know what the original problem is that you are trying to solve but I think it may be worthwhile to reconsider your design.

    -sauoq
    "My two cents aren't worth a dime.";
    

      Superb. Thank you sir, you are indeed a star.

      I think it may be worthwhile to reconsider your design.

      Indeed. The problem I'm trying to solve is immaterial, I simply decided it's time to start playing with objects. So I set myself a challenge and I'm implementing it as a collection of objects - learning all the while. But just occasionally I lapse back in to single use script behaviour. I suspect this is a prime example.

      --
      TTFN, FNORD

      xaphod
      Just to be really perverse ( and firmly in the "My eyes! my eyes! Aaaugh!" department), is there a way to do this using indirect OO syntax? I tried a couple of approaches, the closest I can get is:
      print data { $obj2->object };

        print data { $obj2->object };

        That doesn't even work. The print will break it. print STDOUT data { $ob2->object }; would though.

        I'm not sure how or even whether it is possible to get the ob3->name portion to be used as the method in indirect syntax though. I played with it a bit and the closest I could come to it required an eval. :-) I couldn't get it to work with symbolic refs either. Somehow, I think that's a good thing...

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: How do I do it without a temporary variable?
by jdporter (Paladin) on Dec 27, 2002 at 20:12 UTC
Re: How do I do it without a temporary variable?
by ehdonhon (Curate) on Dec 27, 2002 at 20:06 UTC

    I *think* this should work:

    $obj2->object->{$ob3->name()}
    update: Looks like I was on the right track, but wasn't quite correct. :(