in reply to Variable interpolation of object methods

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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Variable interpolation of object methods
by choroba (Cardinal) on Oct 30, 2010 at 23:22 UTC
    Or, even more obscure:
    print "'@{[$gene->display_id]}'\n";
      Any chance of an explanation rather than ever increasing obscurity ? :-D

        I believe it works like this:-

        $gene->display_id # Invoke the display_id # method of $gene object [ ] # construct anonymous array # around returned value(s) @{ } # dereference same " " # Interpolate into double- # quoted string

        although I'm sure others will correct me if I've got it wrong!

        Cheers,

        JohnGG

Re^2: Variable interpolation of object methods
by Anonymous Monk on Oct 30, 2010 at 23:18 UTC
    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?

        When the interpolation has read $gene, it stops parsing and so never sees the dereference (->).

        Whereas, inside ${ ... }, it expect any code, so it is parsed using the full Perl parser, rather than a limited parser looking only for a variable name.

        Note:That's my fits-the-evidence assumption, not definitive deep source-diving knowledge.


        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.