perl中的文件处理程序在子例程中不起作用

时间:2017-07-13 08:22:50

标签: perl

#!/bin/perl

open( $WP, ">/home/Octa.txt" );

# Subroutine test
sub test {   
    $var1 = shift; 
    print $WP "TESTING\n"; 
}

# Subroutine func
sub func {
    $var = shift;    

    if ( $var eq "Ocat" ) { 
        print $WP "String found\n"; 
        test($var); 
    }
    else { 
        print $WP "String not found\n"; 
    } 
}

$var3 = "Octa"; 
func($var3);

问题是代码无法在test子例程或{< func if else`部分的subroutine, but it prints in the条件内写入任何内容#39; FUNC'子程序。

1 个答案:

答案 0 :(得分:3)

首先,有一个拼写错误 - 您针对$var测试"Ocat",而Octa则打算测试test

因此永远不会调用String not found子例程,只打印use warnings; use strict; my $file = 'Octa.txt'; open my $WP, '>', $file or die "Can't open $file: $!"; my $var3 = "Octa"; func($WP, $var3); #Subroutine test sub test{ my ($fh, $var1) = @_; print $fh "TESTING\n"; } #Subroutine func sub func{ my ($fh, $var) = @_; if ($var eq "Octa"){ print $fh "String found\n"; test($fh, $var); } else { print $fh "String not found\n"; } }

如果更正并且输出文件位于用户可写位置,则您的程序可以正常工作。

但是,有必要进行一些改进。

/home

我已更改输出文件名,因为用户通常无法写入open目录。

评论

  • 使用open的三参数形式要好得多,在这种情况下,你会得到一个词法文件句柄,它可以很好地传递并且是作用域的。至少可以说,这个问题是一个很好的例子,说明全局文件句柄如何让事情变得混乱。

  • 始终检查/home来电。首先,你真的可以写到use warnings;目录吗?

  • 始终使用use strict;/home

    启动程序

还有另一种失败的可能性,它将上述评论中的做法结合在一起。

open中的文件通常不可由用户写入,在这种情况下,发布的程序无法正常工作。

但是如果没有检查use warnings(会失败)并且没有$WH(每当我们触摸无效的import requests import io import mimetypes import xml.etree.ElementTree as ET import sys api = '' urls =["https://.../api/images/products/22", "https://.../api/images/products/31", "https://.../api/images/products/37", "https://.../api/images/products/46", "https://.../api/images/products/212"] def encode_multipart_formdata(file_name,content): """Encode files to an http multipart/form-data. :param files: a sequence of (type, filename, value) elements for data to be uploaded as files. :return: headers and body. """ BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$' CRLF = '\r\n' L = [] L.append('--' + BOUNDARY) L.append( 'Content-Disposition: form-data; \ name="%s"; filename="%s"' % ("image", file_name)) L.append('Content-Type: %s' % get_content_type(file_name)) L.append('') L.append(content) L.append('--' + BOUNDARY + '--') L.append('') body = CRLF.join(L) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY } return headers, body def get_content_type(file_name): """Retrieve filename mimetype. :param filename: file name. :return: mimetype. """ return mimetypes.guess_type(file_name)[0] or 'application/octet-stream' def get_image_url(url): """get from a given url the image url""" r = requests.get(url, auth=(api,"")) tree = ET.fromstring(r.content) return tree.find("image").find("declination").get("{http://www.w3.org/1999/xlink}href") def delete_image(url): """deletes the image on prestashop given by url""" url2 = get_image_url(url) requests.delete(url2, auth=(api,"")) def load_image(file_name): """loads image to upload""" fd = io.open(file_name, "rb") content = fd.read() fd.close() return content, file_name def upload_image(url, file_name): """uploads new image to a given url""" content, file_name = load_image(file_name) header, body = encode_multipart_formdata(file_name, content) requests.post(url, data=body, auth=(api,""), headers=header) if __name__ == "__main__": file_name = sys.argv[1] content, file_name = load_image(file_name) for i in urls: delete_image(i) upload_image(i,file_name) 文件句柄时都会打印),我们将看不到任何这些错误;相反,程序将安静地运行并完成,但它不会写输出文件。

相关问题