Calling a method within qq'ed string

J

jab3

1st: I'm REALLY sorry for the previous empty post (if it makes it out).
I accidentally hit enter after subject.
2nd: Sorry for using Google Groups. I'm not at my normal computer.
3rd: I looked for the answer in this group and on the web, but couldn't
find what I'm looking for. I'm sure someone will immediately point out
15 references on their first query. Sorry.

Anyway, I'm having trouble figuring out how to call a method from an OO
module from within a string built around qq~ ~;. The reason for
building the string is that it's a web-based app, and I'm building HTML
that way. A simple example follows.

#
# MyTest.pm
#
package MyTest;
use strict;
use warnings;

sub new
{
my $class = shift;

$class = ref($class) || $class; # Just to be safe
bless { this => 'that'}, $class;
}

sub what_is_this
{
my $self = shift;

return $self->{this};
# I suppose I could say 'return shift->{this}', but
# I like a little flow
}

1;

-------------------------------------------------------------
#!/usr/bin/perl
#
# try_this.pl
#
use MyTest;
use strict;
use warnings;

my $what = new MyTest;

my $content = qq~
<p>I want the value of this: $what->what_is_this </p>
~;

print $content;

-----------------------------------------------------------

Output:

<p>I want the value of this: MyTest=HASH(0x8170c28)->what_is_this </p>


I tried dereference the method various ways just to see what happened.
One error was 'Can't use string ("that") as a SCALAR ref while "strict
refs" in use at ./try_this.pl line 8.'


So, could someone please tell me there is a way to call the methods
directly instead of sticking the values in variables outside the qq
string? :) Or, what is the right way to go about this? Any help
would be greatly appreciated.


Thanks,
jab3
 
G

Gunnar Hjalmarsson

jab3 said:
Anyway, I'm having trouble figuring out how to call a method from an OO
module from within a string built around qq~ ~;.

perldoc -q expand
 
J

jab3

Gunnar said:
perldoc -q expand

Nice. Of course that's exactly what I was looking for. But now I've
got to decide whether the punctuation is worth it. :)

Either way, thanks for the pointer. Works great.


-jab3
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top