Skip to content

Instantly share code, notes, and snippets.

@cognominal
Last active March 25, 2021 22:52
Show Gist options
  • Save cognominal/191076 to your computer and use it in GitHub Desktop.
Save cognominal/191076 to your computer and use it in GitHub Desktop.
=head2 conditional dereference
A conditional dereference is a pattern matching and data structure
walking at once that blends well with laziness and functional
programming.
Conditional dereference are special dereference operators that work
as their not conditional counterpart if the dereference value des not
exist. Otherwise, the C<<< postcircumfix:<< < >! >> >>>, operator
thows an exception and the C<<< postcircumfix:<< < >? >> >>>
silently aborts. In case of a silent abortion the flow proceeds
directly to the next statement, but if the conditional dereference
expression is evaluated in boolean context, its value is deemed to be
false and the statement proceeds according to that value.
Conditional dereference is useful in a function argument. An C<only>
subroutine or method is not called if any of its arguments evaluation
silently aborts. In such a case, it returns the undefined value of its
return type.
XXX What about multis?
If the value dereferenced with C<<< postcircumfix:<< =< > >> >>> does
not exist, the value is deemed to be the default value that would be
produced in that context.
Also, if the statement starts by a loop modifier and proceeds without
abortion, the control flow goes according to the said modifier.
The following example walks a parse tree to generate html
code. Whithin the loop, if the current node represents a header, that
is C<< .<head> >> is defined, an html string is interpolated. An
exception is thrown if C<< .<head><number> >> does not exist.
If C<< .head<content> >> is missing, the value produced by
C<< .=<content> >> is the default value expected in that context.
for (@array) {
...
next $html .= ".<h$nr>" ~ .=<content>? ~ "</$nr>" if $nr = .<head>?<number>!
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment