关于 Perl 编码使其像 PHP 一样工作的建议
我首先是一名 Perl 编码员,但像许多人一样,我也使用 PHP 为客户工作(尤其是 Web 应用程序)编写代码。
我发现我用两种语言复制了很多项目,但使用了不同的范例(例如,用于处理 cgi 输入和会话数据)或函数。
我想做的是开始以一种更像 PHP 的结构方式编写 Perl 代码,这样我就可以 a)我脑子里保留着一种范式 b) 可以更快地将脚本从一个脚本移植到另一个脚本
具体来说,我问人们是否可以建议您如何在 perl 中执行以下操作?
1) 重现$_SESSION
、$_GET
等的功能。 例如,通过封装会话库 CGI.pm
的 param()
方法?
2)类似PHP的模板库 我习惯于在 PHP 约定中混合代码和 HTML。例如,
<h1>HTML Code here</h1>
<?
print "Hello World\b";
?>
有人可以建议哪个 perl 模板引擎(以及可能的配置)将允许我进行类似的编码吗?
3)PHP函数库 有人知道 perl 库可以重现很多 php 内置函数吗?
I am first and foremost a perl coder, but like many, also code in PHP for client work, especially web apps.
I am finding that I am duplicating a lot of my projects in the two languages, but using different paradigms (e.g. for handling cgi input and session data) or functions.
What I would like to do is start to code my Perl in a way which is structured more like PHP, so that I
a) am keeping one paradigm in my head
b) can more quickly port over scripts from one to the other
Specifically, I am asking if people could advise how you might do the following in perl?
1) Reproduce the functionality of $_SESSION
, $_GET
etc.
e.g. by wrapping up the param()
method of CGI.pm
, a session library?
2) Templating library that is similar to PHP
I am used to mixing my code and HTML in the PHP convention. e.g.
<h1>HTML Code here</h1>
<?
print "Hello World\b";
?>
Can anybody advise on which perl templating engine (and possibly configuration) will allow me to code similarly?
3) PHP function library
Anybody know of a library for perl which reproduces a lot of the php built-in functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看 EmbPerl。
它是一个基于 Perl 的模板系统,基于我对 PHP 的非常有限的了解,它似乎提供了 PHP 所做的任何事情。
为了涵盖您的具体要点:
$_GET
:EmbPerl 提供%fdat
哈希,其中包含通过 POST 或 GET 传递的全套表单数据%fdat 不区分值是源自 GET 的查询字符串还是源自 POST 的表单字段。
如果您绝对必须只获取 GET 的 QUERY_STRING 中的值,这里有一个获取它的函数的简单示例:http://urlgreyhot.com/personal/resources/embperl_getting_values_query_string - 虽然我现在不知道为什么要将 GET 与 POST 数据分开。
$_SESSION
:我不是 100% 明白 PHP 中的作用,但如果我是对的,每个用户数据有%udat
和 < code>%mdat 用于处理会话内容的每个模块/页面数据。“ 中更详细地描述了两者的使用Session Handling” EmbPerl 文档区域,以及 EmbPerl 中所有其他众多会话支持
这是一个简短的
%udat
简介:...一旦您向
%udat
写入任何内容,Embperl 就会创建一个会话 ID 并通过 cookie 将其发送到浏览器。您写入 %udat 的数据由 Apache::Session 存储。下次同一用户请求 Embperl 页面时,浏览器会发回带有会话 ID 的 cookie,并且 Embperl 会使用您为该用户存储的相同值填充来自 Apache::Session 的 %udat 哈希值。您包含的模板代码在 EmbPerl 中如下所示:
Have a look at EmbPerl.
It's a Perl based templating system, which seems to provide anything that PHP does based on my admittedly very small knowledge of PHP.
To cover your specific points:
$_GET
: EmbPerl provides%fdat
hash which contains full set of form data passed via either POST or GET%fdat makes no distinction of whether the value originated in GET's query string or form field via POST).
If you absolutely MUST have only the values from GET's QUERY_STRING, here's a simple example of a function to get it: http://urlgreyhot.com/personal/resources/embperl_getting_values_query_string - although why would you want to separate GET from POST data is escaping me at the moment.
$_SESSION
: I'm not 100% I get what this does in PHP but if I'm right, there's%udat
for per-user data and%mdat
for per-module/page data for handling session stuff.Using both is described in more detail in "Session Handling" area of EmbPerl docs, along with all the other multitude of session support in EmbPerl
Here's a quick
%udat
blurb:... as soon as you write anything to
%udat
, Embperl creates a session id and sends it via a cookie to the browser. The data you have written to %udat is stored by Apache::Session. The next time the same user request an Embperl page, the browser sends the cookie with the session id back and Embperl fills the %udat hash from Apache::Session with the same values as you have stored for that user.The templating code you included would look like this in EmbPerl:
Or for a more idiomatic/correct solution,
P.S. I have no clue what "
\b
" does in PHP so I didn't clone that.Embperl supports all the standard templating stuff (
[- -]
for execution,[+ +]
for inclusion of results of arbitrary Perl code, template flow control commands ([$ if $]
/'[$ for $]` etc...) and much more. It's also fully compatible with mod_perl.2) 如果您确实希望脚本成为 PHP 中的模板,则可以使用
Markup::Perl
模块(它源自另一个实际上称为 PerlHP 的项目)。还有其他模块,例如 HTML::Mason,Perl 程序员将其视为模板引擎。3)在CPAN上我发现
PHP::Strings
和PHP::DateTime
,但我没有没有使用过它们,否则无法为它们提供担保。2) If you literally want your script to be the template as in PHP, there is the
Markup::Perl
module (which grew out of another project that was actually called PerlHP). There are other modules like HTML::Mason for what Perl programmers think of as templating engines.3) On CPAN I found
PHP::Strings
andPHP::DateTime
, but I haven't used them and otherwise can't vouch for them.您还应该查看 mod_perlite,它是一个 Apache 模块,试图模拟 Perl 的 mod_php 行为,尽管开发是在它似乎已经停滞了。更多信息来自 自述文件。
You should also check out mod_perlite, it's an Apache module trying to emulate the mod_php behaviour for Perl, although development on it seems to have been stalled. More info from the README.
我本来想告诉您要喜欢 Perl 和 PHP,因为它们独特的自我,但是没有。 1 给我的印象是一点闲暇的乐趣。我的建议是自己编码,然后将其发布到 CPAN。我读了你的问题并想:
%_ENV
可能只是perl的%ENV
的别名。%_REQUEST
和%_SESSION
可能并列 对象等。哎呀,%_SESSION
甚至可能由PHP::Session::Serializer::PHP
。阅读 CGI 规范,并查看 CGI.pm,当然,还有更简单的模块,例如 CGI::Lite。
I was going to tell you to love Perl and PHP for their unique selves, but no. 1 strikes me as a bit of idle fun. My advice is to code it yourself, and post it to CPAN. I read your question and thought:
%_ENV
is probably just an alias for perl's%ENV
.%_REQUEST
and%_SESSION
are probably tied objects, etc. Heck,%_SESSION
may even be backed byPHP::Session::Serializer::PHP
.Read the CGI spec, and check out the source of CGI.pm, of course, but also simpler modules like CGI::Lite.