Perl core functions as I see them
No Comments
Written by Anders on April 18, 2008 – 12:06 pm
The Perl core defines functions equivalent to the C functions. These functions include:
Functions for scalars or strings
chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack, q/STRING/, qq/ STRING/, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///
Regular expressions and pattern matching
m//, pos, quotemeta, s///, split, study, qr//
Numeric functions
abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand
Functions for real @ARRAYs
pop, push, shift, splice, unshift
Functions for list data
grep, join, map, qw/STRING/, reverse, sort, unpack
Functions for real %HASHes
delete, each, exists, keys, values
Input and output functions
binmode, close, closedir, dbmclose, dbmopen, die, eof, fileno, flock, format, getc, print, printf, read, readdir, rewinddir, seek, seekdir, select, syscall, sysread, sysseek, syswrite, tell, telldir, truncate, warn, write
Functions for fixed length data or records
pack, read, syscall, sysread, syswrite, unpack, vec
Functions for file handles, files or directories
-X, chdir, chmod, chown, chroot, fcntl, glob, ioctl, link, lstat, mkdir, open, opendir, readlink, rename, rmdir, stat, symlink, umask, unlink, utime
Functions for processes and process groups
alarm, exec, fork, getpgrp, getppid, getpriority, kill, pipe, qx/STRING/, setpgrp, setpriority, sleep, system, times, wait, waitpid
Low-level socket functions
accept, bind, connect, getpeername, getsockname, getsockopt, listen, recv, send, setsockopt, shutdown, socket, socketpair
Fetching user and group info
endgrent, endhostent, endnetent, endpwent, getgrent, getgrgid, getgrnam, getlogin, getpwent, getpwnam, getpwuid, setgrent, setpwent
Fetching network info
endprotoent, endservent, gethostbyaddr, gethostbyname, gethostent, getnetbyaddr, getnetbyname, getnetent, getprotobyname, getprotobynumber, getprotoent, getservbyname, getservbyport, getservent, sethostent, setnetent, setprotoent, setservent
Time-related functions
gmtime, localtime, time, times
That was a shortened list; a few segments were culled from the list in the perlfunc documentation.
Perl’s basic I/O uses ‘filehandles’ – these are really just FILE* references as used in C’s stdio library. As always, you start with STDIN, STDOUT, and STDERR; you can open additional file handles for input and/or output. There is a special convenience mechanism for opening file(s) listed on the command line. The simplest mechanism for output is to use the print function, as has been illustrated in earlier code fragments.
By default, print sends its output to STDOUT. Input is typically handled line-by-line. There is a readline function that is roughly the input line handling function equivalent to the output line handling print, but most Perl code uses the ‘diamond’ < > operator. Usually, the diamond operator includes a filehandle, e.g. <STDIN> (or <INPUTFILE> if you have assigned a value to INPUTFILE in a call to the open() file-opening function). An empty diamond operator, <>, is associated with an input stream obtained by concatenating together the contents of all the files listed on the command line. The diamond operator returns the next line from the referenced file:
$line = <STDIN>;
The entire contents of the input line, including the terminating \n character, are assigned to the scalar variable on the left side of the assignment statement. The line input in response to the prompt is read in the $num = <STDIN> line, resulting in a string value in the scalar $num. On the next line, this is used in a construct that requires a numeric value (the repeat factor in the string repetition expression). The input string is automatically coerced to a numeric value (if the input data were not numeric, the resulting value will be zero).
#!/share/bin/perl
print “What is your name? “;
$name = <STDIN>;
chomp($name);
print “Hello $name, welcome to Perl\n”;
Input of the name Fred would produce the string ‘Fred\n’. This would disrupt output formatting, so here chomp() is used to remove the trailing \n.








