Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Updating the hash across the files

11 views
Skip to first unread message

bubunia...@gmail.com

unread,
Aug 19, 2016, 11:25:16 PM8/19/16
to
Hi all,

I have a .pm file which has a hash defined in the pm file along with the blessed class.

For exp:

A.pm
====
package A::Colour;

It is a hash that is bless with the class.
our %color_of = (
"apple" => "red",
"orange" => "orange",
"grape" => "purple",
);

1;

I want to modify this hash(A.pm) from a A.pl file which receives command line arguments.

A.pl [command line arg1] [command line arg2] [command line arg3]

So I did the following:

A. pl
====

my $a = $ARGV[0];
my $b = $ARGV[1];

require A::Colour;
my $obj = A::Colour->new();

$obj->{apple} = $a;
$obj->{orange} = $b;

print "$obj->{apple}"; # This doesnt print the updated values. I would expect the command line arguments value should have been updated in the hash. But it is not the case.

$obj->{apple} still prints "red". This is probably the scope of the object is lost between the files.

One way to fix this issue I think is to create the set/get function in A.pm and import the functions to the A.pl file. But that will increase the code size in the A.pm as for each variable in the hash you need public methods(set/get). Is there any efficient and cleaner way?

Can anyone help me in this regard?

Regards
Pradeep

0 new messages