Bloodnok has asked for the wisdom of the Perl Monks concerning the following question:
Whilst attempting to develop a test harness, using Test::More, for a bespoke module I'm developing, I noticed the behaviour outlined in the following example code/results - simply put, it is that the second call to import() fails to create the class package.
I'm absolutely convinced I've missed something obvious - I've been looking at the problem for so long, I can no longer see the wood for the trees.
Could anyone enlighten this confused resident please ?
Package code:
Script:package WTF; use strict; use warnings; use Carp; sub define { my $self = shift; my $type = shift || croak "No type name"; my ($code, $parent, $subclass) = ('', __PACKAGE__, __PACKAGE__ . "\::$type"); # Get the package code template { local $/ = undef; while (<DATA>) { s,##PACKAGE##,$subclass,g; s,##PARENT##,$parent,g; $code .= $_; } my $WARNING = ''; local $SIG{__WARN__} = sub { $WARNING = "@_" }; eval $code; croak "Failed to create subclass: $subclass - $WARNING$@" if $WARNING || $@; croak "Problem with subclass: $subclass - cannot new()" unless $subclass->can(qw/new/); } print STDERR "Created type: $type\n"; } sub import { my $self = shift; while (@_) { my ($nm, $list) = splice(@_, 0 ,2); __PACKAGE__->define($nm => $list); } } 1; __DATA__ package ##PACKAGE##; use strict; use warnings; use ##PARENT##; my @ISA = ( qw/##PARENT##/ ); sub new { my $self = shift; bless \( my $scalar ), ref $self || $self; }
When WTF.pl is run, the following is seen:use strict; use warnings; # Emulate use BEGIN { require WTF; WTF->import(qw/some/); WTF->import(qw/more/); }
Update #1 When run on AIX (perl V 5.8.8), the same, or extremely similar, results are...C:\WINDOWS\system32\cmd.exe /c perl WTF.pl Created type: some Problem with subclass: WTF::more - cannot new() at WTF.pl line 8 BEGIN failed--compilation aborted at WTF.pl line 9, <DATA> line 1. shell returned 255 Hit any key to close this window...
Update #2 Ultimately, what I'm aiming for is to write a test harness that fully tests the usage of the module - I implemented it all in a single .t file, but am beginning to think I can only acheive my aim by having a plethora of, much smaller, .t files...!perl WTF.pl Created type: some Problem with subclass: WTF::more - cannot new() at WTF.pl line 8 BEGIN failed--compilation aborted at WTF.pl line 9, <DATA> line 1.
TIA
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem emulating use
by almut (Canon) on Jul 11, 2008 at 12:00 UTC | |
by Bloodnok (Vicar) on Jul 11, 2008 at 12:21 UTC | |
|
Re: Problem emulating use
by Arunbear (Prior) on Jul 12, 2008 at 19:46 UTC | |
by Bloodnok (Vicar) on Jul 12, 2008 at 22:55 UTC |