14

I'm reading Beginning Perl by Simon Cozens and in Chapter 8 - Subroutines he states that "subroutines" are user functions, while print, open, split, etc. are built-in operators or functions.

What are they? Are they really built-in, language-intrinsic features (like C's sizeof operator) or are they, actually, subroutines/functions of the main module?

If they're subroutines, are while, for, unless, etc. also subroutines? What about the operators like +, -, eq, etc.?

4
  • 3
    I'd look at perldoc perlfunc and perldoc -f <function name>. Commented Aug 23, 2011 at 21:15
  • 1
    This is my favorite list of perl operators: ozonehouse.com/mark/periodic Commented Aug 23, 2011 at 23:27
  • 1
    @Seth That's for Perl 6, which (for all intents & purposes) is a completely separate language from Perl 5, which is what just about everyone means when they say "Perl." Commented Aug 24, 2011 at 3:13
  • @jwodder: Fair enough, but it's still my favorite! Commented Aug 24, 2011 at 16:55

4 Answers 4

27

print, open, split are not subroutines. They do not result in sub calls. They are not even present in the symbol table (in main:: or otherwise, although you can refer to them as CORE::split, etc), and one cannot get a reference to their code (although work is being done to create proxy subs for them in CORE:: for when you want to treat them as subroutines). They are operators just like +.

$ perl -MO=Concise,-exec -e'sub f {} f()'
1  <0> enter 
2  <;> nextstate(main 2 -e:1) v:{
3  <0> pushmark s
4  <#> gv[*f] s
5  <1> entersub[t3] vKS/TARG,1      <--- sub call
6  <@> leave[1 ref] vKP/REFC
-e syntax OK

$ perl -MO=Concise,-exec -e'split /;/'
1  <0> enter 
2  <;> nextstate(main 1 -e:1) v:{
3  </> pushre(/";"/) s/64
4  <#> gvsv[*_] s
5  <$> const[IV 0] s
6  <@> split[t2] vK                 <--- not a sub call
7  <@> leave[1 ref] vKP/REFC
-e syntax OK

$ perl -MO=Concise,-exec -e'$x + $y'
1  <0> enter 
2  <;> nextstate(main 1 -e:1) v:{
3  <#> gvsv[*x] s
4  <#> gvsv[*y] s
5  <2> add[t3] vK/2                 <--- Just like this
6  <@> leave[1 ref] vKP/REFC
-e syntax OK

They are known by a variety of names:

  • builtin functions
  • functions
  • builtins
  • named operators

And most are considered to be one of the following:

  • list operator
  • named unary operator

Subroutines are often called functions (as they are in C and C++), so "function" is an ambiguous word. This ambiguity appears to be the basis of your question.


As for while, for, unless, etc, they are keywords used by flow control statements

while (f()) { g() }

and statement modifiers

g() while f();
Sign up to request clarification or add additional context in comments.

2 Comments

Is the code you've posted the code that Perl's VM understands, generated by the perl interpreter?
@Raphael, Yes, They are representations of the opcode trees that result from compiling the programs.
11

The Perl keywords are those defined in the regen/keywords.pl file within the Perl source distribution. These are:

__FILE__, __LINE__, __PACKAGE__, __DATA__, __END__, AUTOLOAD, BEGIN, UNITCHECK, CORE, DESTROY, END, INIT, CHECK, abs, accept, alarm, and, atan2, bind, binmode, bless, break, caller, chdir, chmod, chomp, chop, chown, chr, chroot, close, closedir, cmp, connect, continue, cos, crypt, dbmclose, dbmopen, default, defined, delete, die, do, dump, each, else, elsif, endgrent, endhostent, endnetent, endprotoent, endpwent, endservent, eof, eq, eval, exec, exists, exit, exp, fcntl, fileno, flock, for, foreach, fork, format, formline, ge, getc, getgrent, getgrgid, getgrnam, gethostbyaddr, gethostbyname, gethostent, getlogin, getnetbyaddr, getnetbyname, getnetent, getpeername, getpgrp, getppid, getpriority, getprotobyname, getprotobynumber, getprotoent, getpwent, getpwnam, getpwuid, getservbyname, getservbyport, getservent, getsockname, getsockopt, given, glob, gmtime, goto, grep, gt, hex, if, index, int, ioctl, join, keys, kill, last, lc, lcfirst, le, length, link, listen, local, localtime, lock, log, lstat, lt, m, map, mkdir, msgctl, msgget, msgrcv, msgsnd, my, ne, next, no, not, oct, open, opendir, or, ord, our, pack, package, pipe, pop, pos, print, printf, prototype, push, q, qq, qr, quotemeta, qw, qx, rand, read, readdir, readline, readlink, readpipe, recv, redo, ref, rename, require, reset, return, reverse, rewinddir, rindex, rmdir, s, say, scalar, seek, seekdir, select, semctl, semget, semop, send, setgrent, sethostent, setnetent, setpgrp, setpriority, setprotoent, setpwent, setservent, setsockopt, shift, shmctl, shmget, shmread, shmwrite, shutdown, sin, sleep, socket, socketpair, sort, splice, split, sprintf, sqrt, srand, stat, state, study, sub, substr, symlink, syscall, sysopen, sysread, sysseek, system, syswrite, tell, telldir, tie, tied, time, times, tr, truncate, uc, ucfirst, umask, undef, unless, unlink, unpack, unshift, untie, until, use, utime, values, vec, wait, waitpid, wantarray, warn, when, while, write, x, xor, y.

The perlsyn, perlop, and perlsub manpages are required reading, followed perhaps by the perlfunc manpage. To learn how to override builtin operators used with objects, see the overload manpage.

3 Comments

Never saw a language with that much keywords!
@Raphael: Then don't look at COBOL. ;)
@Raphael: You probably are thinking of keywords as something that cannot be overridden, like if and unless. Perl has very few of those absolutely inviolate keywords. Most of Perl’s keywords from regen/keywords.pl can be indeed be overridden through one (or more) of three ways: by importing a function of that name from another package (as in chdir or open) or else through either tying (as in keys or pop) or operator overloading (as in print, cmp, or qq). Which is which requires experience with the language to understand.
1

The built-in operators are not Perl subroutines. For example,

#!/usr/bin/perl

use strict;
use warnings;

sub Foo { print "In foo\n"; }

my $ref;

$ref = \&Foo;
$ref->();

$ref = \&print;
$ref->();

The first $ref->(); is an indirect call; it prints "In foo".

The second one produces a warning:

Undefined subroutine &main::print called at ./tmp.pl line 14

because print is not the name of a subroutine.

1 Comment

fyi, \&CORE::builtin is being added in 5.16. It'll autovivify a sub that calls the builtin.
0

Simply think of "built-in functions" as functions that you did not create. Easy right? :-)

1 Comment

Except you can't take a reference to them. Except you can't override their prototype and special parsing rules by prefixing the call with &. Except you can't goto them. (and I'm sure I'm missing some)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.