--- name: reset signature: 'reset EXPR' signatures: - 'reset EXPR' - 'reset' since: 5.0 status: documented categories: ["Misc"] --- ```{index} single: reset; Perl built-in ``` *[Misc](../perlfunc-by-category)* # reset Clear every package variable whose name begins with one of a set of letters, and re-arm one-shot [`m?pattern?`](m) 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`](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`](my) is untouched. ## Synopsis ```perl 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`](undef), arrays and hashes become empty. - **One-match regexes** ([`m?pattern?`](m)) 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`: ```perl reset; ``` Clear every package variable starting with `X` in the current package: ```perl 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: ```perl while (more_work()) { process(); } continue { reset 'abc'; # wipe $a*, @b*, %c* package vars for next pass } ``` A character range — lower-case variables only: ```perl reset 'a-z'; ``` Combining individual letters and ranges: ```perl 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?...?`](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`](../perlvar), [`@INC`](../perlvar), [`%ENV`](../perlvar) 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`](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?`](m) 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`](my) and ordinary [`m//`](m) regexes — both work identically on pperl and upstream, and are the recommended idiom upstream as well. ## See also - [`my`](my) — lexical variables; preferred in all new code and unaffected by `reset` - [`local`](local) — dynamically scoped save/restore of a package variable, the usual answer when you want a temporary change without a manual cleanup - [`m`](m) — the regular `m//` form; `m?...?` is the one-shot variant that `reset` re-arms - [`continue`](continue) — loop-end block where batch-reset of iteration state is conventionally placed - [`package`](package) — declares which package's variables `reset` will operate on - [`undef`](undef) — clear a single named variable, the per-variable alternative to a broad `reset`