{"id":4812,"date":"2023-04-24T11:26:27","date_gmt":"2023-04-24T03:26:27","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4812"},"modified":"2023-04-29T22:49:20","modified_gmt":"2023-04-29T14:49:20","slug":"lua%e7%9a%84%e5%87%a0%e4%b8%aa%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4812","title":{"rendered":"Lua\u7684\u51e0\u4e2a\u8bb0\u5f55"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u4e00\u3001\u8001\u5f0fmodule\u7684\u5199\u6cd5\nmodule\uff0c\u4e5f\u79f0\u4e3a\u6a21\u5757\n\nmodule(\"actor\")\n\n\u5728\u6b64module\u73af\u5883\u4e2d\uff0c\u542b\u67093\u4e2a\u5185\u7f6e\u53d8\u91cf\n1\u3001_M \u6307\u5b9a\u6b64\u6a21\u5757\u81ea\u5df1(TABLE)\n2\u3001_NAME \u5373\u4e3a\u6b64\u6a21\u5757\u7684\u540d\u79f0, ACTOR\n3\u3001_PACKAGE \u4e3a\u5305\u7684\u5168\u540d\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\n\n\u4e8c\u3001\u8001\u5f0fsetfenv\u7684\u529f\u80fd\n\u5728 5.1 \u4e2d\u542b\u6709 setfenv\n\u65b0\u7248\u672c\u4e2d\u4f7f\u7528_ENV\u66ff\u4ee3\u6b64\u529f\u80fd<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/moonbingbing.gitbooks.io\/openresty-best-practices\/content\/lua\/not_use_module.html\">https:\/\/moonbingbing.gitbooks.io\/openresty-best-practices\/content\/lua\/not_use_module.html<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"\u62b5\u5236\u4f7f\u7528-module-\u5b9a\u4e49\u6a21\u5757\">\u62b5\u5236\u4f7f\u7528 module() \u5b9a\u4e49\u6a21\u5757<\/h1>\n\n\n\n<p>\u65e7\u5f0f\u7684\u6a21\u5757\u5b9a\u4e49\u65b9\u5f0f\u662f\u901a\u8fc7&nbsp;<code>module(\"filename\"[,package.seeall])*<\/code>&nbsp;\u6765\u663e\u5f0f\u58f0\u660e\u4e00\u4e2a\u5305\uff0c\u73b0\u5728\u5b98\u65b9\u4e0d\u63a8\u8350\u518d\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7531&nbsp;<code>filename<\/code>&nbsp;\u6a21\u5757\u51fd\u6570\u7ec4\u6210\u7684&nbsp;<code>table<\/code>\uff0c\u5e76\u4e14\u8fd8\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u8be5&nbsp;<code>table<\/code>&nbsp;\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n\n\n\n<p><code>module(\"filename\", package.seeall)<\/code>&nbsp;\u8fd9\u79cd\u5199\u6cd5\u662f\u4e0d\u63d0\u5021\u7684\uff0c\u5b98\u65b9\u7ed9\u51fa\u4e86\u4e24\u70b9\u539f\u56e0\uff1a<\/p>\n\n\n\n<ol><li><code>package.seeall<\/code>&nbsp;\u8fd9\u79cd\u65b9\u5f0f\u7834\u574f\u4e86\u6a21\u5757\u7684\u9ad8\u5185\u805a\uff0c\u539f\u672c\u5f15\u5165 &#8220;filename&#8221; \u6a21\u5757\u53ea\u60f3\u8c03\u7528\u5b83\u7684&nbsp;<em>foobar()<\/em>&nbsp;\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u5374\u53ef\u4ee5\u8bfb\u5199\u5168\u5c40\u5c5e\u6027\uff0c\u4f8b\u5982&nbsp;<code>\"filename.os\"<\/code>\u3002<\/li><li><code>module<\/code>&nbsp;\u51fd\u6570\u538b\u6808\u64cd\u4f5c\u5f15\u53d1\u7684\u526f\u4f5c\u7528\uff0c\u6c61\u67d3\u4e86\u5168\u5c40\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982&nbsp;<code>module(\"filename\")<\/code>&nbsp;\u4f1a\u521b\u5efa\u4e00\u4e2a&nbsp;<code>filename<\/code>&nbsp;\u7684&nbsp;<code>table<\/code>\uff0c\u5e76\u5c06\u8fd9\u4e2a&nbsp;<code>table<\/code>&nbsp;\u6ce8\u5165\u5168\u5c40\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u8fd9\u6837\u4f7f\u5f97\u6ca1\u6709\u5f15\u7528\u5b83\u7684\u6587\u4ef6\u4e5f\u80fd\u8c03\u7528&nbsp;<code>filename<\/code>&nbsp;\u6a21\u5757\u7684\u65b9\u6cd5\u3002<\/li><\/ol>\n\n\n\n<p>\u6bd4\u8f83\u63a8\u8350\u7684\u6a21\u5757\u5b9a\u4e49\u65b9\u6cd5\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- square.lua \u957f\u65b9\u5f62\u6a21\u5757\nlocal _M = {}           -- \u5c40\u90e8\u7684\u53d8\u91cf\n_M._VERSION = '1.0'     -- \u6a21\u5757\u7248\u672c\n\nlocal mt = { __index = _M }\n\nfunction _M.new(self, width, height)\n    return setmetatable({ width=width, height=height }, mt)\nend\n\nfunction _M.get_square(self)\n    return self.width * self.height\nend\n\nfunction _M.get_circumference(self)\n    return (self.width + self.height) * 2\nend\n\nreturn _M<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5f15\u7528\u793a\u4f8b\u4ee3\u7801\uff1a<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>local square = require \"square\"\n\nlocal s1 = square:new(1, 2)\nprint(s1:get_square())          --output: 2\nprint(s1:get_circumference())   --output: 6<\/code><\/pre>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u8ddf Lua \u7684 module \u6a21\u5757\u76f8\u5173\u9700\u8981\u6ce8\u610f\u7684\u70b9\u662f\uff0c\u5f53 lua_code_cache on \u5f00\u542f\u65f6\uff0crequire \u52a0\u8f7d\u7684\u6a21\u5757\u662f\u4f1a\u88ab\u7f13\u5b58\u4e0b\u6765\u7684\uff0c\u8fd9\u6837\u6211\u4eec\u7684\u6a21\u5757\u5c31\u4f1a\u4ee5\u6700\u9ad8\u6548\u7684\u65b9\u5f0f\u8fd0\u884c\uff0c\u76f4\u5230\u88ab\u663e\u5f0f\u5730\u8c03\u7528\u5982\u4e0b\u8bed\u53e5\uff08\u8fd9\u91cc\u6709\u70b9\u50cf\u6a21\u5757\u5378\u8f7d\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package.loaded[\"square\"] = nil<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u7279\u6027\u4ee3\u7801\u6765\u505a\u4e00\u4e9b\u9ad8\u9636\u73a9\u6cd5\uff0c\u6bd4\u5982\u4ee3\u7801\u70ed\u66f4\u65b0\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/moonbingbing.gitbooks.io\/openresty-best-practic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4812"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4812"}],"version-history":[{"count":5,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4812\/revisions"}],"predecessor-version":[{"id":4830,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4812\/revisions\/4830"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}