WxEXPORTS

From WxPerl Wiki
Jump to: navigation, search

It is tempting but not necessary to start a program with

 use Wx ':allclasses'

or

 use Wx ':everything'

These are in fact two quite different things.

use Wx qw(:allclasses) will attempt to load Wx bindings to all available wxWidgets classes. This provides a minor convenience in that you do not have to remember to, for example, use Wx::Html, before using html classes. However it is wasteful as everything is loaded and possibly may cause faults at startup if there are issues with particular libraries on your system. For instance if you use Wx qw(:allclasses) then wxMediaCtrl must load with no problems on your system and have all dependencies available and installed or your code will fault at startup.

use Wx qw(:everything) is an alias for the more commonly used use ModuleName qw(:all).

The Wx module provides many export tags that can be used selectivly. This small program (contributed by Mark Dootson) provides an extensive list of all Wx export tags and their contents.

#! /usr/bin/perl

# List Wx export tags.
# Written by Mark Dootson.

use strict;
use warnings;
use Wx qw( :allclasses );
my %seen;
my $content;
my $taglist;
for my $tag ( sort keys(%Wx::EXPORT_TAGS) ) {
    next if $tag =~ /^(everything|all)$/;
    $content .=  qq(\n:$tag\n);
    $taglist .= qq(\t$tag\n);
    for my $exp (@{ $Wx::EXPORT_TAGS{$tag} }) {
        $content .=  qq(\t$exp\n);
        $seen{$exp} = 1;
    }
}

print qq(TAGLIST\n);
print $taglist;
print qq(\nTAGINDEX\n);
print $content;
print qq(\nNO TAGS\n);
for my $exp (@{ $Wx::EXPORT_TAGS{everything} }) {
    print qq(\n$exp) if !$seen{$exp};
}

This tool reads a wxperl source file and generates the necessary Wx tags. Note that sometimes some additional tweaking is required.

#!/usr/bin/perl -s

# Generate Wx tags for a source.
# Written by Johan Vromans.
# NOTE: Some manual tweaking of the results may be necessary.

use strict;
use warnings;
our $full;			# full list, not tags

my %rtags;
make_taglist() unless $full;

my %used;
while ( <> ) {
    next if /^\s*#/;
    while ( /\b(wx[A-Z_][A-Za-z_]*)\b/g ) {
	if ( $full ) {
	    $used{$1} = 1;
	    next;
	}
	$used{$rtags{$1}||$1} = 1;
    }
}

delete @used{ qw(wxPerl wxGlade wxWidgets wxVERSION wxMAC) };

unless ( %used ) {
    print "use Wx;\n";
    exit;
}

print "use Wx qw[\n";
for ( sort keys %used ) {
    print "\t  $_\n";
}
print "       ];\n";

sub make_taglist {
    use Wx qw( :allclasses );
    for my $tag ( sort keys(%Wx::EXPORT_TAGS) ) {
	next if $tag =~ /^(everything|all)$/;
	for my $exp (@{ $Wx::EXPORT_TAGS{$tag} }) {
	    $rtags{$exp} = ":$tag";
	}
    }
}