Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
3  
I'd look at perldoc perlfunc and perldoc -f <function name>. – Jack Maney Aug 23 '11 at 21:15
1  
This is my favorite list of perl operators: ozonehouse.com/mark/periodic – Seth Aug 23 '11 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." – jwodder Aug 24 '11 at 3:13
    
@jwodder: Fair enough, but it's still my favorite! – Seth Aug 24 '11 at 16:55
up vote 23 down vote accepted

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();
share|improve this answer
1  
Is the code you've posted the code that Perl's VM understands, generated by the perl interpreter? – Raphael Aug 24 '11 at 4:24
    
@Raphael, Yes, They are representations of the opcode trees that result from compiling the programs. – ikegami Aug 24 '11 at 6:05

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.

share|improve this answer
    
Never saw a language with that much keywords! – Raphael Aug 24 '11 at 4:26
    
@Raphael: Then don't look at COBOL. ;) – musiKk Aug 24 '11 at 11:45
3  
@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. – tchrist Aug 24 '11 at 12:49

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.

share|improve this answer
1  
fyi, \&CORE::builtin is being added in 5.16. It'll autovivify a sub that calls the builtin. – ikegami Aug 23 '11 at 22:08

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

share|improve this answer
5  
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) – ikegami Aug 24 '11 at 6:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.