Skip to content

Instantly share code, notes, and snippets.

@cognominal
Created September 22, 2009 12:57
Show Gist options
  • Save cognominal/191045 to your computer and use it in GitHub Desktop.
Save cognominal/191045 to your computer and use it in GitHub Desktop.
conditional dereference
=head2 conditional dereference
A conditional dereference is a pattern matching and data structure
walking at once that blends well with laziness and functional
programming. A conditional dereference expression is denoted by
prefixing a dereference circumfix with either a C<?>, a C<??> or
C<?=>. When the dereference value does not exist the statement is
silently aborted if the prefix is C<?> or throws an exception if the
prefix is C<??>. 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 a prefix C<?=> 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> >> if is defined, a 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.
XXX not sure what adverb is needed to interpolate C<< .?=<content> >> in the string.
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