我想在Perl脚本中执行以下一行,以使一行与变量enum LongPollError:ErrorType{
case IncorrectlyFormattedUrl
case HttpError
}
public class LongPollingRequest: NSObject {
var GlobalUserInitiatedQueue: dispatch_queue_t {
return dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)
}
var GlobalBackgroundQueue: dispatch_queue_t {
return dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.rawValue), 0)
}
var longPollDelegate: LongPollingDelegate
var request: NSURLRequest?
init(delegate:LongPollingDelegate){
longPollDelegate = delegate
}
public func poll(endpointUrl:String) throws -> Void{
let url = NSURL(string: endpointUrl)
if(url == nil){
throw LongPollError.IncorrectlyFormattedUrl
}
request = NSURLRequest(URL: url!)
poll()
}
private func poll(){
dispatch_async(GlobalBackgroundQueue) {
self.longPoll()
}
}
private func longPoll() -> Void{
autoreleasepool{
do{
let urlSession = NSURLSession.sharedSession()
let dataTask = urlSession.dataTaskWithRequest(self.request!, completionHandler: {
(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
if( error == nil ) {
self.longPollDelegate.dataReceived(data)
self.poll()
} else {
self.longPollDelegate.errorReceived()
}
})
dataTask.resume()
}
}
}
}
和owner
的值匹配,并将其从文件中删除:
type
perl -i -ne\"print unless /\b${owner}\b/ and /\b${type}\b/;\" /tmp/test
的内容:
/tmp/test
当我在shell中执行时,这完全正常,但在Perl脚本中也不行。
我尝试使用反引号node01 A 10.10.10.2
node02 A 10.20.30.1
和system
。似乎没什么用。
exec
是否可以在Perl脚本中执行Perl一个内衬?
如果是这样,我在这里做错了什么?
注意:我不需要使用sed,grep,awk等从文件中删除一行的解决方案。
答案 0 :(得分:5)
您不希望从shell生成Perl代码,因此您要在shell中使用以下其中一项:
perl -i -ne'
BEGIN { $owner = shift; $type = shift; }
print unless /\b\Q$owner\E\b/ and /\b\Q$type\E\b/;
' "$owner" "$type" /tmp/test
或
ARG_OWNER="$owner" ARG_TYPE="$type" perl -i -ne'
print unless /\b\Q$ENV{ARG_OWNER}\E\b/ and /\b\Q$ENV{ARG_TYPE}\E\b/;
' /tmp/test
Perl等价物是
system('perl',
'-i',
'-n',
'-e' => '
BEGIN { $owner = shift; $type = shift; }
print unless /\b${owner}\b/ and /\b${type}\b/;
',
$owner,
$type,
'/tmp/test',
);
和
local $ENV{ARG_OWNER} = $owner;
local $ENV{ARG_TYPE} = $type;
system('perl',
'-i',
'-n',
'-e' => 'print unless /\b\Q$ENV{ARG_OWNER}\E\b/ and /\b\Q$ENV{ARG_TYPE}\E\b/;',
'/tmp/test',
);
答案 1 :(得分:0)
您可以模拟while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
userChoice = prompt("Invalid choice. Please change your answer.");
};
和-i
标记,而不是调用单行。 -n
只需要一个while循环。 -n
涉及创建和写入临时文件,然后将其重命名为输入文件。
要选择临时文件名,您可以使用-i
将processId附加到您选择的基本名称。更复杂的解决方案可以使用File::Temp。
/tmp/scriptname.$$