tkly has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to read a file and print it, But I always get a junk value printed out in the first line.

Code:
#!/usr/local/bin/perl open (MYFILE, '2.pl'); print <MYFILE>; close (MYFILE);
Output:
&#8745;&#9559;&#9488;#!/usr/local/bin/perl open (MYFILE, '2.pl'); print <MYFILE>; close (MYFILE);
How do i get rid of that first few junk characters? If i write onto another file instead of stdout this doesn't happen..

Replies are listed 'Best First'.
Re: Perl prints junk before file
by bart (Canon) on Feb 04, 2012 at 20:31 UTC
    The first I thought when I read this, was "BOM marker?".

    But the bytes don't seem to fit.

      Oh. i dont know how to check that. or fix it. for now i'm using substr and removing the first 3 characters. :(
        3 characters? Or 3 bytes? That would be weird bytes, judging by the sample output you posted... But maybe they got converted again, after the fact. So it could be a BOM marker after all.

        You could try out File::BOM.

Re: Perl prints junk before file
by oko1 (Deacon) on Feb 04, 2012 at 22:01 UTC

    There's a number of hidden factors here that make direct answers to your question mostly meaningless. Without more information, there's not a whole lot to go on.

    • How was '2.pl' created? It is possible that some weird, or corrupted version of the BOM got inserted there.
    • What do you mean by "this doesn't happen" - are you 'cat'ting the resulting file, opening and reading it the same way as '2.pl', reading it in a text editor, or what? Depending on the answer to that, the characters may still be there but be invisible.
    • You _do_ realize that printing a file this way is generally not a good idea, right? Not particularly relevant in this case, but snarfing the entire content of an arbitrary-sized list is a good way to whack your memory.

    Possible solutions:

    • Try 'head -c 1 2.pl' to determine what, if anything, you have as the first character in your source file.
    • Print the content to another file, then read that resulting file with the above script. See if that same thing happens. "bvi", "vim -b", or ':set list' in "vi" (or any other method of viewing the file raw) would be of help here.
    • For Great Ghu's sake, put an ' or die' statement after your 'open'. It makes me nervous to see it sitting there naked and unprotected like that. :)
    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats
Re: Perl prints junk before file
by choroba (Cardinal) on Feb 04, 2012 at 21:07 UTC
    Very strange. Google has some hits on the sequence, most of them related to bittorents. I have no idea what it could mean.
Re: Perl prints junk before file
by chessgui (Scribe) on Feb 04, 2012 at 20:57 UTC
    Try this (I can gaurantee that the junk characters will disappear):
    unlink('2.pl');

    ...together with the file...