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).
From Wikipedia: In programming language design, a first-class citizen (also object, entity, or value), in the context of a particular programming language, is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable.
second-class object (plural second-class objects) (programming, languages) An entity of which the value can be passed as a parameter, but that can neither be returned from a function, nor be assigned to a variable.
A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.
If any programming language has the ability to treat functions as values, to pass them as arguments and to return a function from another function then it is said that programming language has First Class Functions and the functions are called as First Class Citizens in that programming language.
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, the sub
keyword for code, the open
function for IO objects, the format
builtin for formats, the reference operator for references, and the qr{}
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:
my $anonymous_package = package { ... }; # XXX
Shell commands have their own builtins, but are no data objects, so this won't work:
# don't execute `yes`, but store a handle to it in reference
my $shell_command = \qx{yes};
Instead, this statement should not terminate (and probably blow your memory).
Lists in Perl are language constructs, but no data types:
my $listref = \($x, $y, $z); # assigns reference to $z instead
The builtin types in Perl can have coercion rules:
1/8
or (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
$string =~ /ab?c/
the regex could be replaced with $regex
if $regex
is like above:
my $regex = qr/ab?c/;
$string =~ $regex ### no dereferencing syntax!
# $string =~ /$regex/ will work too, but may invoke string overloading first (?)
For example, coderefs require more biolerplate code:
sub foo {...}
foo();
versus
my $foo = sub {...};
$foo->(); # two possibilities
&$foo();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With