Perl 脚本可以修改自身吗? [英] Can a Perl script modify itself?

查看:35
本文介绍了Perl 脚本可以修改自身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的脚本在内部跟踪他们的最后修订日期作为评论.这可能吗?在我看来,它需要获取日期,然后打开其脚本文件进行追加,写入数据并保存文件.

I would like to have my scripts keep track of thier last date of revision internally as a comment. Is this possible? It seems to me that it would need to grab the date and then open its script file for an append, write the data and save the file.

感谢 Everone,非常棒的回答者.根据 GreenMatt 留下的代码片段,我把它放在一起......

Thanks Everone, great answsers one and all. Based on the code snippet left by GreenMatt I threw this together...

#!/usr/bin/perl -w 

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
$year += 1900;
$mon +=1;

open SELF, ">> letterhome.pl" or die "Unable to open self"; 
#print SELF "# ran/modified at " . join(' ', localtime(time)) . "\n"; 
print SELF "# ran/modified at $hour:$min:$sec on $mon/$mday/$year.\n"; 
close(SELF); 

# ran/modified at 31 48 23 24 7 110 2 235 1  
# unformated result of using localtime(time)  

#Results using formated time/date 
# ran/modified at 0:1:43 on 8/25/2010.
# ran/modified at 0:2:40 on 8/25/2010.
# ran/modified at 0:4:35 on 8/25/2010.

推荐答案

这是可能的,但这并不是一个好主意.一方面,它不会更新日期,直到您运行它.

It is possible, but that doesn't make it a good idea. For one thing, it wouldn't update the date until you ran it.

如果您使用的是好的编辑器,它可能会在您保存文件时自动插入时间戳.例如,我将 Emacs 设置为使用 write-contents-hooks 在 HTML 文件中执行此操作.(使用 Perl 代码需要一些修改,但是 cjm-html-timestamp="nofollow noreferrer">cjm-misc.el 会给你一个起点.)

If you're using a good editor, it may have a way to insert a timestamp automatically when you save the file. For example, I set up Emacs to do that in HTML files using write-contents-hooks. (It would need some modification to work with Perl code, but cjm-html-timestamp in cjm-misc.el would give you a starting point.)

这篇关于Perl 脚本可以修改自身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