Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Symbolic Reference in Method Call

by kennethk (Monsignor)
on Jun 27, 2013 at 17:02 UTC ( #1041053=note: print w/ replies, xml ) Need Help??


in reply to Symbolic Reference in Method Call

The appropriate reference here is Symbolic references. Code might look similar to the above, though be aware that by declaring $rtf with my, you are creating a lexical variable, not a package variable. This means it will get garbage collected, and may no longer exist by the time you want to access it. Much better than symbolic references are normal ones, e.g.

use RTF::Writer; my $rtf = RTF::Writer->new_to_file("greetings.rtf"); my $foo = \$rtf; ${$foo}->prolog(); ${$foo}->paragraph( "Hi there!" ); ${$foo}->close;

See Why it's stupid to use a variable as a variable name for why you are opening yourself up for pain. The above also passes strict.

In the end, however, these types of games generally raises the specter of an XY Problem. Why do you need a reference to the object? Is this a caching thing, which might be addressed with proper scoping?


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: Symbolic Reference in Method Call
Select or Download Code
Re^2: Symbolic Reference in Method Call
by Eily (Hermit) on Jun 27, 2013 at 17:17 UTC

    I agree with kennethk, if you want to use symbolic references, there's a high probability that you are doing it wrong, or do not have a clear idea of what you are trying to do. But, if you know that you are asking for trouble, here is something that might help you :

    our $value = "package var"; my $value = "lexical var"; my $ref = "value"; $, = ", "; print ${$ref}, $value, eval "\$$ref";
    package var, lexical var, lexical var

    You should see there that you can only access dynamix variables with symbolic references, not lexicals.

Re^2: Symbolic Reference in Method Call
by EclecticScion (Novice) on Jun 27, 2013 at 17:58 UTC

    Thanks for your reply! I've added a description of what I'm actually trying to accomplish below. I'm also going to spend more time reading some of the links you've suggested.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041053]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-09-19 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (130 votes), past polls