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.?
Perl provides all basic bitwise operators including and (&), or (|), exclusive or (^) , not (~) operators, shift right (>>) and shift left (<<) operators. The bitwise operators perform from right to left. In other words, bitwise operators perform from rightmost bit to the left most bit.
Binary AND Operator copies a bit to the result if it exists in both operands. Example − ($a & $b) will give 12 which is 0000 1100. 2. | Binary OR Operator copies a bit if it exists in eather operand.
A Perl subroutine or function is a group of statements that together performs a task. You can divide up your code into separate subroutines. How you divide up your code among different subroutines is up to you, but logically the division usually is so each function performs a specific task.
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:
And most are considered to be one of the following:
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();
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.
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