Module:IP/doc: Difference between revisions

Content deleted Content added
m change source to syntaxhighlight
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1:
{{Module rating|protected}}
{{cascade-protected template|page=module}}
{{used in system}}
Module:IP is a library for working with IP addresses and subnets. It can handle both [[IPv4]] and [[IPv6]]. The library exports four classes, [[#IPAddress|IPAddress]], [[#Subnet|Subnet]], [[#IPv4Collection|IPv4Collection]], and [[#IPv6Collection|IPv6Collection]].
Line 26 ⟶ 27:
local ipv4Address = IPAddress.new('1.2.3.4')
local ipv6Address = IPAddress.new('2001:db8::ff00:12:3456')
</syntaxhighlight>
 
If a non-IP string or an invalid IP address is passed to the function, that returns an error. If you want to check whether a given string is an IP address and continue the procedure in the caller module, use [[:mw:Extension:Scribunto/Lua_reference_manual#pcall|pcall]].
 
<syntaxhighlight lang="lua">
local isIp, ip = pcall(IPAddress.new, '1.2.3.4') -- isIp: true, ip: IPAddress object
local isIp, ip = pcall(IPAddress.new, 'Example') -- isIp: false, ip: nil
local isIp, ip = pcall(IPAddress.new, '1.2.3.256') -- isIp: false, ip: nil
</syntaxhighlight>
 
Line 194 ⟶ 203:
 
<var>cidrString</var> must be a valid IPv4 or IPv6 [[CIDR]] string.
 
<syntaxhighlight lang="lua">
local cidr = Subnet.new('255.255.255.0/24') -- Subnet object
local cidr = Subnet.new('255.255.255.1/24') -- error
</syntaxhighlight>
 
Subnet objects can be compared for equality:
Line 532 ⟶ 546:
<!-- Categories below this line, please; interwikis at Wikidata -->
 
}}</includeonly><noinclude>
[[Category:Module documentation pages]]
</noinclude>