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

In reply to How do I do it without a temporary variable? by xaphod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.