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

Hi Again,
what im trying to do is convert my hash file (key_value pair) to a binary file.
tried using the "store" command but didn't really work. I guess I miss used it as I never used it before.
I have an XML file I used the Dumper command to get the hash version of the XML file and then im just trying to convert it to binary.
any good way for doing that ?
also what does this mean "store \%table, 'file'"....sorry I know this might be simple but couldn't find online anything that explains it.
in this case what is table and what is file?

Replies are listed 'Best First'.
Re: Hash File to Binary ?
by roboticus (Chancellor) on Dec 11, 2013 at 19:41 UTC

    zak_s:

    First the helpful bit:

    If you're referring to Storable, then \%table is the reference to the hash structure you're saving, 'file' is the name of the file you want the data stored to.

    Now the unhelpful bit:

    Really, all files are utimately binary files, so there's little point in asking for help converting something into a binary file unless you describe what kind of binary file you may be interested in. The type you'll be interested in depends on what you're going to do with it.

    Since you don't provide any information describing where you want to go, I can't suggest a path to get there.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      To elaborate on the unhelpful answer, output from Data::Dumper looks something like this:

      $VAR1 = { 'stuhl' => 'wecker', 'tisch' => 'teppich', 'bett' => 'bild' };

      Let's say you've put that in a file named, helpfully, file1. Assuming there's nothing too weird in there, you can load it up and "convert it to binary" with Storable like so:

      use Storable; do 'file1'; store $VAR1, 'file2';

      You now have a "binary file" named file2. Let's see what's in there.

      >less file2 "file2" may be a binary file. See it anyway? y pst0^D 12345678^D^H^H^H^C^C^@^@^@ ^Fwecker^E^@^@^@stuhl ^Dbild^D^@^@^@bett ^Gteppich^E^@^@^@tisch

      Some words from the original file, and some binary gunk. But I doubt that helps you very much, so, to repeat roboticus, what do you want to do with this?

        This is exactly what im trying to do except the fact that I got the following error when running what you suggested:

        not a reference at C:\Users\myName\Documents\perl\Mycode.pl line 419. in this case its : store $VAR1, 'MYFILE_2';
        My Dump output is exactly as what you mentioned, nothing weird in it. Anyways here is my piece of code for this:
        print MYFILE Dumper($data); do 'MIFILE'; store $VAR1, 'MYFILE_2'; close (MYFILE); close (MYFILE2);
        do I need to use \% for VAR1 ?
        im running this on windows and Linux

      roboticus thanks for explaining. That was helpful.
      As you can see from my response below all im trying to do is use the command "store" on hash table that is stored in file.
      what is the proper way to do this? Or how can I store my Dumper output in a variable properly.
      tried this but didn't work where is my problem ?

      #CODE1 $data = $xml->XMLin("output.xml"); my $hash_table = Dumper($data); store \%hash_table, 'binary_hash.txt';
      #CODE2 $data = $xml->XMLin("output.xml"); print MYFILE Dumper($data); store MYFILE, 'binary_hash.txt';

        zak_s:

        Dumper only displays the data in readable form. The store function writes it into a file in such a way that retrieve can get it back out. Here's a quick example that stores a data structure to a file:

        #!/usr/bin/perl use strict; use warnings; use Storable; # You'll get it via XMLin, I just hardcoded some data my $data = { foo=>'bar', bim=>'bam' }; store $data, 'MyFile.data';

        Now, when we have another program that wants to use the data, we just retrieve it first:

        #!/usr/bin/perl use strict; use warnings; use Storable; use Data::Dumper; my $ref = retrieve('MyFile.data'); print Dumper($ref);

        We're only using Data::Dumper to display the data, you won't need it in either of your scripts unless you need to print it to the screen for debugging. (Which is what I often do.)

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        use XML::Simple; use Storable; $data = XMLin('foo.xml'); store $data, 'foo.dat';