Calling a method within qq'ed string

Discussion in 'Perl Misc' started by jab3, Feb 22, 2006.

  1. jab3

    jab3 Guest

    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
     
    jab3, Feb 22, 2006
    #1
    1. Advertising

  2. jab3 wrote:
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 22, 2006
    #2
    1. Advertising

  3. jab3

    jab3 Guest

    Gunnar Hjalmarsson wrote:
    > jab3 wrote:
    > > 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
    >


    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
     
    jab3, Feb 22, 2006
    #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. Paul Hadfield
    Replies:
    0
    Views:
    360
    Paul Hadfield
    Sep 17, 2007
  2. Steve Hershoff
    Replies:
    4
    Views:
    837
    Steve Hershoff
    Oct 11, 2007
  3. Fernando Gómez
    Replies:
    7
    Views:
    416
    Rolf Magnus
    Jun 8, 2008
  4. Ben
    Replies:
    3
    Views:
    238
  5. why-em-jay

    Calling a method of the calling object ...

    why-em-jay, Sep 15, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    178
    Anno Siegel
    Sep 15, 2005
Loading...

Share This Page