use print to write binary data to a filehandle seems not correct

Discussion in 'Perl Misc' started by quakewang@mail.whut.edu.cn, Nov 22, 2006.

  1. Guest

    hi,

    I get a image data from the internet and then save it into a
    newcreated file, but seems the saved data is not the same as the
    original one.

    $file_name++;
    my $success = open FILE_HD, "> $file_name";
    if ( ! $success) {
    # The open failed
    warn $file_name . "open failed! [$!]\n";
    }
    print FILE_HD $response->content;
    close FILE_HD;

    why?

    I shall use which function to write the binary data?

    thanks.
    , Nov 22, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > I get a image data from the internet and then save it into a
    > newcreated file, but seems the saved data is not the same as the
    > original one.
    >
    > $file_name++;
    > my $success = open FILE_HD, "> $file_name";
    > if ( ! $success) {
    > # The open failed
    > warn $file_name . "open failed! [$!]\n";
    > }
    > print FILE_HD $response->content;
    > close FILE_HD;
    >
    > why?
    >
    > I shall use which function to write the binary data?


    You haven't given enough information to lead to any reasonably certain
    solution, but have you read:
    perldoc -f binmode
    ?

    Paul Lalli
    Paul Lalli, Nov 22, 2006
    #2
    1. Advertising

  3. Guest

    Goooooooooooooooooooooooooooooooooood!!!!!!!!!!!!!!!

    Yes, I have read the "perldoc -f binmode" now, and I change my code to
    add the following line after I open the file handle:

    #use binary layer
    binmode FILE_HD, ":raw";

    then all is good, it is running and get pic for me now,
    ============================

    You said: "You haven't given enough information to lead to any
    reasonably certain solution."

    can you point out me a little furthur, what shall I give for this
    question, or what I do not know about filehandle, and I/O layers?

    thanks
    , Nov 22, 2006
    #3
  4. John Bokma Guest

    wrote:

    > hi,
    >
    > I get a image data from the internet and then save it into a
    > newcreated file, but seems the saved data is not the same as the
    > original one.
    >
    > $file_name++;
    > my $success = open FILE_HD, "> $file_name";
    > if ( ! $success) {
    > # The open failed
    > warn $file_name . "open failed! [$!]\n";
    > }
    > print FILE_HD $response->content;


    uhm, the open failed, what do you expect here to happen?

    > close FILE_HD;


    close ... or die ...

    (yes, close can fail, especially after writing)

    > why?


    Even easier (instead of writing yourself using binmode [1]), read the
    LWP::UserAgent documentation:

    $ua->get( $url , $field_name => $value, ... )

    ....


    Fields names that start with ``:'' are special. These will not
    initialize headers of the request but will determine how the response
    content is treated. The following special field names are recognized:

    :content_file => $filename

    so:

    $ua->get( $url, ':content_file' => $filename );
    ....
    ....


    [1] Also, check out File::Slurp

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Nov 22, 2006
    #4
  5. Paul Lalli Guest

    wrote:
    > You said: "You haven't given enough information to lead to any
    > reasonably certain solution."
    >
    > can you point out me a little furthur, what shall I give for this
    > question,


    Please read the Posting Guidelines that are posted to this group twice
    a week. They will teach you how to construct a useful post.
    Specifically, they instruct you to post a short-but-complete script
    that we can run. In your post, we had no way of knowing what was
    contained in your variables, other than your description. So we
    couldn't possibly know if your description was accurate, or if you had
    a bug in understanding the $response object, or any of a hundred other
    things that could have been wrong.

    Paul Lalli
    Paul Lalli, Nov 22, 2006
    #5
  6. Guest

    "Paul Lalli дµÀ£º

    > Please read the Posting Guidelines that are posted to this group twice
    > a week. They will teach you how to construct a useful post.
    > Specifically, they instruct you to post a short-but-complete script
    > that we can run. In your post, we had no way of knowing what was
    > contained in your variables, other than your description. So we
    > couldn't possibly know if your description was accurate, or if you had
    > a bug in understanding the $response object, or any of a hundred other
    > things that could have been wrong.
    >
    > Paul Lalli


    I will post the complete script next time, it is a program to get
    beautiful girl image from pic web site, now I can get pics, and save
    them into files, but it is slow, and I found it is very difficult to
    pick the wanted pic from handred of pics on every pages.

    I will use fork to increase the scipt to max speed, and try to find a
    way to pic the right pic I save as ... by hand ago.


    yes, seems, sometime the binary write even fail, i get a image can not
    be see by ACDSee, but that cases is rare, when I can download handreds
    or thousands of pics a day, I will go to research that problem.

    thanks.
    , Nov 22, 2006
    #6
  7. Re: use print to write binary data to a filehandle seems notcorrect

    * John Bokma <> wrote:
    >
    > Even easier (instead of writing yourself using binmode [1]), read the
    > LWP::UserAgent documentation:
    >
    > $ua->get( $url , $field_name => $value, ... )
    >
    > ...
    >
    >
    > Fields names that start with ``:'' are special. These will not
    > initialize headers of the request but will determine how the response
    > content is treated. The following special field names are recognized:
    >
    > :content_file => $filename
    >
    > so:
    >
    > $ua->get( $url, ':content_file' => $filename );


    This can be done even simpler by using the LWP::Simple module and its
    getstore() or mirror() methods.

    --
    Lars Haugseth

    "If anyone disagrees with anything I say, I am quite prepared not only to
    retract it, but also to deny under oath that I ever said it." -Tom Lehrer
    Lars Haugseth, Nov 22, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page