develooper Front page | perl.perl5.porters | Postings from July 2001

/usr/share/magic for perl Storable, Data::Dumper output files

From:
Jim Cromie
Date:
July 31, 2001 12:59
Subject:
/usr/share/magic for perl Storable, Data::Dumper output files
Message ID:
3B670E11.D64090C7@divsol.com
hi folks,  my 1st contribution :)

#!/usr/bin/perl

=head1 file magic specs for perl

 # Magic data for file(1) command.
 # Machine-generated from src/cmd/file/magdir/*; edit there only!
 # Format is described in magic(files), where:
 # files is 5 on V7 and BSD, 4 on SV, and ?? in the SVID.

 # new perl magic-specs
 # i cant test this ( i dont want to )
 0 string  perl-store perl Storable(v0.6) data
 >4 byte  >0  (net-order %d)
 >>4 byte  &01  (network-ordered)
 >>4 byte  =3  (major 1)
 >>4 byte  =2  (major 1)

 # this works on cpan/Metadata
 0 string  pst0  perl Storable(v0.7) data
 >4 byte  >0
 >>4 byte  &01  (network-ordered)
 >>4 byte  =5  (major 2)
 >>4 byte  =4  (major 2)
 >>5 byte  >0  (minor %d)

 # this example shows how much 'info' you could pack into line
 #  its probably too cluttered
 #0 string  \$VAR1\ =\  perl -MData::Dumper -e 'print Dumper(\$VAR1)'

 # use file-helper-string if present (the Ext is, well, extraneous)
 0 string  \#\ Data::Dumper::Ext perl Data::Dumper::Extended data
 >2 string  Data::Dumper::Ext # perl -MData::Dumper -e 'print
Dumper(\$VAR1)'

 # this suffers from dependence on default var-names
 #  $@% typing is really weak - and is primarily a counter-example
 0 string  \$VAR1\ =\  perl Data::Dumper data
 >8 string  [  (array)
 >8 string  {  (hash)
 >8 string  '  (scalar)


  if these are technically correct, they should be altered to provide
  the 'right' amount of info, where 'right' == get_consensus();

=head1 storable test

  I did before/after tests, looking for effects of new magic;

  1- scan filesystem, find one example of each file-type available,
  Repeat after change, look for masking of a file-type.

  2- list all files with type: data.  If magic-spec is specific
  enough, it wont classify any other files as 'perl Storable data'.

  My sloppy/incomplete tests indicate ( but dont prove ) that spec is
  basically correct, but 'proof by inspection' should be possible, esp
  for p5-porters.

 caveats: premature exit with "xargs: unmatched single quote" -> didnt
 finish entire filesytem, ymmv. (hints accepted)


=head1 Data::Dumper file magic

  file-magic for Data::Dumper files is harder; Data::Dumper doesnt use
  any fixed-strings which are guaranteed to be there, so we'd have to
  insert one.

  Ive started by pretended this isnt a problem, by telling file to
  look for $VAR1 (ie simple Dumper usage, but not named variables);

  scalar-array-hash distinction is useless and misleading when
  multiple $VAR's are Dump'd.

  Probably best solution is inserting a fixed-comment-string; a
  comment shouldnt interfere with subsequent eval-ing of string. tho
  obscure probs may lurk.

  Still leaves problem in that

  One possible problem is piping a dump-string thru a chomping
  process, then the end-of-comment-nl is stripped, and the entire
  string becomes a comment.

 `cat dd_all.dat` after running prog, or `grep dd_all $0`

=head1 Implementing

  implementing this in Data::Dumper seems straight-forward, but whats
  the (a good) interface ?

  Data::Dumper::output_versions (1);
  Data::Dumper::output_versions = 1;
  Data::Dumper::output_versioning (1);

  default or not ? (conservatism+CORE+peripheral-value says not)

  use Data::Dumper ( :versioned )

=head1 Other-Todo

    any other perlish formats that would be worth recognizing ?

   is it worth trying to  find "\npackage (\w+);\n" with magic ?  is
it more robust than *.pm ?

   reach consensus here; then submit to file-maintainer.

=cut

##########
use Storable;
use Data::Dumper;

##########
# set up simple files

print "\nstorable test\n";

$hello = "Hello World\n";
store (\$hello, 'st_scalar.dat');

@arr = ( 1, 2, "hi there\n");
store (\@arr, 'st_array.dat');

%table = ( first => 1, second => 2);
store \%table, 'st_hash.dat';

print `file st_*.dat`;

# uglytest();

##########

print "\nDumper test\n";

open (STRM, "> dd_scalar.dat");
print STRM Dumper ($hello);

open (STRM, "> dd_array.dat");
print STRM Dumper (\@arr);

open (STRM, "> dd_hash.dat");
print STRM Dumper (\%table);

open (STRM, "> dd_all.dat");
# insert file-helper-string ( note - must be at beginning of file for
file-magic)
print STRM "# Data::Dumper::Ext\n",
   Dumper ( $hello, \@arr, \%table);

print `'more' dd_*.dat`;

##########

if (1) {

print "\n\n############\n# Data::Dumper->Dump test\n";

open (STRM, "> dd_scalar.dat");
print STRM Data::Dumper->Dump ( [$hello] => ['hello']);

open (STRM, "> dd_array.dat");
print STRM Data::Dumper->Dump ( [\@arr] => ['array']);

open (STRM, "> dd_hash.dat");
print STRM Data::Dumper->Dump ( [\%table] => ['hash']);

open (STRM, "> dd_all.dat");
print STRM "# Data::Dumper::Ext\n",
  Data::Dumper->Dump
    ( [ $hello, \@arr, \%table ],
     [qw( hello arr table )], );

print `'more' dd_*.dat`;

}

##########


sub uglytest {

  open (STRM, "slocate / | xargs -n1000 file | ");

  my %types;

  $way1 = 0;
  $way2 = 1;

  if ($way1) {
    while (<STRM>) {
      chomp;
      ($name, $type) = split(/\s+/, $_, 2);

      # clean-up type (delete extra, instance specific data)
      $type =~ s/(symbolic link).*/$1/;
      $type =~ s/[:;,].*//;

      if (not exists $types{$type}) {
 $types{$type} = $name;
      }
    }
    print "found types\n";
    foreach (sort keys %types) {
      print "$_: $types{$_}\n";
    }
  }


  elsif ($way2) {
    while (<STRM>) {
      chomp;
      ($name, $type) = split(/\s+/, $_, 2);
      push @files, $name if $type eq 'data';
    }
    local $" = "\n";
    print "found types\n@files";
  }
}

__END__

jimc






nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About