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
push on scalar forbidden: why? #16506
Comments
From perl-diddler@tlinx.orgCreated by perl-diddler@tlinx.orgIf a scaler is null or an ARRAY ref, why shouldn't perl be ex: I'm aware an alternate way to do this was added, but perl, like many This is a perfect example of perl autovivifying an array Even more curious is that perl is a permissive language. If you Can perl be allowed to do the right thing when perl already knows Thanks, Perl Info
|
From @GrinnzThis was an experimental feature added in 5.14: https://metacpan.org/pod/perl5140delta#Array-and-hash-container-functions-accept-references and then removed in 5.24: https://metacpan.org/pod/perl5240delta#The-autoderef-feature-has-been-removed . One of the biggest problems with it is that there's no correct way to handle an object with both hash and array overloads defined. |
The RT System itself - Status changed from 'new' to 'open' |
From @GrinnzOn Fri, 13 Apr 2018 16:43:47 -0700, grinnz@gmail.com wrote:
Also, adding an @ to dereference it is a very simple workaround, and will autovivify an undefined scalar used in this manner. |
From @philiprbrenanThe initial experimental form was a good idea but too broad because of the for($a) {...} But when it was removed the removal was perhaps too broad as well and my $a; but in this case the point is moot so the scheme survives. And in cases my %a = (0=>undef); it would make code much more readable. If Linda's scheme can be shown to be safe and unambiguous on all cases of |
From @GrinnzOn Fri, 13 Apr 2018 17:32:21 -0700, philiprbrenan@gmail.com wrote:
Just for reference, this can also be written as push $a{0}->@* on Perl 5.24+ (or slightly earlier with the experimental feature). That might be more palatable to some when it comes to long dereference chains. |
From perl-diddler@tlinx.orgDan Book via RT wrote:
I disagree, in so much as if there is ambiguity, it is IMO, inherently an |
From perl-diddler@tlinx.orgPhilip R Brenan via RT wrote:
?? If there is no way to interpret the expression without an error However, the above is legal perl code even if '$a' is Since this is legal: for ([1,2,3],[4,5,6]) {...} (with loop run twice: _=[1,2,3], then _=[4,5,6]). then this must be treated the same way (and is): for ([1,2,3]) {...} Are you saying there is some situation where somehow If so, I don't see it. I.e. I don't see how there would be
If '$a' is a ref to a hash: In that case, treating it as "%$a" would seem the only valid
What other cases was it removed from? I don't see a problem allowing it anywhere where: 1) it would be an error to NOT dereference the ref, I'm sure this was thoroughly 'hashed' out at the time, so If there was ambiguity before, and it was allowed, And if the code was already "legal", then dereferencing So what am I missing? |
From perl-diddler@tlinx.orgPhilip R Brenan wrote:
It should never be "ambiguous". According to the language, it prog='P "perl-%s:", $]; then (in shell & various perl-bin dirs): (probs building 5.6.2) perl-5.8.9/usr/bin> PATH=$PWD:$PATH perl -MP -CSA -e "$prog" perl-5.10.1/usr/bin> PATH=$PWD:$PATH perl -MP -CSA -e "$prog" perl-5.12.5/usr/bin> PATH=$PWD:$PATH perl -MP -CSA -e "$prog" (had probs building 5.14)
(5.18-5.22 not built/tested) perl-5.24.0/usr/bin> PATH=$PWD:$PATH perl -MP -CSA -e "$prog" /perl-5.26.0/usr/bin> PATH=$PWD:$PATH perl -MP -CSA -e "$prog" (Note: I have 'P' installed from cpan in the versions Except for the untested versions, it seems like there is Was the problem in one of the untested versions? |
From perl-diddler@tlinx.orgIs perl in a nice version control system where a patch or code-push could Or if a problem developed, could it be bisected like the kernel dev tree? |
From @doughera88On Tue, Apr 17, 2018 at 05:29:58PM -0700, L A Walsh wrote:
Yes, see perldoc pod/perlhack.pod
Yes, see perldoc Porting/bisect-runner.pl -- |
From @xsawyerxHi Linda, Whether there are situations that are unambiguous for the compiler or I think this ticket should be respectfully rejected. On 04/14/2018 01:31 AM, Linda Walsh (via RT) wrote:
|
From perl-diddler@tlinx.orgSawyer X via RT wrote:
Might I ask, how it is confusing for a developer? Most languages perl -e 'my $a={}; I'm not sure what you mean by providing for auto-deref in some How would that be unclear for a perl developer who probably But to make it easier, it looks like perfunc holds the answer. It I may have missed something, but the dereference feature would only If you want to complain about it working in some places and not The manpage shows: Functions for real %HASHes As far as I can tell, these should be the entirety of functions Similarly, the open call can take a scalar with a value of 'undef' Similarly, in any location where an ARRAY or HASH is required, The feature was enabled for 2 years, and triggered When one of the main complaints against perl (as being read by others or I don't see this being some vague nebulous issue the way some Perhaps Sawyer is thinking about something other than what this |
From @GrinnzOn Thu, Apr 19, 2018 at 6:09 PM, L A Walsh <perl-diddler@tlinx.org> wrote:
each, keys, and values work on both arrays and hashes. As does exists and -Dan |
From perl-diddler@tlinx.orgDan Book via RT wrote:
Yes...for dereferencing to work in this case, the scalar would have to Only functions that require an ARRAY *XOR* HASH could take undef. Off Does that clarify the intent of my proposal? It may be the case that |
From @iabynOn Thu, Apr 19, 2018 at 06:37:26PM -0700, L A Walsh wrote:
But when you have a scalar reference which is overloaded, perl can't know use overload my $r = bless []; push $r, 3; # prints "array derdef", modifies @real_a What should perl do in the last case? -- |
From @philiprbrenanPresumably it would return () as $r is still a reference to an empty array If push $r, 3 were always interpreted identically to push @$r, 3 we would use Data::Dump qw(dump); my $r = bless []; push *@$r*, 3; I.e. $r is not empty, which illustrates that push $r and push @$r would not I urge that both of these problems can be overcome by Linda's statement of *Push gets its array either directly or via a reference. *In this example: push $r, 3 would affect only $r, not @real_a, because no array dereference would be On Sat, Apr 21, 2018 at 4:37 PM, Dave Mitchell <davem@iabyn.com> wrote:
-- Phil <http://www.appaapps.com/howToWriteAnApp.html> Philip R Brenan <http://www.appaapps.com/howToWriteAnApp.html> |
From @iabynOn Sat, Apr 21, 2018 at 06:17:09PM +0100, Philip R Brenan wrote:
Then you've just broken overloading. For example, suppose someone writes sub push_foo { this method works exactly as documented and expected, until the day -- |
From perl-diddler@tlinx.orgDave Mitchell wrote:
Two answers. The first answer: it never gets that far. PERL5OPT="" perl /tmp/ovld.pl 2nd answer -- this was brought up by Dan Brook. I answered: -------- Original Message -------- Dan Book wrote:
I disagree, in so much as if there is ambiguity, it is IMO, inherently an In any case where there is ambiguity, the compiler must throw an error(like): Ambiguous dereference @ line 13: Sigil required. My suggestion was to auto-dereference references where perl knew the type The 2nd part was to allow a scalar that holds 'undef' to be my $ref would autovivify a temporary array pointed to by '$ref', as push requires Similarly: would act identically to: my $val=pop @$ref; Any case where perl has an ambiguous choice, must be flagged as an |
From @iabynOn Sat, Apr 21, 2018 at 02:10:05PM -0700, L A Walsh wrote:
That's because specifically to get round the problem of the overloading
Since perl almost never knows the type of a reference at compile-time,
Do you mean as a compile-time error or a run-time error? -- |
From perl-diddler@tlinx.orgDave Mitchell via RT wrote:
It would make the new perl incompatible with old code. One of the
I'm guessing where this feature was really meant to "fix" things was as I think I understand it, only if the ref is in a class can it Type of argument to keys on reference must be unblessed hashref or Again, I think checking if there was a bless on the ref was a bit of a In that scenario, blessed or not, the only way it could be resolved is my @k; or something similar. If you are looking for a compile time "
It works for giving a runtime error just like: my $r={}; does now. It can't be detected until runtime if you want to use Adding the "overload" module simply allow more options for runtime errors
run-time -- just as the above example generates now. No algorithm will work for all input. That doesn't mean you throw out |
From @xsawyerxI'm currently in no position to have a long discussion on this so I'll We tried autoderef. We removed it. We are not returning it. On 04/20/2018 12:09 AM, L A Walsh wrote:
|
From @iabynOn Sat, Apr 21, 2018 at 07:56:48PM -0700, L A Walsh wrote:
The big difference between the two is that there's (in general) no So there's a very modest gain for allowing push $r, while there are many This feature, and its removal, has already been extensively discussed; -- |
Migrated from rt.perl.org#133109 (status was 'open')
Searchable as RT133109$
The text was updated successfully, but these errors were encountered: