M
Mintcake
What is the best way to make a copy (not a deep copy) of an object
created by fields::new?
My complete example program is below.
+++++++++++++++++++++++++++++++++++++++++
#!/usr/local/bin/perl -wl
use strict;
package Ftest;
use fields qw(foo bar xyzzy);
sub new
{
my $class = shift;
my __PACKAGE__ $self = fields::new($class);
@$self{qw(foo bar xyzzy)} = @_;
return $self;
}
package main;
my Ftest $f = new Ftest 1,2,3;
my Ftest $g = new Ftest;
@$g = @$f;
---------------------------------------------------------------------------------
The last line above does the actual copy. This version works with 5.8
as the fields implementation uses pseudo-hashes but it doesn't work
with 5.9 because of course they are not array references. If I change
it to...
%$g = %$f
.... then it works on both Perls but I get a warning about pseudo-hashes
being deprecated on the 5.8 version.
I am reluctant to include a no warnings 'deprecated'; statement. There
is a good reason for this: One of the mistakes I frequently make, when
declaring a scalar and assigning to it a reference to such an object,
is to forget the type, i.e.
my $foo = shift;
instead of
my Type $foo = shift;
The warning about deprecated pseudio-hashes catches this which I find
very useful.
I particularly want my code to work with both 5.8 and 5.10 (when it
arrives) and I don't want to put conditional code in there.
created by fields::new?
My complete example program is below.
+++++++++++++++++++++++++++++++++++++++++
#!/usr/local/bin/perl -wl
use strict;
package Ftest;
use fields qw(foo bar xyzzy);
sub new
{
my $class = shift;
my __PACKAGE__ $self = fields::new($class);
@$self{qw(foo bar xyzzy)} = @_;
return $self;
}
package main;
my Ftest $f = new Ftest 1,2,3;
my Ftest $g = new Ftest;
@$g = @$f;
---------------------------------------------------------------------------------
The last line above does the actual copy. This version works with 5.8
as the fields implementation uses pseudo-hashes but it doesn't work
with 5.9 because of course they are not array references. If I change
it to...
%$g = %$f
.... then it works on both Perls but I get a warning about pseudo-hashes
being deprecated on the 5.8 version.
I am reluctant to include a no warnings 'deprecated'; statement. There
is a good reason for this: One of the mistakes I frequently make, when
declaring a scalar and assigning to it a reference to such an object,
is to forget the type, i.e.
my $foo = shift;
instead of
my Type $foo = shift;
The warning about deprecated pseudio-hashes catches this which I find
very useful.
I particularly want my code to work with both 5.8 and 5.10 (when it
arrives) and I don't want to put conditional code in there.