What is the difference between first-class entities and second-class entities in perl?
perl
Solution
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:
- Scalars
- Arrays
- Hashes
- Coderefs (e.g. anonymous subroutines)
- IO
- Typeglobs (The symbol table is a hash of globs)
- Formats
Those can reside in the symbol table. The scalar slot can be occupied by various other types in addition:
- Signed integers
- Unsigned integers
- Floating point numbers
- Strings
- References
- Regexes
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:
- Numbers and Strings coerce back and forth.
- A single scalar in list context is a list of arity 1.
- An array in scalar context evaluates to the length of the array
- An (even valued) array can be assigned to a hash
- A Hash can be assigned to an array so that assigning this array to another hash would recreate the same hash
- A Hash in scalar context evaluates to (a) a false value if it is empty or (b) to a string indicating the number of filled and allocated buckets e.g. `1/8` or (c) to the number of keys in numerical context.
- Regexes in string context evaluate to a pattern string that behaves like the one they were specified with: `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();
Problem
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).