Skip to content

Conversation

@JRaspass
Copy link
Contributor

@JRaspass JRaspass commented Sep 16, 2025

B::Concise tries hard to hide its own BEGIN blocks when asked to report BEGIN blocks in end-user's code. It does this by maintaining a count of each BEGIN/use/no (and those of the dependencies) and then splicing these BEGIN blocks off before printing. However this count has drifted over time, which this commit fixes.

Before:

$ perl -MO=Concise,BEGIN,-exec -e 'BEGIN {1} BEGIN {2}'
... BEGIN 1-8 ...
BEGIN 9:
26 <;> nextstate(main 2 -e:1) v
27 <$> const[IV 1] s
28 <1> leavesub[1 ref] K/REFC,1
BEGIN 10:
29 <;> nextstate(main 4 -e:1) v
2a <$> const[IV 2] s
2b <1> leavesub[1 ref] K/REFC,1
-e syntax OK

After:

$ perl -MO=Concise,BEGIN,-exec -e 'BEGIN {1} BEGIN {2}'
BEGIN 1:
1  <;> nextstate(main 2 -e:1) v
2  <$> const[IV 1] s
3  <1> leavesub[1 ref] K/REFC,1
BEGIN 2:
4  <;> nextstate(main 4 -e:1) v
5  <$> const[IV 2] s
6  <1> leavesub[1 ref] K/REFC,1
-e syntax OK

The tests in ext/B/t/optree_specials.t now correctly only show the things being tested, but that also meant removing the bogus final test, as with this fix the code under test outputs nothing as it has no phase blocks in it.

  • This set of changes requires a perldelta entry, and it is included.

@tonycoz
Copy link
Contributor

tonycoz commented Sep 18, 2025

I think it needs a perldelta entry, I can see people who've written code expecting the extra erroneous BEGINs being surprised (but hopefully not dismayed) by this.

As to what's in the perldelta entry, just add a new entry to Updated Modules and Errata matching the example pattern describing the change. It doesn't need to be detailed.

It looks like a reasonable change.

B::Concise tries hard to hide its own BEGIN blocks when asked to report
BEGIN blocks in end-user's code. It does this by maintaining a count of
each BEGIN/use/no (and those of the dependencies) and then splicing
these BEGIN blocks off before printing. However this count has drifted
over time, which this commit fixes.

Before:

  $ perl -MO=Concise,BEGIN,-exec -e 'BEGIN {1} BEGIN {2}'
  ... BEGIN 1-8 ...
  BEGIN 9:
  26 <;> nextstate(main 2 -e:1) v
  27 <$> const[IV 1] s
  28 <1> leavesub[1 ref] K/REFC,1
  BEGIN 10:
  29 <;> nextstate(main 4 -e:1) v
  2a <$> const[IV 2] s
  2b <1> leavesub[1 ref] K/REFC,1
  -e syntax OK

After:

  $ perl -MO=Concise,BEGIN,-exec -e 'BEGIN {1} BEGIN {2}'
  BEGIN 1:
  1  <;> nextstate(main 2 -e:1) v
  2  <$> const[IV 1] s
  3  <1> leavesub[1 ref] K/REFC,1
  BEGIN 2:
  4  <;> nextstate(main 4 -e:1) v
  5  <$> const[IV 2] s
  6  <1> leavesub[1 ref] K/REFC,1
  -e syntax OK

The tests in ext/B/t/optree_specials.t now correctly only show the
things being tested, but that also meant removing the bogus final test,
as with this fix the code under test outputs nothing as it has no phase
blocks in it.
@JRaspass JRaspass force-pushed the fix-b-concise-begin-splice branch from 92386bf to 7b2bf5a Compare September 18, 2025 02:24
@JRaspass
Copy link
Contributor Author

Amended to include a perldelta.

Copy link
Contributor

@leonerd leonerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM I guess.

I agree this is an improvement on the prior broken state, but it still feels very fragile. I wonder if there's a more robust way this could be handled that didn't rely on it having to maintain a recursively-correct count that knows the number of all the BEGIN blocks it itself uses. Not to block this particular improvement, but perhaps something we can think about in future.

@khwilliamson khwilliamson merged commit 52427ad into Perl:blead Sep 20, 2025
33 checks passed
@tonycoz
Copy link
Contributor

tonycoz commented Sep 21, 2025

but it still feels very fragile.

When I review this I looked into whether we could automatically calculate the count at load time, but I think if B::Concise isn't loaded first that count is going to include other BEGIN blocks.

Though if B::Concise isn't loaded first we're probably going to strip the wrong BEGIN blocks, so I don't know.

Maybe we could add trivial BEGINs to the start and end of B::Concise and use those as brackets to select the BEGINs to eliminate.

@JRaspass
Copy link
Contributor Author

Maybe we could add trivial BEGINs to the start and end of B::Concise and use those as brackets to select the BEGINs to eliminate.

@tonycoz that's what #23743 does, though I didn't consider code being loaded before B::Concise, I think I was mainly concerned with -MO= usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants