Simple methods when using fields

N

Nick of course

I am convertion a class in which objects are currently implemented as
blessed hashes to use 'fields' instead.

In the existing class there are many simple methods that ane currently
written using syntax similar to that below:

sub setfoo99 { $_[0]->{foo} = 99 }

The equivalent new syntax is:

sub setfoo99 {
my __PACKAGE__ $self = shift;
$self->{foo} = 99
}

Is there an alternative syntax that eliminates the need for an
intermediate typed variable but still ensures compile time key lookup
 
B

Ben Morrow

Quoth "Nick of course said:
I am convertion a class in which objects are currently implemented as
blessed hashes to use 'fields' instead.

In the existing class there are many simple methods that ane currently
written using syntax similar to that below:

sub setfoo99 { $_[0]->{foo} = 99 }

The equivalent new syntax is:

sub setfoo99 {
my __PACKAGE__ $self = shift;
$self->{foo} = 99
}

Is there an alternative syntax that eliminates the need for an
intermediate typed variable but still ensures compile time key lookup

Compile-time key lookup will no longer happen anyway as of 5.10.
Pseudohashes are deprecated, as they have many problems. I would just
skip the declaration and leave the sub as it is.

Ben
 
M

Mintcake

Ben said:
Compile-time key lookup will no longer happen anyway as of 5.10.
Pseudohashes are deprecated, as they have many problems. I would just
skip the declaration and leave the sub as it is.

I'm not explicitly using pseudohashes. I'm using the fields pragma
which, in 5.8, happens to be implemented using pseudohashes. The same
code should work perfectly well under 5.10 where fields will (I assume)
be implemented using restricted hashes instead.

I've tried the code out on 5.9.3 and if I supply an invalid key I
receive a *compiler* error. Are you saying that the fields
implementation will change between 5.9.3 and 5.10.0?
 
M

Mintcake

Ben said:
Compile-time key lookup will no longer happen anyway as of 5.10.
Pseudohashes are deprecated, as they have many problems. I would just
skip the declaration and leave the sub as it is.
5.9.3 uses restricted hashes to implement fields and if you supply an
invalid key you get a *compile* time error. Will this change for 5.10?
 
B

Ben Morrow

Quoth "Mintcake said:
I'm not explicitly using pseudohashes. I'm using the fields pragma
which, in 5.8, happens to be implemented using pseudohashes. The same
code should work perfectly well under 5.10 where fields will (I assume)
be implemented using restricted hashes instead.

I've tried the code out on 5.9.3 and if I supply an invalid key I
receive a *compiler* error. Are you saying that the fields
implementation will change between 5.9.3 and 5.10.0?

Oh... I wasn't aware of that... that's a little weird. OK, you still get
compile-time checking; but since you don't get any performance benefit I
stand by what I said above: forget the typed variables. They really
don't fit in Perl5.

Ben
 

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
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top