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

Hi Perl Monks, I am trying to read a file which has JSON into a string using read_file of File::Slurp. But when I print the output string i see some junk characters at the beginning.Could someone please let me know how can I get rid of the junk characters? Thanks in advance, Anisha

Replies are listed 'Best First'.
Re: JSON into string
by Corion (Patriarch) on Aug 31, 2016 at 12:03 UTC

    File::Slurp is notoriously bad at reading files that have an encoding. Most likely, a good approach is to avoid using it and reading the file yourself.

Re: JSON into string
by choroba (Cardinal) on Aug 31, 2016 at 12:15 UTC
    Junk characters at the beginning? File::BOM maybe?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: JSON into string
by stevieb (Canon) on Aug 31, 2016 at 13:14 UTC

    Any chance you could post a section of the data? From the beginning leading up to and at least a bit past the beginning of the valid JSON?

      Thank you so much everyone. The problem was with the encoding only. Thanks again.
Re: JSON into string
by GotToBTru (Prior) on Aug 31, 2016 at 12:07 UTC

    substr?

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: JSON into string
by neilwatson (Priest) on Aug 31, 2016 at 18:46 UTC

      :) thats terribly unsweetened :P

      use Path::Tiny qw/ path /; my $foo = path($file)->slurp_utf8; my $foo = path($file)->slurp_raw; my $foo = path($file)->slurp( {binmode => ":raw"} );