http://www.perlmonks.org?node_id=522093

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

Hello monks, I have written a script that I intended to use to escape binary data that I plan to insert into a bytea field into a postgresql database. the specs for the escaping are here: http://www.postgresql.org/docs/8.1/static/datatype-binary.html see table 8-7 my script works, but apparently not correctly because the DB won't take the escaped data. I am returned: invalid input syntax for type bytea I would like some feed back on what I might be missing.
#!/usr/bin/perl open(BINFILE, "your favorite binary file here"); binmode(BINFILE); $escaped = ""; while(read(BINFILE, $char, 1)){ $char = escape(ord($char)); $escaped .= $char } print $escaped, "\n"; sub escape { $ordinal = shift(); %escapeList = (0 => "\\\\000", 1 => "\\\\001", 2 => "\\\\002", 3 => "\\\\003", 4 => "\\\\004", 5 => "\\\\005", 6 => "\\\\006", 7 => "\\\\007", 8 => "\\\\008", 9 => "\\\\009", 10 => "\\\\010", 11 => "\\\\011", 12 => "\\\\012", 13 => "\\\\013", 14 => "\\\\014", 15 => "\\\\015", 16 => "\\\\016", 17 => "\\\\017", 18 => "\\\\018", 19 => "\\\\019", 20 => "\\\\020", 21 => "\\\\021", 22 => "\\\\022", 23 => "\\\\023", 24 => "\\\\024", 25 => "\\\\025", 26 => "\\\\026", 27 => "\\\\027", 28 => "\\\\028", 29 => "\\\\029", 30 => "\\\\030", 31 => "\\\\031", 32 => "\\\\032", 39 => "\\\\047", 92 => "\\\\134", ); if(defined($escapeList{$ordinal})){ return $escapeList{$ordinal}; } else{ return chr($ordinal); } }
Thanks. p.s. i used this ascii reference as a guide: http://www.lookuptables.com/
FIXED: Never mind I am stupid. I was reading the wrong column for the conversion. Of course it is rejected because the codes are in HEX, NOT OCT. Sorry for wasting the post space.
Update: Now that I just verified that it works fine with the correct escape codes, would any one mind suggesting a cleaner, more elegant method than what I have?
Thanks, again.