Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Interpolating Variables as Package Names

by dReKurCe (Scribe)
on Mar 09, 2005 at 20:06 UTC ( [id://438030]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Monks;


I am trying to intropolate a variable from a different package into a here document as follows:

#! /usr/bin/perl; unshift @INC, "/path/to/package"; print "Introplate variables from which package?"; $package=<STDIN>; use $package; print << EOF; This variable: #heres the intropolation I want to effect: $package::variable originates in package $package. EOF

So the problem I am trying to solve is the intropolation of the package name into the $packagename::variable format. What is the most correct approach to solving this?


Thank you

Replies are listed 'Best First'.
Re: Interpolating Variables as Package Names
by Roy Johnson (Monsignor) on Mar 09, 2005 at 20:22 UTC
    I'm dumping my previous answer in favor of pointing you here. At least until I figure out a concise answer.

    Update: Ok, I worked it out:

    use strict; use warnings; package foo; our $var = 'got it'; package main; my $pkg = 'foo'; my $v = 'var'; print ${$main::{$pkg.'::'}{$v}};
    Note that you get no hassles about symbolic references. "main::" is the name of the hash that includes the symbol tables for all the other packages. You index it by the package name (with double-colon appended), and then the variable name. That gives you a glob reference, which you can dereference using the appropriate sigil.

    Caution: Contents may have been coded under pressure.
      An amazing solution!Thanks for the help!
Re: Intropating Variables as Package Names
by crashtest (Curate) on Mar 09, 2005 at 20:22 UTC
    use is done at compile time in a BEGIN block. If you want to dynamically load a package at run time, you want require instead. The below code worked for me:
    #!/usr/bin/perl; unshift @INC, "/path/to/package"; print "Interpolate variables from which package? "; $package = <STDIN>; chomp($package); eval "require $package"; print <<EOF; This variable: $package::variable (actually this doesn't work) originates in package $package. EOF

    Update:: gaal is absolutely right, the code will not show the correct package variable. I was more focused on showing how to load modules at runtime. Roy Johnson seems to have the interpolation part worked out.

      $package::variable looks for a dynamic variable called $variable in the package package::. As Roy Johnson said, you need to look in the symbol table of $package.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438030]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found