Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between first-class entities and second-class entities in perl?

Tags:

perl

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).

like image 333
Sean Avatar asked Nov 19 '12 07:11

Sean


People also ask

What is a first class entity?

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.

What is a second class object?

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.

What does first class mean in programming?

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.

What does it mean for a function to be a first class citizen?

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.


1 Answers

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();
like image 160
amon Avatar answered Oct 20 '22 23:10

amon