develooper Front page | perl.perl5.porters | Postings from May 2016

[perl #128301] [PATCH] Test to see if hash values are eagerlycreated

Thread Next
From:
James E Keenan via RT
Date:
May 31, 2016 22:40
Subject:
[perl #128301] [PATCH] Test to see if hash values are eagerlycreated
Message ID:
rt-4.0.18-25491-1464734436-108.128301-15-0@perl.org
On Tue May 31 05:29:03 2016, oainikusama@gmail.com wrote:
> Hi everyone,
> 
> I have spoken to Zefram and TonyC on #p5p, and apparently it's not well
> documented in perl which operations will eagerly create hash elements. In
> foo(\$x{bla}), for example, the \ is treating its operand as an lvalue,
> same as if it were on the lhs of an assignment like that. Now, foo($x{bla})
> *also* treats $x{bla} as an lvalue, but doesn't eagerly create it. Instead,
> it passes a PVLV to foo(), which can then create the hash element by
> assigning to $_[0].
> 
> Function arguments are usually taken by value but there's no type
> information to say which arguments specifically are taken by reference and
> so need the lvalue treatment. This patch adds two extra tests for that
> behaviour, so at least we can check whether they remain consistent.
> 
> Hope it helps :)
> Thanks!
> 
> garu

Here's the commit message:

#####
It's not well documented which operations will eagerly create hash
elements. In foo(\$x{bla}), for example, the \ is treating its
operand as an lvalue, same as if it were on the lhs of an assignment
like that. Now, foo($x{bla}) *also* treats $x{bla} as an lvalue, but
doesn't eagerly create it. Instead, it passes a PVLV to foo(), which
can then create the hash element by assigning to $_[0].

Function arguments are usually taken by value but there's no type
information to say which arguments specifically are taken by
reference and so need the lvalue treatment. This patch adds two
extra tests for that behaviour, so at least they remain consistent.
#####

... and here are the added tests:

#####
+# Test to see if hash values are eagerly created
+{
+    sub myspringersub {}
+    myspringersub($spring3{"foo"});
+    is (scalar keys %spring3, 0, 'regular key was not eagerly created after sub call');
+    myspringersub(\$spring3{"foo"});
+    ok (exists $spring3{"foo"}, 'referenced key was eagerly created after sub call');
+}
+
#####

Now, there's a lot of analysis packed into that commit message -- but I'm not sure that all of it is reflected in the tests.

The commit message has two paragraphs.  The first focuses on lvalues; the second on function arguments.  The tests seem to me to focus only on the second.

I don't think there is anything really *wrong* here -- but I wonder if the combination of the commit message and the tests really clarifies the issue for people (like me) who are not experts in the perlguts.

Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=128301

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About