Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Console output != file output

by mkk82 (Initiate)
on Sep 01, 2013 at 17:29 UTC ( #1051833=perlquestion: print w/ replies, xml ) Need Help??
mkk82 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there!

I really Need help!

In a nutshell: The output here:

print "start" . $type . "end"; is unequals the output here:
open (FH, 'out.txt'); print FH "start" . $type . "end"; close FH;

Some more details:

I receive an email via Mail::IMAPClient and store the message in one single scalar variable. Thereafter, use some RegEx to find several pieces of text.

The RegEx look like this:

my $type = ""; if ($msgString =~ m/Typ\s*:\s*(\w+)/i) { $type = $1; $type =~ s/\r\n//g; }#if print "start" . $type . "end";

This should give me:

start orange end

This works on a couple of text pieces I am after but on some the console output is screwed up and looks like this:

start range

As you can see, the first letter is missing and the final "end" string is not printed at all.
I even tried something like this and could believe my eyes:

print "start" . $type . "end\n"; print "$type\n"; print "xxxxxxxxxxxxxxxxxxxxxxx$type\n";

Console output:
xxxxxxxxxxxxxxxxxxxxxx
endrange

And keep in mind: When I write to a file, I get exactly what everbody would expect.
Any ideas?
PS: Perl 5.8.8 for MSWin32

Comment on Console output != file output
Select or Download Code
Re: Console output != file output
by toolic (Chancellor) on Sep 01, 2013 at 17:53 UTC

    Tip #1 from the Basic debugging checklist: warnings

    print() on closed filehandle FH at

    You are opening the file for input, not output: see open. This works for me:

    use warnings; use strict; my $type = 123; open (FH, '>out.txt'); print FH "start" . $type . "end"; close FH;

      He should be doing 3 arg open and would be best if he change that file handle to a var. Just some best practices.

      use warnings; use strict; my $type = 123; my $FH; open ($FH, '>', 'out.txt'); print $FH ("start" . $type . "end"); close $FH;
Re: Console output != file output
by ig (Vicar) on Sep 01, 2013 at 19:39 UTC

    If you provided a complete program and sample data that manifests the problem it would be easier to help. With only the fragments you have provided, one can only guess what might be causing the problem.

    The console output you provide suggests that there might be unexpected carriage control characters in your output that cause your output to be written in unexpected, overlapping locations. When I have such problems I use the od command to examine the output in details, including non-printing characters (with od -c). This might help you identify what is going on.

    You could try perl script.pl | od -c

Re: Console output != file output
by kschwab (Priest) on Sep 02, 2013 at 05:11 UTC

    Sounds to me like you have non-printable characters in the output.

    When you look at the created file with a utility like 'more' or an editor, it probably looks fine. The funny characters are there, but the utility filters them in a way where you can see what you want to see. However, on the console, they aren't filtered for you.

    Try adding $type=~s/[^[:print:]]/_/g; (before you print it) and see if any underscores start showing up in $type. That would indicate non-printables (backspaces, vertical tabs, etc) are in your data.

Re: Console output != file output
by tinita (Parson) on Sep 02, 2013 at 10:07 UTC
    Whenever you have a problem like this, don't just "print".
    use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper $type;
    Now you will see all unprintable characters and you can continue programming instead of blindly guessing =)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051833]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2014-07-22 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (116 votes), past polls