Chinaunix

标题: perl中字节与字符串如何转换 [打印本页]

作者: zk1878    时间: 2012-08-06 23:27
标题: perl中字节与字符串如何转换
         本人是学java的 ,perl小菜鸟,但非常喜欢perl。
         有个问题一直困扰小弟,java中字节 与字符串的转换相当简单,但perl的让我看起来不知如何转换,貌似用pack和unpack可以做到
比如 我打算按字节读文件,然后打印到控制台,怎么实现呢,假设一次读取1024个字节, 文件是文本文件。java中是这样的
  1. String file = "C:/Users/ke/Desktop/a.pl";
  2.                 FileInputStream in = new FileInputStream(file);
  3.                 byte[]buf = new byte[1024];
  4.                 int len;
  5.                 while((len = in.read(buf))>0){
  6.                         String s = new String(buf,0,len);
  7.                         System.out.print(s);
  8.                 }
  9.                 in.close();
复制代码
相同的作法,perl如何写呢,望高人指点,务必写的详细点,谢谢
作者: hrb_kid    时间: 2012-08-07 07:45
本帖最后由 hrb_kid 于 2012-08-07 10:11 编辑

在*nix/bsd上可以这样。

  1. #!/usr/bin/perl -w
  2. use strict;

  3. my ( $file, $handle, $buffer ) = '/foo/bar/baz';

  4. open $handle, '<', $file or die "open: $!\n";
  5. print $buffer while read $handle, $buffer, 1024;
  6. close $handle;
复制代码
当然这只是个简单应用,假设的文件内容是ascii。如果有unicode,则要为已经打开的文件句柄开启binmode。(详见perldoc)
  1. binmode $handle, ':utf8';  
  2. binmode STDOUT, ':utf8';
复制代码

作者: zk1878    时间: 2012-08-07 09:19
回复 2# hrb_kid
read函数貌似是读字符的,你上面的代码还是用字符来读的,没涉及到字节,是不是应该用sysread

   
作者: jason680    时间: 2012-08-07 09:27
回复 1# zk1878

1. default use Line in 文本文件, easy and simple

2. what do you want to do
   How to read is not important in normal case
   
作者: hrb_kid    时间: 2012-08-07 10:00
本帖最后由 hrb_kid 于 2012-08-07 10:13 编辑

回复 3# zk1878

“貌似“的判断根据是什么?读过read的perldoc么?

       read FILEHANDLE,SCALAR,LENGTH,OFFSET
       read FILEHANDLE,SCALAR,LENGTH
               Attempts to read LENGTH characters of data into variable SCALAR
               from the specified FILEHANDLE.  Returns the number of charac-
               ters actually read, 0 at end of file, or undef if there was an
               error (in the latter case $! is also set).  SCALAR will be
               grown or shrunk so that the last character actually read is the
               last character of the scalar after the read.

               An OFFSET may be specified to place the read data at some place
               in the string other than the beginning.  A negative OFFSET
               specifies placement at that many characters counting backwards
               from the end of the string.  A positive OFFSET greater than the
               length of SCALAR results in the string being padded to the
               required size with "\0" bytes before the result of the read is
               appended.

               The call is actually implemented in terms of either Perl’s or
               system’s fread() call.  To get a true read(2) system call, see
               "sysread".

               Note the characters: depending on the status of the filehandle,
               either (8-bit) bytes or characters are read.  By default all
               filehandles operate on bytes
, but for example if the filehandle
               has been opened with the ":utf8" I/O layer (see "open", and the
               "open" pragma, open), the I/O will operate on UTF-8 encoded
               Unicode characters, not bytes.  Similarly for the ":encoding"
               pragma: in that case pretty much any characters can be read.

另外,sysread是系统指令。原则上讲如对同一文件句柄进行系统操作,则都要是系统操作,最好不要混用。
open,seek,read,print,close对应的系统操作是sysopen,sysseek,sysread,syswrite,sysclose。
作者: cdtits    时间: 2012-08-07 10:22
回复 3# zk1878


    请教一下:你的代码中字节与字符区别?
作者: zk1878    时间: 2012-08-07 17:33
本帖最后由 zk1878 于 2012-08-07 17:35 编辑

         这个帖子可能提问得不太好,java中字节与字符串的概念比较清晰, 出于这两个语言的对比,所以想问下而已,并非不知道如何读取打印文本文件。
         另外我发这个帖子的目的是, 我想用socket构建http请求,来下载一个文件,比如图片,mp3之类的,这样基于字符串的方式就不行了,因为
服务器响应除了http头部信息外,内容是二进制的。 像这种情况,通过socket发送http请求怎么实现呢,我看过java的实现,是先获取请求头Content-Length,然后在读取http响应正文中Content-Length 长度的字节,这Content-Length 长度的字节就是要下载的文件,   所以才想问问perl中字节的问题
作者: cdtits    时间: 2012-08-07 19:06
html 貌似文本文件
作者: 只是一个红薯    时间: 2013-05-09 10:48
@zk1878这个问题解决了么
作者: zk1878    时间: 2013-05-14 09:58
回复 9# 只是一个红薯


    用sysread来按字节读取了,先解析http的响应头部,解析出content-length的值,然后再进一步操作, 具体代码没写,应该是这个思路




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2