#!/usr/bin/perl -w =head1 NAME xpm2perl - convert an xpm file into valid Perl syntax as an embeddable image =head1 SYNOPSIS xpm2perl < test.xpm > test_xpm.pl xpm2perl test.xpm > test_xpm.pl =head1 DESCRIPTION X Pixel Map files use a simple C syntax that is meant to be #included directly into your C programs as embedded image data. It's possible to use XPM data in Gtk2-Perl programs as well, but XPM files are not valid Perl syntax. This utility converts the data inside an XPM into valid Perl code that you can paste directly into your Perl program. The data in the XPM file must have a name --- that is, the line which declares the array-of-strings strings variable must look like this: static char * some_name [] = { rather than this: static char *[] = { The latter appears to be allowed by some XPM loaders, but it not valid C syntax and would result in a compile failure if you tried to include this in a C program. C uses the array symbol name as the name of the string array for the Perl code output, so this name is mandatory. Be warned that this only does a simple substitution-based conversion, and is rather easy to trick. =head1 SEE ALSO L L L C in L =head1 AUTHOR muppet =head1 LICENSE This program is available under the same terms as Perl itself. =cut @lines = <>; while (@lines) { last if $lines[0] =~ m/\/\*\s*XPM\s*\*\//; shift @lines; } die "not xpm data\n" unless @lines; shift @lines; $lines[0] =~ s/static char \*\s*(\w+)\[\] = {/our \ $1 = (/; print $lines[0]; shift @lines; foreach (@lines) { chomp; # convert C comments to Perl comments. only catches comments # that are alone on the line. s{^(\s*)/\*(.*)\*/\s*$}{$1# $2}; # escape 's s/'/\\'/g; # convert enclosing double quotes to non-interpolating single quotes s/^\s*"(.+)"(,|};?|)\s*$/'$1'$2/; # watch for the closing brace. if (s/};\s*$/);/) { $quit++; } print "$_\n"; last if $quit; }