Subject: | Can't use hashref as parameter to "new" |
Date: | Sat, 20 Dec 2014 23:29:02 +0100 |
To: | bug-Inline-Struct [...] rt.cpan.org |
From: | Heinz Knutzen <heinz.knutzen [...] gmail.com> |
The manual page of Inline::Struct states:
"new ... If you provide values, they should be appropriate for the
field type, and in the same order as they are defined in the struct."
When looking at the source code, we see that the first value is handled
specially if it is an array or hash ref. In this case it is used to
initialize multiple fields.
1. problem:
If the initial value is a hash ref and it is passed to "new" inside a
hashref,
the value can't be retrieved later.
2. problem:
There is a conceptual problem, if the first field is of type SV *.
If the first parameter to "new" is a hash ref, it can't be decided, if
the value should be used to initialize the first field or if it should
be handled specially.
Test case:
============================================
use strict;
use warnings;
use Test::More;
use Inline C => <<'END', structs => 1, force_build => 1,
clean_after_build => 0;
struct Foo {
SV *hash;
};
END
my $HASH = { a => { b => 'c' } };
my $o = Inline::Struct::Foo->new({hash => $HASH});
is_deeply $o->hash, $HASH, "hashref retrieved";
my $p = Inline::Struct::Foo->new($HASH);
is_deeply $p->hash, $HASH, "hashref retrieved";
done_testing;
============================================
This fails for me with:
not ok 1 - hashref retrieved
# Failed test 'hashref retrieved'
# at svmember.t line 13.
# Structures begin differing at:
# $got = '157264960'
# $expected = HASH(0x95d0da8)
No such field 'a' in struct Foo
# Tests were run but no plan was declared and done_testing() was not seen.
Used versions:
- Inline-Struct-0.16
- Perl v5.20.1