I sat, thought about this, and I still don't understand it.

  • Thread starter grocery_stocker
  • Start date
G

grocery_stocker

The following code is taken from perltooc

#!/usr/bin/perl -w

package Some_Class;
use strict;
our %ClassData = (
CData1 => "",
CData2 => "",
);


print "keys: " , keys %ClassData , "\n";

for my $datum (keys %ClassData) {
print "Datum values are: $datum \n";
print "Shit in global array: @_ \n";

no strict "refs";
*datum = sub {
#shift;
my $class = shift;

print "The class is: $class \n";
print "The value of datum is now: $datum \n";

$ClassData{$datum} = shift if @_;

print "values:" , $ClassData{$datum} , "\n";
print "The glob is: ", *datum , "\n";
print "The value of datum again is: ", $datum , "\n";

return $ClassData{$datum};
}
}

#print "The glob is: ", *datum , "\n";

package main;
no strict "refs";
#$Some_Class::CData1 = "test";

#my $first_value = Some_Class->datum("test");
#print "The value in main is: $first_value \n";

1)When I run the code; I get the following:
keys: CData2CData1
Datum values are: CData2
Shit in global array:
Datum values are: CData1
Shit in global array:
Subroutine Some_Class::datum redefined at ./att.pl line 33.

I'm not too sure how the subroutine gets redefined.

2)How do I invoke the method?

There are a few other things that are murky, but these are the two
major issues.

Chad
 
P

Peter J. Holzer

The following code is taken from perltooc

Not quite. You made a small but crucial error in copying the code.
Subroutine Some_Class::datum redefined at ./att.pl line 33.

I'm not too sure how the subroutine gets redefined.

So you should have a closer look at that line.

hp
 
G

grocery_stocker

Peter said:
Not quite. You made a small but crucial error in copying the code.


So you should have a closer look at that line.

hp

Okay, I caught the error. I changed
*datum = sub {

to

*$datum = sub {

So this makes more sense. I'm dealing with a scalar and not a
subroutine. I'm still lost on how to insert data into the hash. In
main, I tried the following:
package main;
no strict "refs";
Some_Class::{$ClassData}{$CData1} = "no";

But I get:
syntax error at ./att.pl line 45, near "Some_Class::{"
Global symbol "$ClassData" requires explicit package name at ./att.pl
line 45.
Execution of ./att.pl aborted due to compilation errors.
$emacs -nw att.pl

I'm figuring the syntax is wrong. I know how to write this if this was
a scalar, but I'm lost no how to write it when this is a hash.
 
P

Peter J. Holzer

[slightly buggy code to automatically create accessor methods from a
hash]
Okay, I caught the error. I changed
*datum = sub {

to

*$datum = sub {

So this makes more sense. I'm dealing with a scalar and not a
subroutine. I'm still lost on how to insert data into the hash. In
main, I tried the following:
package main;
no strict "refs";
Some_Class::{$ClassData}{$CData1} = "no";

The assignment

*$datum = sub { ... }

installs a new function with the name $datum. So after the loop you have
two new functions CData1 and CData2, and you can simply call them:

package main;

Some_Class->CData1("test");

my $first_value = Some_Class->CData1();
print "The value in main is: $first_value \n";


will print

The value in main is: test

That's the point of the exercise: To encapsulate the hash so that it
can't be manipulated directly from the outside, only by calling the
accessor methods.

hp
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top