New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
delete function error with ternary operator #15771
Comments
From gomesbascoy@gmail.comperldoc for the delete function indicates the following form: delete EXPR Here is an example of the issue I have found: my %a = (a=>123, b=>321); delete (1 ? $a{a} : $a{b}); # OK my %a = (a=>123, b=>321); my $n = 1; delete ($n ? $a{a} : $a{b}); # FAIL More specifically, the error message is: "delete argument is not a HASH or Some folks on #perl @freenode mentioned that my first example only works Thanks! |
From @jkeenanOn Tue, 13 Dec 2016 21:16:52 GMT, gomesbascoy@gmail.com wrote:
Confirmed.
Agreed. -- |
The RT System itself - Status changed from 'new' to 'open' |
From zefram@fysh.orgBrian Gomes Bascoy wrote:
That wording seems to have misled you. The arbitrary complexity
"delete $a{$n ? 'a' : 'b'}" would be legal.
Yes. The early constant folding of the conditional means that the -zefram |
From gomesbascoy@gmail.comOn Wed, 14 Dec 2016 15:26:48 -0800, zefram@fysh.org wrote:
I can totally see people using this kind of conditional compilation, probably with constants instead of plain numeric literals. At the same time I think that the optimization phase shouldn't change the semantics of the code being interpreted. Using compile-time conditionals as a sort of macro language is just a hack, not something that it's part of the Perl language itself but just a casual result of it's current implementation, or at least that's my point of view :) Consider the following example: That piece of code is ambiguous because foo was not defined. If foo is a numeric constant (e.g. "use constant foo => 9;") that code evaluates just fine. On the other hand, if foo is a subroutine that returns a number (e.g. "sub foo { 9 }") it fails because it requires run-time evaluation. IIRC constants are inlined subroutines so I can see how someone could go from a constant foo to a more complex subroutine foo. I personally think that the delete syntax may be inherently inconsistent; in JavaScript, for instance, there is a similar delete operator that deletes a property from a given object, but the syntax is more restrictive because you really have to give obj.prop... if you try a ternary operator there the interpreter will reduce obj.prop to its value before it reaches delete (maybe because of its evaluation strategy, or by ECMAScript's specification, I don't know). In my opinion other scripting languages like Perl6, Lua or even Python have much better approaches. Maybe extending delete's syntax to something like "delete hash, key"? but it's too late for that. Thanks |
From @demerphqOn 15 December 2016 at 19:57, Brian Gomes Bascoy via RT
FWIW, we used to have similar issues with ternaries and split, Yves -- |
From @jkeenanOn Fri, 16 Dec 2016 07:24:43 GMT, demerphq wrote:
Is this a problem about which, in the short run, we should write some cautionary documentation? -- |
Migrated from rt.perl.org#130343 (status was 'open')
Searchable as RT130343$
The text was updated successfully, but these errors were encountered: