What is defined by the saying “first-class entities” and how does it differ from “second-class entities”?
What does it mean when one says “regexes are first-class entities in modern perl when created with the qr// operator” (taken from Modern Perl: the book).
As MeNoMore correctly said, a first-class-entity is a data type of the language you can freely assign to variables etc. In Perl, these include:
Those can reside in the symbol table. The scalar slot can be occupied by various other types in addition:
Some of these entities have built-in constructors into tha language: Number and String literals for scalars, list notation for arrays and hashes,
[]and{}for anonymous array- and hashrefs, thesubkeyword for code, theopenfunction for IO objects, theformatbuiltin for formats, the reference operator for references, and theqr{}operator for regexes.There are language constructs in Perl that are not first-class entities and cannot be assigned to scalars or other first-class entities. For example, packages. This code doesn’t work:
Shell commands have their own builtins, but are no data objects, so this won’t work:
Instead, this statement should not terminate (and probably blow your memory).
Lists in Perl are language constructs, but no data types:
The builtin types in Perl can have coercion rules:
1/8or (c) to the number of keys in numerical context.qr(ab?c) eq "(?-xism:ab?c)", depending on the version of perl.Objects can be overloaded to show similar coercion rules through overloading.
In the case of regex-refs, a scalar containing such a reference can be used interchangeably with a regex literal, e.g. in the pattern
the regex could be replaced with
$regexif$regexis like above:For example, coderefs require more biolerplate code:
versus