Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize Perl Data::Dumper output in PHP

I have a result of export variable in Perl like this string:

$VAR1 = {
    'guard' => undef,
    'work_hand' => undef,
    'images' => 
        {'1' => 
            {
            'mini_height' => 150,
            'width' => 150,
            'extension' => 'jpg',
            'filename' => 'object_1.1330907414.96873.jpg',
            'mini_width' => 150,
            'class' => 'Ontico::Image',
            'height' => 150,
            'mini_filename' => 'object_1.1330907414.96873.mini.jpg',
            'size' => 26053,
            'symname' => 'big_logo'
            },
        '2' => 
            {
            'width' => 48,
            'extension' => 'jpg',
            'alt' => 'Даниэле Галлоппа',
            'height' => 48,
            'mini_filename' => 'object_91.1235312905.mini.jpg',
            'size' => 12809,
            'symname' => 'logo',
            'mini_height' => 150,
            'filename' => 'object_91.1235312905.jpg',
            'mini_width' => 150,
            'class' => 'Ontico::Image'
            }
        },
        'show_league_banner' => 0,
        'back_hand' => undef,
        'weight_category' => undef,
        'stick_position' => undef
    };

How can I deserialize this data in PHP?

P.S. I already have data in this format in DB, I cannot change it to json or another.

like image 876
Dimmduh Avatar asked Sep 10 '12 13:09

Dimmduh


1 Answers

You've got a number of suggestions for trying to parse it one way or another, but the real question is why?

Why not just have a small Perl program that loads it, and spits out an equivalent JSON string.

You could then either call that Perl program from within your PHP to do the conversion; this would mean you are using Perl to read the Perl format, which would guarantee correct conversion.

Or (better yet) run it against your entire database in a batch, to get rid of the Perl-specific data format from the DB; then you can just use PHP's standard JSON functions.

That would then make life so much simpler in your PHP code (or in any other language you need to read the data with at a later date).

like image 85
SDC Avatar answered Sep 30 '22 13:09

SDC