Question:

Dear perl expert, I got an odd line of string, <span title=""HASH(0x225518)|HASH(0x225440)|HASH(0x15300fc)|HASH">"HASH(0x225518)|HASH(0x22...</span>

by  |  earlier

0 LIKES UnLike

I got this odd line of string in my file which I used in for my output of my program. The program I wrote just uses this statement:

sub writetofile {

open(PROFAIL, ">$profail");

print PROFAIL $_[0];

close(PROFAIL);

}

where this sub, named writetofile takes 1 argument and use it as the input string to be written into the file. One last time I ran it, it went through the string exactly in my file, but one time(this time), it show me this line on top of the exact string, where the rest is still ok. This is just like a header of the file, but I haven't done anything like that. All I've just done is just the sub, then the string passed to it is just consisted of a plain user data, like name, password, and so on in line-by-line basis. It went out ok before, but not this time. If you need to see what the inputs are, I can send it to you later.

Thanks.

 Tags:

   Report

2 ANSWERS


  1. The argument you sent to that function has &quot;garbage&quot; in it.  

    You forgot to check whether the open succeeded.

    You should use the File::Slurp module to read &amp; write entire files.


  2. The problem is the input.  Or, it probably is.  It could be in your $profail variable (because it&#039;s a global variable, so it may be getting changed elsewhere), but that&#039;s less likely.  The key is that whatever you&#039;re passing got screwed up somewhere along the line.  In fact, the bug probably has nothing to do with the writetofile function, or how you call it.  It&#039;s probably how you&#039;re building the string that you send to it.

    Somewhere along the line, you&#039;re attempting to put a hash reference into string context.  A hash reference, when in string context will look something like this:

    HASH(0x110595)

    So it&#039;s telling you that it&#039;s a hash reference, located at memory address 0x110595.  In your case, you&#039;re getting (apparently) 3 hash references, separated by pipe characters.  So you&#039;re likely creating that string incorrectly, and the string that gets sent is a perfectly valid string (and getting written to the file exactly as it should be), but it&#039;s just not the string you were expecting.

    With some more details, we could probably track it down a lot better.

    DaveE

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.