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'insidepackage Fooleaves$Bar::Xalone.Does not touch lexicals.
my $xandour $xbehave differently here:ouris an alias to the package variable and is reset;myis 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#
resetwith no argument does not touch variables at all. It re-arms one-shotm?...?matches in the current package and nothing else. If you want to clear variables you must pass an argument.Lexicals are invisible to
reset. Amy $Xtotalis completely unaffected byreset 'X'; only the package variable$Xtotalis. Code that migrated fromour/package globals tomywill findresethas silently become a no-op for those variables.reset 'A-Z'is a foot-gun. In themainpackage that wipes@ARGV,@INC,%ENVand 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 withX, not just scalars. There is no way to restrict the reset to one sigil.Current package is decided at compile time via the enclosing
packagedeclaration, not by the caller. Aresetcall inside a module clears that module’s variables, not the caller’s.No regex argument form.
resettakes a string of letters, not a regex or a list of names. You cannot writereset qr/^X/orreset '$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
resetis a no-op — the call parses, returns1, and has no side effects. Package variables are not cleared andm?pattern?one-shot match state is not tracked. Code that relies onresetto 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. Prefermyand ordinarym//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 byresetlocal— dynamically scoped save/restore of a package variable, the usual answer when you want a temporary change without a manual cleanupm— the regularm//form;m?...?is the one-shot variant thatresetre-armscontinue— loop-end block where batch-reset of iteration state is conventionally placedpackage— declares which package’s variablesresetwill operate onundef— clear a single named variable, the per-variable alternative to a broadreset