What does the qw(:everything) do on a use line in perl?

2.7k views Asked by At

This is an embarrassing question to ask but why does this line work while the other doesn't?

Working line:

use strict;
use warning;
use Wx qw(:everything);
my $dialog = Wx::MessageDialog->new(
    $self,
   "About test\n" .  "Version 0.01\n",
   "About Test",
   wxOK | wxCENTRE
);

Non-working line:

use strict;
use warning;
use Wx;
my $dialog = Wx::MessageDialog->new(
   $self,
   "About test\n" .  "Version 0.01\n",
   "About Test",
   wxOK | wxCENTRE
);

Error message, from non-working line:

Bareword "wxOK" not allowed while "strict subs" in use at test.pl line 123.
Bareword "wxCENTRE" not allowed while "strict subs" in use at test.pl line 123.
BEGIN not safe after errors--compilation aborted at test.pl line 348.
2

There are 2 answers

0
gpojd On BEST ANSWER

It is the equivalent of this:

BEGIN {
    require 'Wx';
    Wx->import( ':everything' );
};

That code will import ':everything' from Wx into the current namespace. My guess is that Wx uses Exporter and has a group of things to import when called with ':everything'.

You can check the Wx's source and Exporter's source to try to make more sense of it.

I missed your working/non-working example. The non-working one doesn't work because the wxOK and wxCENTRE constants are not imported into the current namespace. This is done using Exporter, as explained above.

0
ikegami On
qw(:everything)

is very similar to

(split ' ', ':everything')

So

use Wx qw(:everything);

is the same as

use Wx ':everything';

The string :everything is passed to the Wx's export routine (import). The semantics are completely up to the module, but I presume Wx to defines it to be "export everything I can possibly export". Most modules use :ALL for that, if they provide it at all.

It looks like Wx doesn't export anything unless you ask, so wxOK and wxCENTRE don't exist in your namespace unless you tell Wx to export them using

use Wx qw( wxOK wxCENTRE );

or

use Wx qw( :everything );

For future reference, please include error messages and such instead of simply saying something doesn't work.