← Back to Pull requests

Update `pop()` and `shift()` docs with examples #20780

Merged
scottchiefbaker merged 1 files (51 lines) on Mar 29, 2023 17:56 UTC
View on GitHub
scottchiefbaker scottchiefbaker commented on Feb 08, 2023 22:54 UTC
Author

Add some simple examples to pop() and shift() and simplify some wording.

Diff: 51 lines (+37 / -14) across 1 files Reviewer: demerphq Review duration: 70262m
All Vouch review confidence checks passed
Residual risk 0.07 = Change Risk (0.27) × (1 − Review Confidence (0.74)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.27 · 51 lines diff across 1 files
0.27
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.51 · 5 substantive comment(s)
0.51
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
74%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.236
Real PR Conversation Timeline
15 items
demerphq demerphq commented on code on Feb 09, 2023 08:54 UTC
Code Review
pod/perlfunc.pod:1
@@ -7547,14 +7556,23 @@ X<shift>
 
 =for Pod::Functions remove the first element of an array, and return it
 
-Shifts the first value of the array off and returns it, shortening the
-array by 1 and moving everything down.  If there are no elements in the
-array, returns the undefined value.  If ARRAY is omitted, shifts the
-L<C<@_>|perlvar/@_> array within the lexical scope of subroutines and
-formats, and the L<C<@ARGV>|perlvar/@ARGV> array outside a subroutine
-and also within the lexical scopes
-established by the C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>,

I think this part of the documentation was added because many people view these special blocks as special forms of subroutine. Also there is ambiguity as to what happens when a BEGIN (for instance) is embedded inside of a sub. So I don't think it should be entirely removed, but at the same time I think your text is better overall. Maybe we can add a special subsection that covers both shift and unshift and explains this? Eg, consider

perl -le'sub test { BEGIN { print "BEGIN:", pop } print "sub test: ", pop } test(42)' argv
BEGIN:argv
sub test: 42

demerphq demerphq requested changes on Feb 09, 2023 08:55 UTC
Changes Requested Reviewer

Requested changes.

scottchiefbaker scottchiefbaker commented on Feb 09, 2023 16:54 UTC
Author

@demerphq good call. The original text was kind of convoluted so I removed the section on BEGIN, INIT, and CHECK. I went ahead and added back that section on the bottom so it's less confusing to novices.

demerphq demerphq commented on Feb 09, 2023 17:14 UTC
Contributor

@scottchiefbaker i think you only added it for shift, but not for pop. But im a bit tired and maybe I am misreading it.

scottchiefbaker scottchiefbaker commented on Feb 09, 2023 19:49 UTC
Author

@demerphq updated with BEGIN stuff for pop() as well.

scottchiefbaker scottchiefbaker commented on Feb 13, 2023 18:24 UTC
Author

What else needs to change for this to land?

demerphq demerphq commented on code on Feb 17, 2023 13:32 UTC
Code Review
pod/perlfunc.pod:1
@@ -7547,14 +7559,26 @@ X<shift>
 
 =for Pod::Functions remove the first element of an array, and return it
 
-Shifts the first value of the array off and returns it, shortening the
-array by 1 and moving everything down.  If there are no elements in the
-array, returns the undefined value.  If ARRAY is omitted, shifts the
-L<C<@_>|perlvar/@_> array within the lexical scope of subroutines and
-formats, and the L<C<@ARGV>|perlvar/@ARGV> array outside a subroutine
-and also within the lexical scopes
-established by the C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>,
-C<UNITCHECK {}>, and C<END {}> constructs.
+Removes and returns the B<first> element of an array. This shortens the
+array by one and moves everything down.
+
+    my @arr  = ('cat', 'dog');
+    my $item = shift(@arr); # 'cat'
+
+    # @arr is now ('dog');
+
+Returns C<undef> if the array is empty. If ARRAY is omitted, C<shift>
+operates on the C<@ARGV> array in the main program, and the C<@_> array
+in subroutines.

I think i would move the "If ARRAY is ommitted" part down to join the part about eval and the special blocks.

But what I was really wondering is if you can somehow eliminate the need to duplicate this bit of docs, by maybe making one of the two refer to the other.

Eg, Perhaps "shift" would say

If ARRAY is omitted, C<shift> operates on the the C<@_> array
in normal subroutines, and on the C<@ARGV> array in the main program, and 
in C<eval STRING>, C<BEGIN {}>,  C<INIT {}>, and C<CHECK {}> blocks.

And then in the docs for pop it could say

If ARRAY is omitted, C<pop> defaults to using either C<@_> or C<@ARGV>, 
exactly as C<shift> does, see L</shift> for details.

(module a correct L<> link for shift of course).

Sorry it took me so long to get back to you, I was out of action for a while

Grinnz Grinnz commented on code on Feb 17, 2023 18:03 UTC
Code Review
pod/perlfunc.pod:1
@@ -7547,14 +7559,26 @@ X<shift>
 
 =for Pod::Functions remove the first element of an array, and return it
 
-Shifts the first value of the array off and returns it, shortening the
-array by 1 and moving everything down.  If there are no elements in the
-array, returns the undefined value.  If ARRAY is omitted, shifts the
-L<C<@_>|perlvar/@_> array within the lexical scope of subroutines and
-formats, and the L<C<@ARGV>|perlvar/@ARGV> array outside a subroutine
-and also within the lexical scopes
-established by the C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>,
-C<UNITCHECK {}>, and C<END {}> constructs.
+Removes and returns the B<first> element of an array. This shortens the
+array by one and moves everything down.
+
+    my @arr  = ('cat', 'dog');
+    my $item = shift(@arr); # 'cat'
+
+    # @arr is now ('dog');
+
+Returns C<undef> if the array is empty. If ARRAY is omitted, C<shift>
+operates on the C<@ARGV> array in the main program, and the C<@_> array
+in subroutines.

I don't think it's necessarily worth consolidating with a cross link just to save a line of description. For small things it can be better to just write what people need to know in the place they need to know it.

demerphq demerphq commented on code on Feb 18, 2023 06:08 UTC
Code Review
pod/perlfunc.pod:1
@@ -7547,14 +7559,26 @@ X<shift>
 
 =for Pod::Functions remove the first element of an array, and return it
 
-Shifts the first value of the array off and returns it, shortening the
-array by 1 and moving everything down.  If there are no elements in the
-array, returns the undefined value.  If ARRAY is omitted, shifts the
-L<C<@_>|perlvar/@_> array within the lexical scope of subroutines and
-formats, and the L<C<@ARGV>|perlvar/@ARGV> array outside a subroutine
-and also within the lexical scopes
-established by the C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>,
-C<UNITCHECK {}>, and C<END {}> constructs.
+Removes and returns the B<first> element of an array. This shortens the
+array by one and moves everything down.
+
+    my @arr  = ('cat', 'dog');
+    my $item = shift(@arr); # 'cat'
+
+    # @arr is now ('dog');
+
+Returns C<undef> if the array is empty. If ARRAY is omitted, C<shift>
+operates on the C<@ARGV> array in the main program, and the C<@_> array
+in subroutines.

I dont feel very strongly about cross linking or not, but i do feel strongly that the different bits of docs for the "no array specifed" case should be in one sentence, and not part of a sentence referencing what happens if the array is empty.

The cross linking suggestion was mostly to avoid the two descriptions getting out of sync with each other as I think they were previously, and to avoid double bookkeeping, but I don't feel that strongly about it.

demerphq demerphq commented on Feb 19, 2023 09:52 UTC
Contributor

btw, @scottchiefbaker if I have annoyed you with my feedback on this please feel free to return the favour in https://github.com/Perl/perl5/pull/20823 :-) Docs are hard. but its valuable to get them right! Your feedback on readability appreciated.

demerphq demerphq commented on Mar 28, 2023 20:40 UTC
Contributor

This has been stalled for quite a while. It would be a shame if 5.38 didnt include this doc tweak. @scottchiefbaker do you want me to pick up the loose ends?

scottchiefbaker scottchiefbaker commented on Mar 29, 2023 16:14 UTC
Author

@demerphq thanks for reminding me this was pending. Re-reading things, I agree with your conclusion. I moved the "if ARRAY is omitted" sections to the bottom as they're not the important part of that documentation and shouldn't be the first thing a novice reads. PR has been updated.

scottchiefbaker scottchiefbaker commented on code on Mar 29, 2023 16:14 UTC
Code Review
pod/perlfunc.pod:1
@@ -7547,14 +7559,26 @@ X<shift>
 
 =for Pod::Functions remove the first element of an array, and return it
 
-Shifts the first value of the array off and returns it, shortening the
-array by 1 and moving everything down.  If there are no elements in the
-array, returns the undefined value.  If ARRAY is omitted, shifts the
-L<C<@_>|perlvar/@_> array within the lexical scope of subroutines and
-formats, and the L<C<@ARGV>|perlvar/@ARGV> array outside a subroutine
-and also within the lexical scopes
-established by the C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>,
-C<UNITCHECK {}>, and C<END {}> constructs.
+Removes and returns the B<first> element of an array. This shortens the
+array by one and moves everything down.
+
+    my @arr  = ('cat', 'dog');
+    my $item = shift(@arr); # 'cat'
+
+    # @arr is now ('dog');
+
+Returns C<undef> if the array is empty. If ARRAY is omitted, C<shift>
+operates on the C<@ARGV> array in the main program, and the C<@_> array
+in subroutines.

I agree with moving things down... I updated the PR.

demerphq demerphq approved these changes on Mar 29, 2023 16:30 UTC
Approved Reviewer

Approved these changes.

Reviewers
Assignees
scottchiefbaker scottchiefbaker
Labels
risk: low confidence: 74.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 26.6% (LOW) · Higher = larger blast radius
26.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 74.5% · Lower = rushed or shallow review
74.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.8% · Safe to merge
6.8%
Repository