Perl built-in functions: Difference between revisions

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
m A: spelling fix; arctangent probably could use an article...
m Arrays: fixed typo
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{seed}}[[Computing]]: [[Programming]]: [[Perl]]: [[Perl built-in functions|built-in functions]]
{{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 97: Line 185:
*'''kill'''()
*'''kill'''()
===L===
===L===
*'''last'''()
* '''last'''()
*'''lc'''(''string'') - returns ''string'' in lowercase
* '''lc'''(''string'') - returns ''string'' in lowercase
*'''lcfirst'''(''string'') - returns ''string'' with first character lowercased
* '''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
* '''{{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
* '''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)
 
===M===
* '''m'''''/pattern/''<u>gimosx</u> - 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'''
* '''{{perlfunc|my}}'''(''variable list'') - makes all variables in ''variable list'' local to the current {block}
 
===N===
* '''next'''
* '''no'''
===O===
* '''oct'''(<u>string</u>) - converts <u>string</u> from octal to decimal
* '''open'''(<u>fileHandle</u>, <u>name</u>) - opens a file named <u>name</u> and associates it with <u>fileHandle</u>
* '''opendir'''(<u>dirHandle</u>, <u>name</u>) - opens a directory named <u>name</u> and associates it with <u>dirHandle</u>
* '''ord'''(<u>string</u>) - returns the ASCII code of the first character in <u>string</u>, as an integer
===P===
* '''pack'''(<u>template</u>, <u>list</u>)
* '''pipe'''(<u>readhandle</u>, <u>writehandle</u>)
* '''pop'''(<u>array</u>)
* '''pos'''(<u>scalar</u>)
* '''print'''(<u>filehandle</u>, <u>list</u>)
* '''printf'''(<u>filehandle</u>, <u>list</u>)
* '''push'''(<u>array</u>, <u>list</u>)
===Q===
* '''q/'''<u>string</u>'''/'''
* '''qq/'''<u>string</u>'''/'''
* '''quotemeta'''(<u>string</u>) - prepares a ''string'' for use in a regex by backslashing any characters with special meanings
* '''qw/'''<u>string</u>'''/'''
* '''qx/'''<u>string</u>'''/'''
===R===
* '''rand'''(<u>number</u>) - returns a random number between 0 and <u>number</u>
* '''read'''(<u>filehandle</u>, <u>scalar</u>, <u>length</u>)
* '''readdir'''
* '''readlink'''
* '''recv'''
* '''redo'''
* '''ref'''
* '''rename'''(<u>oldname</u>, <u>newname</u>) - renames file <u>oldname</u> to <u>newname</u>
* '''require'''(<u>filename</u>) - executes the Perl code inside file <u>filename</u> (if no path specified, searches Perl path)
* '''reset'''
* '''return'''
* '''reverse'''(<u>list</u>) - reverses the sorting of array <u>list</u>
* '''rewinddir'''
* '''rindex'''(<u>string</u>, <u>substring</u>) - returns the position of the ''last'' occurrence of <u>substring</u> in <u>string</u>
* '''rmdir'''
===S===

Latest revision as of 13:15, 20 April 2006

Computing: Programming: Perl: built-in functions<hide>

 page status::seed

</hide>

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