Misc

reset#

Clear every package variable whose name begins with one of a set of letters, and re-arm one-shot m?pattern? matches.

reset is a holdover from Perl 4’s globals-only world. In modern code it survives in two narrow roles: re-arming m?...? one-match regexes so they fire again, and — at the end of a loop’s continue block — wiping a batch of package variables before the next iteration. Its reach is limited to the current package and to package variables; anything declared with my is untouched.

Synopsis#

reset EXPR
reset

EXPR is a string interpreted as a set of single characters, with hyphens allowed as ranges: 'X', 'a-z', 'abc', 'A-Za-z'. Every scalar, array, and hash in the current package whose name starts with one of those letters is reset to its pristine, undefined state.

With no argument, only m?pattern? one-match searches in the current package are re-armed — variables are not touched.

What you get back#

Always 1. The return value carries no success signal; there is nothing for reset to fail at.

Global state it touches#

  • Every package variable in the current package whose name starts with a listed letter: scalars, arrays, and hashes all together. Scalars become undef, arrays and hashes become empty.

  • One-match regexes (m?pattern?) compiled in the current package — their “already matched” flag is cleared so the next evaluation matches again.

  • Does not cross package boundaries. reset 'X' inside package Foo leaves $Bar::X alone.

  • Does not touch lexicals. my $x and our $x behave differently here: our is an alias to the package variable and is reset; my is not.

Examples#

Re-arm all one-shot matches in the current package — the typical modern use of reset:

reset;

Clear every package variable starting with X in the current package:

package Stats;
our $Xtotal = 0;
our @Xsamples = (1, 2, 3);
our %Xindex = (a => 1);
reset 'X';
# $Xtotal, @Xsamples, %Xindex are all emptied

Reset at the bottom of a loop so the next iteration starts clean:

while (more_work()) {
    process();
} continue {
    reset 'abc';   # wipe $a*, @b*, %c* package vars for next pass
}

A character range — lower-case variables only:

reset 'a-z';

Combining individual letters and ranges:

reset 'a-cXY';    # a, b, c, X, Y

Edge cases#

  • reset with no argument does not touch variables at all. It re-arms one-shot m?...? matches in the current package and nothing else. If you want to clear variables you must pass an argument.

  • Lexicals are invisible to reset. A my $Xtotal is completely unaffected by reset 'X'; only the package variable $Xtotal is. Code that migrated from our/package globals to my will find reset has silently become a no-op for those variables.

  • reset 'A-Z' is a foot-gun. In the main package that wipes @ARGV, @INC, %ENV and every other upper-case global, almost certainly not what was intended.

  • Scalars, arrays, and hashes share one namespace here. reset 'X' clears every variable of every sigil whose name starts with X, not just scalars. There is no way to restrict the reset to one sigil.

  • Current package is decided at compile time via the enclosing package declaration, not by the caller. A reset call inside a module clears that module’s variables, not the caller’s.

  • No regex argument form. reset takes a string of letters, not a regex or a list of names. You cannot write reset qr/^X/ or reset '$Xtotal' — the latter tries to reset variables starting with $, X, t, o, a, l, which is almost never what the author meant.

Differences from upstream#

  • pperl’s reset is a no-op — the call parses, returns 1, and has no side effects. Package variables are not cleared and m?pattern? one-shot match state is not tracked. Code that relies on reset to wipe a batch of globals at the end of a loop, or to re-fire a one-match regex, will not see that behaviour under pperl. Prefer my and ordinary m// regexes — both work identically on pperl and upstream, and are the recommended idiom upstream as well.

See also#

  • my — lexical variables; preferred in all new code and unaffected by reset

  • local — dynamically scoped save/restore of a package variable, the usual answer when you want a temporary change without a manual cleanup

  • m — the regular m// form; m?...? is the one-shot variant that reset re-arms

  • continue — loop-end block where batch-reset of iteration state is conventionally placed

  • package — declares which package’s variables reset will operate on

  • undef — clear a single named variable, the per-variable alternative to a broad reset