L
lroland
Hi
I am trying to use "lock_keys" to create a unmodifiable perl module -
my code is inspired by this article
http://perltraining.com.au/tips/2005-01-31.html - I figured that if I
do the following in my constructor then it works:
----------------
my %i = ( name => undef );
bless \%i, 'Misc';
lock_keys(%i);
----------------
unfortunately this is not very handy in a perl module rather than using
a explicit hash I would like to use a reference in the style of:
----------------
package Misc::Misc;
use diagnostics;
use warnings;
use strict;
use Hash::Util qw(lock_keys);
use Data:umper;
sub new
{
my ($this) = @_;
bless {
name => undef,
info => {
age => undef
}
}, $this;
lock_keys(%$this); # this does not work
}
sub print
{
my ($this) = @_;
my $content = Dumper($this);
print "Hash content is:\n $content";
}
1;
----------------
This does however not work, when trying to use the module:
----------------
#!/usr/bin/perl
use strict;
use Misc::Misc;
my $test = Misc::Misc->new();
$test->{name} = "foobar";
$test->{info}->{age} = "42";
$test->print();
I am trying to use "lock_keys" to create a unmodifiable perl module -
my code is inspired by this article
http://perltraining.com.au/tips/2005-01-31.html - I figured that if I
do the following in my constructor then it works:
----------------
my %i = ( name => undef );
bless \%i, 'Misc';
lock_keys(%i);
----------------
unfortunately this is not very handy in a perl module rather than using
a explicit hash I would like to use a reference in the style of:
----------------
package Misc::Misc;
use diagnostics;
use warnings;
use strict;
use Hash::Util qw(lock_keys);
use Data:umper;
sub new
{
my ($this) = @_;
bless {
name => undef,
info => {
age => undef
}
}, $this;
lock_keys(%$this); # this does not work
}
sub print
{
my ($this) = @_;
my $content = Dumper($this);
print "Hash content is:\n $content";
}
1;
----------------
This does however not work, when trying to use the module:
----------------
#!/usr/bin/perl
use strict;
use Misc::Misc;
my $test = Misc::Misc->new();
$test->{name} = "foobar";
$test->{info}->{age} = "42";
$test->print();