EclecticScion has asked for the wisdom of the Perl Monks concerning the following question:
Greetings, O monks,
I am having difficulty using references in the context of a method call I am using. For example, this code (stolen from here) works perfectly:
use RTF::Writer; my $rtf = RTF::Writer->new_to_file("greetings.rtf"); $rtf->prolog(); $rtf->paragraph( "Hi there!" ); $rtf->close;
I would like to know how to use a reference in place of $rtf. Specifically, I would like to able to retrieve the string "rtf" from somewhere else, and then call $(insert string here)->... My best guess on how to do this, after consulting perlmod, would be something like this:
use RTF::Writer; my $rtf = RTF::Writer->new_to_file("greetings.rtf"); my $foo = "rtf"; ${$foo}->prolog(); ${$foo}->paragraph( "Hi there!" ); ${$foo}->close;
As it currently stands, the above returns an error. "use diagnostics;" reveals that the value of ${$foo} is undefined. Upon further research, I have determined that using symbolic references in this context is destined to fail. This page talks about using hashes as references for functions, but I haven't been able to find anything on method calls that are stored as scalars, as in the first example code above.
My short term goal is to figure out the correct code to replace ${$foo} in the second code block above. Any help would be greatly appreciated!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Symbolic Reference in Method Call
by kennethk (Abbot) on Jun 27, 2013 at 17:02 UTC | |
by Eily (Monsignor) on Jun 27, 2013 at 17:17 UTC | |
by EclecticScion (Novice) on Jun 27, 2013 at 17:58 UTC | |
|
Re: Symbolic Reference in Method Call
by Loops (Curate) on Jun 27, 2013 at 17:02 UTC | |
|
Re: Symbolic Reference in Method Call
by EclecticScion (Novice) on Jun 27, 2013 at 17:55 UTC | |
by kennethk (Abbot) on Jun 27, 2013 at 18:27 UTC | |
by EclecticScion (Novice) on Jun 28, 2013 at 15:23 UTC |