Difference between revisions of "Perl built-in functions"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
(→‎O: -R)
m (→‎Arrays: fixed typo)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{sidebar|__TOC__}}[[Computing]]: [[Programming]]: [[Perl]]: [[Perl built-in functions|built-in functions]]{{seed}}
+
{{boxright|__TOC__}}[[Computing]]: [[Programming]]: [[Perl]]: [[Perl built-in functions|built-in functions]]{{seed}}
 +
==By Type==
 +
===Arrays===
 +
* '''{{perlfunc|join}}'''(<u>demarc</u>,<u>array</u>) &ndash; joins <u>array</u> elements into one string, using <u>demarc</u> as a seperator string
 +
* '''{{perlfunc|split}}'''(<u>pattern</u>,<u>string</u>,<u>limit</u>) &ndash; splits <u>string</u> into an array (return value) by dividing it wherever <u>pattern</u> is matched
 +
* '''{{perlfunc|splice}}'''(<u>target</u>,<u>offset</u>,<u>length</u>,<u>source</u>) &ndash; combines parts of two arrays (replaces parts of <u>target</u> with <u>source</u>)
 +
* '''{{perlfunc|pop}}'''(<u>array</u>) &ndash; remove/return the last element of <u>array</u>
 +
* '''{{perlfunc|push}}'''(<u>target</u>,<u>source</u>) &ndash; adds elements from <u>source</u> to the end of the array <u>target</u> (opposite of {{perlfunc|pop}}())
 +
* '''{{perlfunc|reverse}}'''(<u>array</u>) &ndash; returns an array containing the elements of the input <u>array</u> in reverse order
 +
* '''{{perlfunc|sort}}'''(<u>array</u>) &ndash; returns an array containing the elements of the input <u>array</u> in sorted order
 +
* '''{{perlfunc|shift}}'''(<u>array</u>) &ndash; remove/return the first element of <u>array</u> (opposite of {{perlfunc|unshift}}())
 +
* '''{{perlfunc|unshift}}'''(<u>target</u>,<u>source</u>) &ndash; add elements from the array <u>source</u> to the beginning of the array <u>target</u> (opposite of {{perlfunc|shift}}())
 +
 
 +
===Dirs===
 +
* '''opendir'''(<u>dirhandle</u>, <u>dirspec</u>)
 +
* '''rmdir'''
 +
====Dir Handles====
 +
* '''closedir'''
 +
* '''readdir'''
 +
* '''rewinddir'''
 +
* '''seekdir'''
 +
===Files===
 +
* '''chdir'''
 +
* '''chmod'''
 +
* '''chown'''
 +
* '''chroot'''
 +
* '''link'''
 +
* '''mkdir'''
 +
* '''open'''(<u>filehandle</u>, <u>filespec</u>)
 +
* '''readlink'''
 +
* '''rename'''                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
 +
====File Handles====
 +
* '''binmode'''
 +
* '''close'''
 +
* '''eof'''
 +
* '''flock'''
 +
* '''getc'''
 +
* '''lstat'''
 +
* '''read'''
 +
* '''seek'''
 +
* '''truncate'''
 +
* '''write'''
 +
===Numeric===
 +
* '''abs'''(<u>number</u>)
 +
* '''exp'''
 +
* '''log'''
 +
* '''rand'''
 +
** '''srand'''
 +
* '''sqrt'''
 +
====Base conversion====
 +
* '''hex'''
 +
* '''oct'''
 +
====Trigonometry====
 +
* '''atan2'''(<u>Y</u>, <u>X</u>) - returns the [[arctangent]] of <u>Y</u>/<u>X</u>
 +
* '''cos'''
 +
* '''sin'''
 +
 
 +
===RegEx===
 +
* '''m/'''<u>pattern</u>'''/'''
 +
* '''quotemeta'''
 +
* '''s/'''<u>pattern</u>'''/'''<u>replacement</u>'''/'''''egimosx''
 +
* '''tr/'''<u>search_list</u>'''/'''<u>replace_list</u>'''/'''''cds''
 +
* '''y/'''<u>search_list</u>'''/'''<u>replace_list</u>'''/'''''cds''
 +
===String===
 +
* '''chomp'''
 +
* '''chop'''
 +
* '''crypt'''
 +
* '''index'''
 +
* '''lc'''
 +
* '''lcfirst'''
 +
* '''length'''
 +
* '''quotemeta'''
 +
* '''rindex'''
 +
* '''scalar'''
 +
* '''{{perlfunc|substr}}'''(<u>source</u>,<u>offset</u>,<u>len</u>) &ndash; return <u>len</u> bytes from <u>source</u> after skipping <u>offset</u> characters
 +
