What are Perl built-in operators/functions?

后端 未结 4 1141
北恋
北恋 2020-12-31 06:56

I\'m reading Beginning Perl by Simon Cozens and in Chapter 8 - Subroutines he states that \"subroutines\" are user functions, while print,

相关标签:
4条回答
  • 2020-12-31 07:07

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

    0 讨论(0)
  • 2020-12-31 07:16

    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.

    0 讨论(0)
  • 2020-12-31 07:21

    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.

    0 讨论(0)
  • 2020-12-31 07: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();
    
    0 讨论(0)
提交回复
热议问题