Carp argument‑truncation limits in Perl 5.34+ create ambiguous diagnostic output
0 reputation · 16 Mar 2021, 23:29 UTC
0 reputation · 16 Mar 2021, 23:29 UTC
When diagnosing a failed Perl deployment, the goal is to retain enough argument detail in Carp‑generated stack traces (e.g., from Carp::confess) to pinpoint which parameter caused the failure across Perl versions 5.30 through 5.38.
However, the introduction of $Carp::MaxArgLen in 5.30 and $Carp::MaxArgNum in 5.34 creates a compatibility boundary: the two limits can combine to truncate arguments to zero length or omit them entirely, and the documentation marks this behavior as experimental with no guaranteed default, while individual CPAN modules may locally reset the variables.
What combination of $Carp::MaxArgLen and $Carp::MaxArgNum should be considered safe to preserve diagnostic usefulness?
How can a deployment script reliably detect whether the current Perl version will truncate argument output before invoking Carp?
To preserve full argument detail in Carp::confess output on Perl 5.34 and later, set both $Carp::MaxArgLen and $Carp::MaxArgNum to 0 (or undef) after all modules have been loaded. A value of zero disables the corresponding truncation limit. On Perl 5.30–5.33, only $Carp::MaxArgLen exists; guard any reference to $Carp::MaxArgNum with a defined check.
Leaving either variable at its default can combine with the other to truncate arguments to zero length or omit them entirely. A deployment script can detect this risk by loading all application and CPAN dependencies first, then checking whether either variable is defined and greater than zero. If either condition is true, Carp may shorten or omit arguments. For certainty, run a controlled smoke test: pass a known long string to Carp::confess, capture the output, and confirm the full string appears unchanged.
Confirmed: $Carp::MaxArgLen was introduced in Perl 5.30 with a default of 200. $Carp::MaxArgNum was introduced in Perl 5.34 with a default of 20. Setting either to zero removes that limit. These are package variables in the Carp namespace, and their behavior is documented as experimental.
Likely but version-dependent: The exact truncation marker (for example, an ellipsis or shortened suffix) and the combined effect when both limits are active can vary between maintenance releases. Treat the precise rendering as something to verify on your target interpreter rather than assuming a fixed format.
defined $Carp::MaxArgLen && $Carp::MaxArgLen > 0 and, if available, defined $Carp::MaxArgNum && $Carp::MaxArgNum > 0.local block around the failing code path.Changing these variables globally affects every module that calls Carp. If third-party code relies on the defaults for log brevity, prefer a localized change or a wrapper around your own diagnostic calls. Because a CPAN module may reassign the variables at load time, perform detection after the final use or require statement in your startup sequence.
One detail that would change the recommendation: Do any modules in your stack explicitly assign to $Carp::MaxArgLen or $Carp::MaxArgNum when they are loaded? If so, the detection and override must run after that module is loaded, not before.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.