* '''uc'''
 +
* '''ucfirst'''
 +
* '''unpack'''
 +
====conversion====
 +
* '''int'''
 +
====single-character====
 +
* '''chr'''(<u>number</u>) - returns the character whose [[ASCII]] code is <u>number</u>
 +
* '''ord'''(<u>string</u>) - returns the [[ASCII]] code of the first character in <u>string</u>
 +
 
 +
===Time===
 +
* '''gmtime'''
 +
* '''localtime'''
 +
* '''time'''
 +
* '''times'''
 
==Alphabetical==
 
==Alphabetical==
 
===A===
 
===A===
Line 103: Line 191:
 
* '''link'''(''oldfile'',''newfile'') - creates ''newfile'' as a link to ''oldfile''
 
* '''link'''(''oldfile'',''newfile'') - creates ''newfile'' as a link to ''oldfile''
 
* '''listen'''(''socket'',''queuesize'') - waits for incoming events on a socket
 
* '''listen'''(''socket'',''queuesize'') - waits for incoming events on a socket
* '''local'''(''variable list'') - makes all variables in ''variable list'' local to the current subroutine
+
* '''{{perlfunc|local}}'''(''variable list'') - makes all variables in ''variable list'' local to the current subroutine
 
* '''localtime'''(''number'') - converts the binary time stored in ''number'' to a 9-element list and localizes it to the machine's timezone
 
* '''localtime'''(''number'') - converts the binary time stored in ''number'' to a 9-element list and localizes it to the machine's timezone
 
* '''log'''(''number'') - returns the [[natural logarithm]] (i.e. ''log<sub>e</sub>'') of ''number''
 
* '''log'''(''number'') - returns the [[natural logarithm]] (i.e. ''log<sub>e</sub>'') of ''number''
 
* '''lstat'''(''fileHandle'') - returns file statistics for the file opened with file handle ''fileHandle'' (does not de-reference symbolic links)
 
* '''lstat'''(''fileHandle'') - returns file statistics for the file opened with file handle ''fileHandle'' (does not de-reference symbolic links)
 +
 
===M===
 
===M===
 
* '''m'''''/pattern/''<u>gimosx</u> - performs pattern-matching; other characters may be used as delimiters instead of "/". See [[Perl regex]]
 
* '''m'''''/pattern/''<u>gimosx</u> - performs pattern-matching; other characters may be used as delimiters instead of "/". See [[Perl regex]]
Line 115: Line 204:
 
* '''msgrcv'''
 
* '''msgrcv'''
 
* '''msgsnd'''
 
* '''msgsnd'''
* '''my'''(''variable list'') - makes all variables in ''variable list'' local to the current {block}
+
* '''{{perlfunc|my}}'''(''variable list'') - makes all variables in ''variable list'' local to the current {block}
 +
 
 
===N===
 
===N===
 
* '''next'''
 
* '''next'''

Latest revision as of 13:15, 20 April 2006

Computing: Programming: Perl: built-in functions

This page is a seed article. You can help HTYP water it: make a request to expand a given page and/or donate to help give us more writing-hours!

By Type

Arrays

  • join(demarc,array) – joins array elements into one string, using demarc as a seperator string
  • split(pattern,string,limit) – splits string into an array (return value) by dividing it wherever pattern is matched
  • splice(target,offset,length,source) – combines parts of two arrays (replaces parts of target with source)
  • pop(array) – remove/return the last element of array
  • push(target,source) – adds elements from source to the end of the array target (opposite of pop())
  • reverse(array) – returns an array containing the elements of the input array in reverse order
  • sort(array) – returns an array containing the elements of the input array in sorted order
  • shift(array) – remove/return the first element of array (opposite of unshift())
  • unshift(target,source) – add elements from the array source to the beginning of the array target (opposite of shift())

Dirs

  • opendir(dirhandle, dirspec)
  • rmdir

Dir Handles

  • closedir
  • readdir
  • rewinddir
  • seekdir

Files

  • chdir
  • chmod
  • chown
  • chroot
  • link
  • mkdir
  • open(filehandle, filespec)
  • readlink
  • rename

File Handles

  • binmode
  • close
  • eof
  • flock
  • getc
  • lstat
  • read
  • seek
  • truncate
  • write

Numeric

  • abs(number)
  • exp
  • log
  • rand
    • srand
  • sqrt

Base conversion

  • hex
  • oct

Trigonometry

  • atan2(Y, X) - returns the arctangent of Y/X
  • cos
  • sin

RegEx

  • m/pattern/
  • quotemeta
  • s/pattern/replacement/egimosx
  • tr/search_list/replace_list/cds
  • y/search_list/replace_list/cds

