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

This is trivial but I'm getting tired of working around it.
Does anybody have a solution? The attached code returns;
I want blat to be the correct value not blah=HASH(0x804c8d4)->blat()

I know why Perl does this but...
I often use here documents for HTML and OO accessor methods, isn't there a better way then reassigning $self->blat all the time?
#!/usr/bin/perl -w + package blah; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } + sub print_dumb_question { my $self = shift; my $blat = $self->blat; print <<HERE I want blat to be the $blat not $self->blat() \n HERE ; } + sub blat { return ('correct value'); } + package main; my $dumb_problem = blah->new; $dumb_problem->print_dumb_question;

Replies are listed 'Best First'.
Re: Printing accessor methods in here documents
by dragonchild (Archbishop) on May 06, 2005 at 18:47 UTC
    @{[$self->blat()]}

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Printing accessor methods in here documents
by davidrw (Prior) on May 06, 2005 at 20:00 UTC
    Another alternative to the @{ ... } and ${\( ... )} syntaxes still allows the use of here-docs -- just use printf instead of print:
    my $self = shift; my $blat = $self->blat; printf <<HERE, $blat, $self->blat, $self; blat is now %s, same as ->blat: %s, and not just %s->blat() HERE
Re: Printing accessor methods in here documents
by bart (Canon) on May 06, 2005 at 19:44 UTC
    Check out Interpolation on CPAN. It's not exactly intended to bind to methods, but to functions.

    But it sure as hell beats the heck out of the @{[ ... ]} and ${\( ... )} syntaxes.

Re: Printing accessor methods in here documents
by cog (Parson) on May 06, 2005 at 18:49 UTC
    I think this would do the trick:

    print <<HERE I want blat to be the $blat not @{[$self->blat()]} \n HERE

    O:-)

    Two other things:

    1) You're not using strict!!! You should! :-)

    2) Why are you using \n inside a heredoc? Why not just hitting return?

      Wow you guys are quick, thanks.
      Now i'm scratching my head trying to parse that
      So tell me, whats going on with @{[ ]}

        Similarly, if you'd wanted $self->blat() evaluated in scalar context, you could say ${\$self->blat()}.

        (Added) !1++, you're exactly right.

        After Compline,
        Zaxo

        The [] creates a reference to a list. The @{ } dereferences that list, so you get basically the same thing you've put in it (kind of).

        The trick here is: inside a string, @{[ ]} gets whatever is inside it to be evaluated as an array.

        Your first version was having $self being parsed all by itself (try opening your code with Vim, having syntax on; Vim's syntax will catch that for you).

        And don't forget to use strict. And probably to create an account here, too, so we can all say "welcome" :-)