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

Q

quakewang

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.
 
P

Paul Lalli

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
 
Q

quakewang

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
 
J

John Bokma

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)

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
 
P

Paul Lalli

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
 
Q

quakewang

"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.
 
L

Lars Haugseth

* John Bokma said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,824
Latest member
Nater888

Latest Threads

Top