Update `pop()` and `shift()` docs with examples #20780
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
Requested changes.
@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.
@scottchiefbaker i think you only added it for shift, but not for pop. But im a bit tired and maybe I am misreading it.
@demerphq updated with BEGIN stuff for pop() as well.
What else needs to change for this to land?
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
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.
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.
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.
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?
@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.
I agree with moving things down... I updated the PR.
Approved these changes.
demerphq
scottchiefbaker
Add some simple examples to
pop()andshift()and simplify some wording.