Skip to content

Commit fca7f5a

Browse files
committed
START phaser --> once statement prefix
1 parent 9f5a968 commit fca7f5a

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

S03-operators.pod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,10 +4950,10 @@ sign (which should not be confused with a normal assignment, because
49504950
the timing of the initialization depends on the natural lifetime of the
49514951
container, which in turn depends on which declarator you use).
49524952

4953-
my $foo = 1; # happens at the same time as normal assignment
4953+
my $foo = 1; # happens at execute time, like normal assignment
49544954
our $foo = 1; # happens at INIT time
49554955
has $foo = 1; # happens at BUILD time
4956-
state $foo = 1; # happens at START time
4956+
state $foo = 1; # happens at execute time, but only once
49574957
constant $foo = 1; # happens at BEGIN time
49584958

49594959
(Note that the semantics of C<our> are different from Perl 5, where the

S04-control.pod

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,16 +1345,15 @@ transition from one phase of computing to another. For instance,
13451345
a C<CHECK> block is called at the end of compiling a compilation
13461346
unit. Other kinds of phasers can be installed as well; these are
13471347
automatically called at various times as appropriate, and some of
1348-
them respond to various control exceptions and exit values:
1348+
them respond to various control exceptions and exit values.
1349+
Phasers marked with a C<*> can be used for their return value.
13491350

13501351
BEGIN {...}* at compile time, ASAP, only ever runs once
13511352
CHECK {...}* at compile time, ALAP, only ever runs once
13521353
FINAL {...}* at link time, ALAP, only ever runs once
13531354
INIT {...}* at run time, ASAP, only ever runs once
13541355
END {...} at run time, ALAP, only ever runs once
13551356

1356-
START {...}* on first ever execution, once per closure clone
1357-
13581357
ENTER {...}* at every block entry time, repeats on loop blocks.
13591358
LEAVE {...} at every block exit time (even stack unwinds from exceptions)
13601359
KEEP {...} at every successful block exit, part of LEAVE queue
@@ -1372,16 +1371,34 @@ them respond to various control exceptions and exit values:
13721371

13731372
COMPOSE {...} when a role is composed into a class
13741373

1375-
Those marked with a C<*> can also be used within an expression:
1374+
Some of the statement prefixes also behave a little bit like phasers,
1375+
but they run in-line with the executable code, so they are spelled
1376+
in lowercase. They parse the same as phasers:
1377+
1378+
do {...}* run a block or statement as a term
1379+
once {...}* run only once, suppressing additional evaluations
1380+
gather {...}* start a co-routine thread
1381+
lift {...}* use caller's semantics for operators
1382+
lazy {...}* return a promise to evaluate
1383+
sink {...}* eval eagerly and throw results away
1384+
try {...}* evaluate and trap exceptions
1385+
quietly {...}* evaluate and suppress warnings
1386+
contend {...}* attempt side effects under STM
1387+
async {...} start a subthread
1388+
1389+
Constructs marked with a C<*> have a run-time value, and if evaluated
1390+
earlier than their surrounding expression, they simply save their
1391+
result for use in the expression later when the rest of the expression
1392+
is evaluated:
13761393

13771394
my $compiletime = BEGIN { now };
1378-
our $temphandle = START { maketemp() };
1395+
our $temphandle = ENTER { maketemp() };
13791396

13801397
As with other statement prefixes, these value-producing constructs
13811398
may be placed in front of either a block or a statement:
13821399

13831400
my $compiletime = BEGIN now;
1384-
our $temphandle = START maketemp();
1401+
our $temphandle = ENTER maketemp();
13851402

13861403
In fact, most of these phasers will take either a block or a statement
13871404
(known as a I<blast> in the vernacular). The statement form can be
@@ -1393,7 +1410,7 @@ variables with the same scope as the preceding example, but run the
13931410
statements as a whole at the indicated time:
13941411

13951412
BEGIN my $compiletime = now;
1396-
START our $temphandle = maketemp();
1413+
ENTER our $temphandle = maketemp();
13971414

13981415
(Note, however, that the value of a variable calculated at compile
13991416
time may not persist under run-time cloning of any surrounding closure.)
@@ -1433,7 +1450,7 @@ the closure as its topic:
14331450
Apart from C<CATCH> and C<CONTROL>, which can only occur once, most
14341451
of these can occur multiple times within the block. So they aren't
14351452
really traits, exactly--they add themselves onto a list stored in the
1436-
actual trait (except for C<START>, which executes inline). So if you
1453+
actual trait. So if you
14371454
examine the C<ENTER> trait of a block, you'll find that it's really
14381455
a list of phasers rather than a single phaser. In general, initializing
14391456
phasers execute in order declared, while finalizing phasers execute in
@@ -1443,16 +1460,16 @@ the using module. (It is erroneous to depend on this order if the
14431460
module is used more than once, however, since the phasers are only
14441461
installed the first time they're noticed.)
14451462

1446-
The semantics of C<INIT> and C<START> are not equivalent to each
1463+
The semantics of C<INIT> and C<once> are not equivalent to each
14471464
other in the case of cloned closures. An C<INIT> only runs once for
1448-
all copies of a cloned closure. A C<START> runs separately for each
1465+
all copies of a cloned closure. A C<once> runs separately for each
14491466
clone, so separate clones can keep separate state variables:
14501467

14511468
our $i = 0;
14521469
...
14531470
$func = { state $x will start { $x = $i++ }; dostuff($i) };
14541471

1455-
But C<state> automatically applies "start" semantics to any initializer,
1472+
But C<state> automatically applies "once" semantics to any initializer,
14561473
so this also works:
14571474

14581475
$func = { state $x = $i++; dostuff($i) }
@@ -1462,11 +1479,11 @@ previous, and each clone maintains its own state of C<$x>, because that's
14621479
what C<state> variables do.
14631480

14641481
Even in the absence of closure cloning, C<INIT> runs before the
1465-
mainline code, while C<START> puts off the initialization till the
1482+
mainline code, while C<once> puts off the initialization till the
14661483
last possible moment, then runs exactly once, and caches its value
14671484
for all subsequent calls (assuming it wasn't called in sink context,
1468-
in which case the C<START> is evaluated once only for its side effects).
1469-
In particular, this means that C<START> can make use of any parameters
1485+
in which case the C<once> is evaluated once only for its side effects).
1486+
In particular, this means that C<once> can make use of any parameters
14701487
passed in on the first call, whereas C<INIT> cannot.
14711488

14721489
All of these phaser blocks can see any previously declared lexical
@@ -1496,7 +1513,7 @@ by C<next>. In particular, a C<last> bypasses evaluation of C<NEXT>
14961513
phasers.
14971514

14981515
[Note: the name C<FIRST> used to be associated with C<state>
1499-
declarations. Now it is associated only with loops. See the C<START>
1516+
declarations. Now it is associated only with loops. See the C<once>
15001517
above for C<state> semantics.]
15011518

15021519
Except for C<CATCH> and C<CONTROL> phasers, which run while an exception

0 commit comments

Comments
 (0)