Setting variables in a package passed as a hash

D

doug

Does anyone know how to set a local variable in a package by passing a
hash? ie: I basically want to pass the values to a package and if the
varaibles exist in the package space then set the values, if they do
not exist then ignore them.

I could do this using a hash, but I wanted to try this method with
local variables.

#!/opt/third-party/bin/perl -w

use strict;
use defined2;

{
my $hash_key = 0;
my ( %attrib ) = @ARGV;

my $d = new defined2();
$d->setValue( x => 102, y => 99, z => 'Loretta' );
$d->printAll();
}

exit 0;

package defined2;

use strict;

{

my $x = 20;
my $y = 25;
my $z = 'douglas';


sub new {
my $object = shift;
return bless {}, $object;
}

sub setValue {
my $self = shift;
my %args = @_;
foreach my $key ( keys %args ) {
no strict;
*{"$key"} = sub { $args{$key} };
}
}

sub printAll {
my $self = shift;
print "x is [ $x ]\n";
print "y is [ $y ].\n";
print "z is [ $z ].\n";
}
}
1;


thanks
dn
 
T

Tad McClellan

doug said:
Does anyone know how to set a local variable in a package by passing a


If by "local variable" you mean a "lexical variable", then:

No, because lexical variables are not in any package.

If by "local variable" you mean a local() variable (ie. a package
variable), then:

Yes, those can be manipulated with symbol table tricks (such
as a type glob).

hash? ie: I basically want to pass the values to a package and if the
varaibles exist in the package space then set the values, if they do
not exist then ignore them.


But your defined2 package does not have any variables in
the package space, it only has lexical variables, which are
not in any package.

I could do this using a hash, but I wanted to try this method with
local variables.

#!/opt/third-party/bin/perl -w


use warnings; # is much better the the -w command line switch

use defined2;


You don't need to use the package when it is already in the same file.

{
my $hash_key = 0;
my ( %attrib ) = @ARGV;


Why are those there?

They are not used anywhere else...

my $d = new defined2();
$d->setValue( x => 102, y => 99, z => 'Loretta' );
$d->printAll();
}

exit 0;

package defined2;

use strict;

{

my $x = 20;
my $y = 25;
my $z = 'douglas';


If you intend to play symbol table tricks with them, then they
must _be_ in the symbol table:

our $x = 20;
our $y = 25;
our $z = 'douglas';

sub new {
my $object = shift;
return bless {}, $object;
}

sub setValue {
my $self = shift;
my %args = @_;
foreach my $key ( keys %args ) {
no strict;
*{"$key"} = sub { $args{$key} };


( A useless use of double quotes, see: perldoc -q vars )

Here you fill the coderef part of the glob...

}
}

sub printAll {
my $self = shift;
print "x is [ $x ]\n";


.... but here you are accessing the scalar part of the glob, which
hasn't been mucked with.

If you want coderefs as accessors, then you must call the coderef:

print "x is [ ", x(), " ]\n";

If you want to muck with the scalar slot of the glob, then:

*{$key} = \$args{$key};


If you want something else, then try asking again 'cause I'm not
too sure what that something else is...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top