我有很多遗留代码,我想要做的是添加一个require
或最小的代码更改,以使反引号做一些不同的事情,例如打印而不是运行代码
我尝试使用,use subs
但我无法接受反击或qx(我重新定义系统,这是一个不用担心的事情)
我也尝试制作一个包:
package thingmbob;
use Data::Dumper;
use overload '``' => sub { CORE::print "things!:\t", Dumper \@_};
#this works for some reason
$thingmbob::{'(``'}('ls');
#this does the standard backtick operation
`ls`
Run Code Online (Sandbox Code Playgroud)
不可思议的是,我没有OOP perl的经验,而且我的google-fu技能让我失望,有人能指出我正确的方向吗?
警告:我在一个预装了几个cpan模块的封闭系统中,可能是因为我没有安装任何花哨的模块,我绝对无法获得新的模块
我正在使用perl5.14
编辑:
为了完整起见,我想添加我的(大部分)最终产品
BEGIN {
*CORE::GLOBAL::readpipe = sub {
print Dumper(\@_);
@internal = readpipe(@_);
if(wantarray){
return @internal;
}else{
return join('',@internal);
}
};
}
Run Code Online (Sandbox Code Playgroud)
我想让它打印出即将运行的内容,然后运行它.这wantarray
很重要,因为没有它标量上下文不起作用
我正在尝试做这样的事情
$SIG{ALRM} = sub {
print $line_number_when_alarm_went_off;
};
alarm 10;
# rest of the script
Run Code Online (Sandbox Code Playgroud)
我正在使用ALRM
一个例子,我将最终使用不同的信号从外部杀死以触发它.这种操作有一种巧妙的方法吗?
我有一些慢脚本,有时我想给他们一个信号,知道那时代码在哪里.
我希望尽可能不引人注目,这样我就可以打包并将其添加到遗留代码中.
当使用反引号(在perl中)时,我似乎与大括号扩展有某种不一致
print `ls -d ~/{a,b}`;
Run Code Online (Sandbox Code Playgroud)
工作正常,但当我尝试
print `ls -d ~/{a}`;
Run Code Online (Sandbox Code Playgroud)
我明白了:
Run Code Online (Sandbox Code Playgroud)ls: cannot access /home/user/{a}: No such file or directory
我尝试了各种引用,间距和逃脱但无济于事.我的问题是,有没有办法强制扩张?我意识到如果我自己动起来,我可以一起避免这个问题,但我对这种行为感到好奇
我在bash和tcsh下试过这个.当直接在tcsh下使用时,该命令有效,但在bash下则没有
早上好/下午/晚上好!我正在制作一个程序,我遇到了一个问题.此程序应从给定文本创建QR代码,并应将其颜色更改为RGB或HEX中的给定颜色.但是,当我把HEX颜色代码变成RGB"解释器"......好吧,你可以看到这个问题:输入 - 61c3ff和输出应该给我看一些像R = 61到十进制(97),G = c3到十进制( 195)和B = ff到十进制(255).但在输出中我只能看到R = 6,G = 12,B = 15.这到底是什么?这是我的代码的一部分,它必须识别给出哪一个颜色代码:RGB或HEX并且必须将HEX"转换"为RGB(例如,RGB中的{#} 9effec将是158 255 236).
for i in color:
if i == " ":
color = color.split()
x = color[0]
y = color[1]
z = color[2]
else: # If HEX color
if color[0] == "#": # if it starts with "#"
color = color[1:]
decX = color[0:1]
decY = color[2:3]
decZ = color[4:5]
x = int(decX, 16)
y = int(decY, 16)
z = int(decZ, 16)
print(color, …
Run Code Online (Sandbox Code Playgroud) 我试图以面向对象的方式在Class和Function中传递变量列表但面临一些错误,我不明白它有什么问题我在Eclipse中使用PyDev
class Mydetails:
__details = ''
def __init__(self,details): # constructor
#self.__details['country'] = details['country']
self.__details = details
def set_element(self,details):
self.__details = details
def get_list(self,details):
return self.__details
details = ['ABC','DEF','GHI','JKL']
pdetails = Mydetails()
pdetails.set_element(details)
print(pdetails.get_list())
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "C:\workspace\python\pythonapp\list.py", line 18, in <module>
pdetails = Mydetails()
TypeError: __init__() missing 1 required positional argument: 'details'
Run Code Online (Sandbox Code Playgroud)