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

Dear Monks

I am new to perl. Please can you tell me how I achieve varaible interpolation of the an object method e.g

print "'$gene->display_id'\n";
This gives 'Bio::EnsEMBL::Gene=HASH(0x2c88e90)->display_id' Whereas I want the value in single quotes. I tried rmoving the single quotes and adding backslahes but to no avail. There is nothing in my book about how to do this.

thanks

Replies are listed 'Best First'.
Re: Variable interpolation of object methods
by BrowserUk (Patriarch) on Oct 30, 2010 at 22:57 UTC

    Somewhat obscure, but logical:

    print "'${ \$gene->display_id }'";

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Or, even more obscure:
      print "'@{[$gene->display_id]}'\n";
        Any chance of an explanation rather than ever increasing obscurity ? :-D
      Browseruk, can you explain why your deferencing method worked but mine didn't. I thought perl was deferencing the refernce for the gene object by using the -> notation. Whta exactly are you deferencing as the display_id method doesn't retrun a reference. thanks
        are you creating a reference to this variable $gene->display_id and then deferencing it straight away with ${ } Why do you have to do this? Why doesnt perl perform variable interpolation of these types of things out of the box as it were?
Re: Variable interpolation of object methods
by choroba (Cardinal) on Oct 30, 2010 at 22:20 UTC
    Use list:
    print "'",$gene->display_id,"'\n";
Re: Variable interpolation of object methods
by Anonymous Monk on Oct 30, 2010 at 22:28 UTC
    I had a guess at this to deference the gene object reference but this just printed out nothing print "${$gene}{'display_id'}\n";