> Justin Allegakoen wrote:
> > -------8<--------
> > I'd like to figure out how to access package variables at runtime. In
> > other words, I may have a variable $class that contains the classname,
> > and I want to set a value to the package reference by this variable.
> > It'll be something like:
> >
> > $class::Message = "It works";
> > -------8<--------
> > Sounds like youre on the OOP path but don’t have an in depth
> understanding of it, at least not in a Perl sense.
> >
> >
> > -------8<--------
> > The problem now is that the package name is not known until runtime. I
> > may either be writing to $Package1::Message or $Package2::Message.
> >
> > Can anyone enlighten me on the proper syntax to use to do this?
> > -------8<--------
> > Run time binding and polymorphism are things that you should be
> evaluating.
> >
> >
> > -------8<--------
> > To complicate matters, what happens if I also want to assign values of
> > variables which are known only at runtime? For example (pseudocode):
> >
> > $class::$variable = "It works";
> > -------8<--------
> > Looks as if youre unwittingly trying to use symbolic references. Mark
> Jason Dominus explains the donts here
> http://perl.plover.com/varvarname.html
> >
> >
> > As for the rest well perlboot and perltoot will point you in the right
> direction.
> >
> > Just in
> >
> > _______________________________________________
> > Perl-Win32-Users mailing list
> > Perl-Win32-Users@listserv.ActiveState.com
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> >


> -----Original Message-----
> From: Foo JH [mailto:[EMAIL PROTECTED]
> Sent: Friday, 24 August 2007 10:40 AM
> To: Justin Allegakoen
> Cc: 'perl-win32-users@listserv.ActiveState.com'
> Subject: Re: Dynamically referencing a package variable
> 
> Thanks Justin,
> 
> I'm quite familiar with OOP. It's class variables that I'm interested in
> setting, not instance variables. In Perl, this is implemented as package
> variables like $MyPackage::MyVariable.
> 
> I could've done this: $__PACKAGE__::MyVariable, but as I said, the
> package name is not determined at run time, so I need to use a variable
> in place of __PACKAGE__
> 
> Hope you have some ideas on this. Thanks.


The way I read your post had the encapsulation bells ringing.

Again, symbolic references offer a possible solution:-

<code>

package Root;
$me = 'How much?';

package main;
use strict;
use warnings;

my $package = 'Root';
my $var_name = 'me';

# And ducking the rotten tomatoes he continues with
no strict 'refs';       
print 'I was quibbed with "' . ${"${package}::$var_name"} . qq{"\n};

${"${package}::$var_name"} = 'For you? Ten dollars';

print 'So I replied "' . ${"${package}::$var_name"} . '"';

</code>

Typeglobs and aliasing may be more revered though.

Just in

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to