Mailing List

Flat
Dynamically including package variable
User: mcholste
Date: 2/8/2012 2:00 pm
Views: 405
Rating: 0
Ok, I'm having a really hard time doing something basic:

perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'

I want it to print "abcd" but I can't figure out the way to do this.
The reason is I want to include a package variable from a plugin
module, so I won't know the module name ahead of time.  I don't want
to instantiate the plugin, so I don't want to use a method.  Anyone
know why $$var isn't working?

--Martin
Re: Dynamically including package variable
User: david-delikat
Date: 2/8/2012 2:18 pm
Views: 0
Rating: 0

perl -le 'my $var = \my $asdf = "abcd"; print $$var;'

that should do it ( though you mighty need to double the
quote '\' in order for it to make it past the shell. )

-dav


On Feb 8, 2012, at 2:00 PM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:

mcholste wrote:

Ok, I'm having a really hard time doing something basic:

perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'

I want it to print "abcd" but I can't figure out the way to do this.
The reason is I want to include a package variable from a plugin
module, so I won't know the module name ahead of time.  I don't want
to instantiate the plugin, so I don't want to use a method.  Anyone
know why $$var isn't working?

--Martin

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

Re: Dynamically including package variable
User: david-delikat
Date: 2/8/2012 6:23 pm
Views: 0
Rating: 0

>  perl -le 'my $var = \my $asdf = "abcd"; print $$var;'

someday I'll remember to read the whole message...
Re: Dynamically including package variable
User: Spizzat2
Date: 2/8/2012 2:20 pm
Views: 0
Rating: 0
I think the $$var is going to try to create a value from the variable  at memory location "asdf", which is nonsense.

Do you need something more like this?
http://www.perlmonks.org/?node_id=827168

On Wed, Feb 8, 2012 at 2:00 PM, <mcholste@gmail.com> wrote:

mcholste wrote:

Ok, I'm having a really hard time doing something basic:

perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'

I want it to print "abcd" but I can't figure out the way to do this.
The reason is I want to include a package variable from a plugin
module, so I won't know the module name ahead of time.  I don't want
to instantiate the plugin, so I don't want to use a method.  Anyone
know why $$var isn't working?

--Martin

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

Re: Dynamically including package variable
User: tmurray
Date: 2/8/2012 6:18 pm
Views: 0
Rating: 0
On 2/8/2012 2:20 PM, spizzat2@gmail.com wrote:
> Spizzat2 wrote:
>
> I think the $$var is going to try to create a value from the variable
> at memory location "asdf", which is nonsense.

It's a symbolic link, which does technically work for package vars.
It's just not a good idea.

>
> Do you need something more like this?
> http://www.perlmonks.org/?node_id=827168
>
> On Wed, Feb 8, 2012 at 2:00 PM,  > wrote:
>
>     mcholste wrote:
>
>     Ok, I'm having a really hard time doing something basic:
>
>     perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'
>
>     I want it to print "abcd" but I can't figure out the way to do this.
>     The reason is I want to include a package variable from a plugin
>     module, so I won't know the module name ahead of time.  I don't want
>     to instantiate the plugin, so I don't want to use a method.  Anyone
>     know why $$var isn't working?
>
>     --Martin
>
>     View Online
>    
>
>
>
>     Madison Area Perl Mongers - MadMongers
>     http://www.madmongers.org
>
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org

Re: Dynamically including package variable
User: jt
Date: 2/8/2012 2:22 pm
Views: 0
Rating: 0
The way you have that going it would be dereferencing a scalar ref. I think instead you want:

perl -le 'my $var = "asdf"; my $asdf = "abcd"; print eval($var);'



On Feb 8, 2012, at 2:00 PM, <mcholste@gmail.com> wrote:

mcholste wrote:

Ok, I'm having a really hard time doing something basic:

perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'

I want it to print "abcd" but I can't figure out the way to do this.
The reason is I want to include a package variable from a plugin
module, so I won't know the module name ahead of time.  I don't want
to instantiate the plugin, so I don't want to use a method.  Anyone
know why $$var isn't working?

--Martin

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

Re: Dynamically including package variable
User: miner
Date: 2/8/2012 2:34 pm
Views: 0
Rating: 0
Because it doesn't work with lexical variables.

Getting rid of the lexical on $asdf (and break -w -- which I notice you're not using, but that's hopefully just because you simplified the example) works:

-----
(3298) wanger [~]perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'

(3299) wanger [~]perl -le 'my $var = "asdf"; $asdf = "abcd"; print $$var;'
abcd

-----

But, of course, it's a terribly evil thing to do, and fraught with danger.  Be very sure of what you are doing.  MJD wrote about it long ago: http://perl.plover.com/varvarname.html

jon

On 2/8/12 2:00 PM, mcholste@gmail.com wrote:

mcholste wrote:

Ok, I'm having a really hard time doing something basic:

perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'

I want it to print "abcd" but I can't figure out the way to do this.
The reason is I want to include a package variable from a plugin
module, so I won't know the module name ahead of time.  I don't want
to instantiate the plugin, so I don't want to use a method.  Anyone
know why $$var isn't working?

--Martin

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

-- 
.Jonathan J. Miner----------------------------------------------------.
|  jon@jjminer.org  |      photos - http://photos.jjminer.org/        |
|                   | R.A.W. #1629 - http://www.reggaeambassadors.org |
|                   | LOCS Webmaster - http://www.locs-buffett.org    |
|  jabber/gchat: camrycurbhopper@gmail.com                            |
`---------------------------------------------------------------------'

"We don't have a town drunk...   We all take turns!"
Re: Dynamically including package variable
User: tmurray
Date: 2/8/2012 2:35 pm
Views: 0
Rating: 0
Only package vars are accessible through symbolic refs:

$ perl -le 'my $var = "asdf"; our $asdf = "abcd"; print $$var;'
abcd

On 2/8/2012 2:00 PM, mcholste@gmail.com wrote:
> mcholste wrote:
>
> Ok, I'm having a really hard time doing something basic:
>
> perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $$var;'
>
> I want it to print "abcd" but I can't figure out the way to do this.
> The reason is I want to include a package variable from a plugin
> module, so I won't know the module name ahead of time. I don't want
> to instantiate the plugin, so I don't want to use a method. Anyone
> know why $$var isn't working?
>
> --Martin
>
> View Online
>
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org

Re: Dynamically including package variable
User: afbach
Date: 2/8/2012 2:40 pm
Views: 0
Rating: 0
On Wed, Feb 8, 2012 at 2:00 PM,   wrote:
> Ok, I'm having a really hard time doing something basic:
>
> perl -le 'my $var = "asdf"; my $asdf = "abcd"; print $var;'

Well this works:
perl -le 'my $var = "asdf"; my $asdf = "abcd"; print eval qq{ "\$$var"; };'

as does (pace The Camel pg 254):
perl -le 'my $var = "asdf"; ${$var} = "abcd"; print ${$var};'

or:
perl -le 'my $var = "asdf"; our $asdf = "abcd"; print $$var;'

(note the my/our there).  But usually there's a better way - is it a
limited set of possible modules?
--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: Dynamically including package variable
User: esayward
Date: 2/8/2012 3:00 pm
Views: 1
Rating: 0
if you're trying to do what I think you are.. try this
 
my %var = ();
my $var{'one'} = "asdf";
my $var{'two'} = "abcd";
 
print "$var{'two'}";
 
Re: Dynamically including package variable
User: david-delikat
Date: 2/8/2012 6:28 pm
Views: 0
Rating: 0

perhaps a saner implementation would be a class method that returns a reference to the scalar:

package plugin;

my $data;

sub getvar { \$data }

... later ...

$class = 'plugin';

my $rdata = $class->getvar();

but then who said sanity was part of the specs?

Re: Dynamically including package variable
User: mcholste
Date: 2/9/2012 11:46 am
Views: 92
Rating: 0
Thanks all, for your responses!  None of you were helpful.  ;)

> david-delikat wrote:
> package plugin;
> my $data;
> sub getvar { \$data }
> $class = 'plugin';
> my $rdata = $class->getvar();

This is close, but Moose prevents this from working elegantly.  Here's
the whole story:

package Plugins::MyPackage;
use Moose;
our $Description = "this my package";
has 'description' => (is => 'rw', isa => 'Str', required => 1, default
=> $Description);

package MyObj;
use Moose;
use Module::Pluggable require => 1, search_path => qw(Plugins);
sub BUILD {
 foreach my $plugin ($self->plugins()){
   print $plugin->description;
 }
}

This fails in Moose because the object has not yet been instantiated.
It will spit:
Can't use string ("Plugins::MyPackage") as a HASH ref while "strict
refs" in use at accessor Plugins::MyPackage::description

The easy solution is to just use a very simple sub as Dave suggested:

sub description { return $Description }

This works great, but it really irks me that I have to do it this way
because it doesn't fit into the "Moose world" well of inheritance,
etc.  That's why I tried to do it just accessing the package variable
directly:

sub BUILD {
 foreach my $plugin ($self->plugins()){
   print "$plugin::Description";
 }
}

which also saves on creating the method at all.  This doesn't work
though, and was the cause of my email.  So, is there anything better
than a simple subroutine wrapper around the package variable?
Re: Dynamically including package variable
User: jt
Date: 2/9/2012 1:13 pm
Views: 0
Rating: 0
I submit that the reason we weren't helpful was that you weren't forthcoming about what you were actually trying to do.  =)


On Feb 9, 2012, at 11:46 AM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:

mcholste wrote:

Thanks all, for your responses!  None of you were helpful.  ;)

Re: Dynamically including package variable
User: hoelzro
Date: 2/9/2012 1:21 pm
Views: 0
Rating: 0
On Thu, 09 Feb 2012 11:46:41 -0600
wrote:

> mcholste wrote:
> Thanks all, for your responses!  None of you were helpful.  ;)
>
> > david-delikat wrote:
> > package plugin;
> > my $data;
> > sub getvar { \$data }
> > $class = 'plugin';
> > my $rdata = $class->getvar();
>
> This is close, but Moose prevents this from working elegantly.  Here's
> the whole story:
>
> package Plugins::MyPackage;
> use Moose;
> our $Description = "this my package";
> has 'description' => (is => 'rw', isa => 'Str', required => 1, default
> => $Description);
>
> package MyObj;
> use Moose;
> use Module::Pluggable require => 1, search_path => qw(Plugins);
> sub BUILD {
>  foreach my $plugin ($self->plugins()){
>    print $plugin->description;
>  }
> }
>
> This fails in Moose because the object has not yet been instantiated.
> It will spit:
> Can't use string ("Plugins::MyPackage") as a HASH ref while "strict
> refs" in use at accessor Plugins::MyPackage::description
>
> The easy solution is to just use a very simple sub as Dave suggested:
>
> sub description { return $Description }
>
> This works great, but it really irks me that I have to do it this way
> because it doesn't fit into the "Moose world" well of inheritance,
> etc.  That's why I tried to do it just accessing the package variable
> directly:
>
> sub BUILD {
>  foreach my $plugin ($self->plugins()){
>    print "$plugin::Description";
>  }
> }
>
> which also saves on creating the method at all.  This doesn't work
> though, and was the cause of my email.  So, is there anything better
> than a simple subroutine wrapper around the package variable?
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org

You could use symbolic references:

 no strict 'refs';
 my $description = ${$plugin . '::Description'};

which is unclean.  Or you could use symbol tables:

 my $description = ${ *{ $main::{$plugin . '::'}{'Description'} }{'SCALAR'} };

which is ugly.

-Rob
Re: Dynamically including package variable
User: david-delikat
Date: 2/9/2012 1:40 pm
Views: 0
Rating: 0

yeah if you use 'hasa' it won't work.  but it you use a sub it will.

_hasa_ creates an _object method_

_sub_ can be used as a _class method_ which is what you want.


On Feb 9, 2012, at 11:46 AM, <mcholste@gmail.com> <mcholste@gmail.com> wrote:

mcholste wrote:

Thanks all, for your responses!  None of you were helpful.  ;)

> david-delikat wrote:
> package plugin;
> my $data;
> sub getvar { \$data }
> $class = 'plugin';
> my $rdata = $class->getvar();

This is close, but Moose prevents this from working elegantly.  Here's
the whole story:

package Plugins::MyPackage;
use Moose;
our $Description = "this my package";
has 'description' => (is => 'rw', isa => 'Str', required => 1, default
=> $Description);

package MyObj;
use Moose;
use Module::Pluggable require => 1, search_path => qw(Plugins);
sub BUILD {
 foreach my $plugin ($self->plugins()){
   print $plugin->description;
 }
}

This fails in Moose because the object has not yet been instantiated.
It will spit:
Can't use string ("Plugins::MyPackage") as a HASH ref while "strict
refs" in use at accessor Plugins::MyPackage::description

The easy solution is to just use a very simple sub as Dave suggested:

sub description { return $Description }

This works great, but it really irks me that I have to do it this way
because it doesn't fit into the "Moose world" well of inheritance,
etc.  That's why I tried to do it just accessing the package variable
directly:

sub BUILD {
 foreach my $plugin ($self->plugins()){
   print "$plugin::Description";
 }
}

which also saves on creating the method at all.  This doesn't work
though, and was the cause of my email.  So, is there anything better
than a simple subroutine wrapper around the package variable?

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

Re: Dynamically including package variable
User: haarg
Date: 2/9/2012 9:58 pm
Views: 0
Rating: 0
Is description intended to be a mutable attribute?  Is the source from
the class or the object?  Those considerations make a big difference
in how you implement this.  If it's meant to be a constant value
associated with the class, then your approach is perfectly valid, and
isn't any more 'moosy' than any other.  If you actually want a class
attribute, there is https://metacpan.org/module/MooseX::ClassAttribute
for that.  If you want it to be a mutable object attribute then you
need something entirely different.

On Thu, Feb 9, 2012 at 12:46 PM,   wrote:
> mcholste wrote:
>
> Thanks all, for your responses!  None of you were helpful.  ;)
>
>
>> david-delikat wrote:
>> package plugin;
>> my $data;
>> sub getvar { \$data }
>> $class = 'plugin';
>> my $rdata = $class->getvar();
>
> This is close, but Moose prevents this from working elegantly.  Here's
> the whole story:
>
> package Plugins::MyPackage;
> use Moose;
> our $Description = "this my package";
> has 'description' => (is => 'rw', isa => 'Str', required => 1, default
> => $Description);
>
> package MyObj;
> use Moose;
> use Module::Pluggable require => 1, search_path => qw(Plugins);
> sub BUILD {
>  foreach my $plugin ($self->plugins()){
>    print $plugin->description;
>  }
> }
>
> This fails in Moose because the object has not yet been instantiated.
> It will spit:
> Can't use string ("Plugins::MyPackage") as a HASH ref while "strict
> refs" in use at accessor Plugins::MyPackage::description
>
> The easy solution is to just use a very simple sub as Dave suggested:
>
> sub description { return $Description }
>
> This works great, but it really irks me that I have to do it this way
> because it doesn't fit into the "Moose world" well of inheritance,
> etc.  That's why I tried to do it just accessing the package variable
> directly:
>
> sub BUILD {
>  foreach my $plugin ($self->plugins()){
>    print "$plugin::Description";
>  }
> }
>
> which also saves on creating the method at all.  This doesn't work
> though, and was the cause of my email.  So, is there anything better
> than a simple subroutine wrapper around the package variable?
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
PreviousNext
Madison Area Perl Mongers