String

  • chomp
  • chop
  • crypt
  • index
  • lc
  • lcfirst
  • length
  • quotemeta
  • rindex
  • scalar
  • substr(source,offset,len) – return len bytes from source after skipping offset characters
  • uc
  • ucfirst
  • unpack

conversion

  • int

single-character

  • chr(number) - returns the character whose ASCII code is number
  • ord(string) - returns the ASCII code of the first character in string

Time

  • gmtime
  • localtime
  • time
  • times

Alphabetical

A

  • abs(arg) - returns absolute value of arg
  • accept()
  • alarm($seconds) - sends an alarm signal after $seconds seconds
  • atan2($y,$x) - returns the arctangent of $y/$x

B

  • bind()
  • binmode(filehandle) - sets filehandle to be treated as binary
  • bless()

C

  • caller()
  • chdir()
  • chmod()
  • chomp()
  • chop()
  • chown()
  • chr()
  • chroot()
  • close()
  • closedir()
  • connect()
  • cos()
  • crypt()

D

  • dbmclose()
  • dbmopen()
  • defined()
  • delete()
  • die()
  • do subroutine
  • dump()

E

  • each()
  • endgrent
  • endhostent
  • endnetent
  • endprotoent
  • endpwent
  • endservent
  • eof()
  • eval()
  • exec()
  • exists($assoc($key)) - returns TRUE if $key exists within the hash $assoc. Doesn't work with dbm hashes.
  • exit()
  • exp()

F

  • fcntl()
  • fileno()
  • flock()
  • fork
  • format
  • formline()

G

  • getc()
  • getgrent
  • getgrgid()
  • getgrnam()
  • gethostbyaddr()
  • gethostbyname()
  • gethostent
  • getlogin
  • getnetbyaddr()
  • getnetbyname()
  • getnetent
  • getpeername()
  • getpgrp()
  • getppid
  • getpriority()
  • getprotobyname()
  • getprotobynumber()
  • getprotoent
  • getpwent
  • getpwnam()
  • getpwuid()
  • getserverbyname()
  • getserverbyport()
  • getservent
  • getsockname()
  • getsockopt()
  • glob()
  • gmtime()
  • goto()
  • grep()

H

  • hex()

I

  • index()
  • int()
  • ioctl()

J

  • join()

K

  • keys()
  • kill()

L

  • last()
  • lc(string) - returns string in lowercase
  • lcfirst(string) - returns string with first character lowercased
  • length(string) - returns length (number of characters in) string
  • link(oldfile,newfile) - creates newfile as a link to oldfile
  • listen(socket,queuesize) - waits for incoming events on a socket
  • local(variable list) - makes all variables in variable list local to the current subroutine
  • localtime(number) - converts the binary time stored in number to a 9-element list and localizes it to the machine's timezone
  • log(number) - returns the natural logarithm (i.e. loge) of number
  • lstat(fileHandle) - returns file statistics for the file opened with file handle fileHandle (does not de-reference symbolic links)

M

  • m/pattern/gimosx - performs pattern-matching; other characters may be used as delimiters instead of "/". See Perl regex
  • map(expr,list) - returns an array containing the output of expr(list item) for each item in list
  • mkdir(name,mode) - creates a directory named name (can be relative or absolute filespec), with the file attributes in mode. See mkdir for related information.
  • msgctl
  • msgget
  • msgrcv
  • msgsnd
  • my(variable list) - makes all variables in variable list local to the current {block}

N

  • next
  • no

O

  • oct(string) - converts string from octal to decimal
  • open(fileHandle, name) - opens a file named name and associates it with fileHandle
  • opendir(dirHandle, name) - opens a directory named name and associates it with dirHandle
  • ord(string) - returns the ASCII code of the first character in string, as an integer

P

  • pack(template, list)
  • pipe(readhandle, writehandle)
  • pop(array)
  • pos(scalar)
  • print(filehandle, list)
  • printf(filehandle, list)
  • push(array, list)

Q

  • q/string/
  • qq/string/
  • quotemeta(string) - prepares a string for use in a regex by backslashing any characters with special meanings
  • qw/string/
  • qx/string/

R

  • rand(number) - returns a random number between 0 and number
  • read(filehandle, scalar, length)
  • readdir
  • readlink
  • recv
  • redo
  • ref
  • rename(oldname, newname) - renames file oldname to newname
  • require(filename) - executes the Perl code inside file filename (if no path specified, searches Perl path)
  • reset
  • return
  • reverse(list) - reverses the sorting of array list
  • rewinddir
  • rindex(string, substring) - returns the position of the last occurrence of substring in string
  • rmdir

S