Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Prototypes being ignored with use_ok() from Test:More ?

by JPMH (Novice)
on Nov 13, 2012 at 16:08 UTC ( [id://1003647]=perlquestion: print w/replies, xml ) Need Help??

JPMH has asked for the wisdom of the Perl Monks concerning the following question:

I've got some weirdness I don't understand when I load a module with Test::More's use_ok()

I was using an array-reference prototype to pass an array by reference without the module user having to fuss about it -- but when I load the module with Test::More's use_ok() all the prototypes seem to be getting ignored.

What is it I'm missing ?

Here's a cut-down example module, CST2.pm:

package CST2; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(ArrayCount); sub ArrayCount(\@) { (my $array_Ref, undef) = @_; my $ct; foreach my $element (@{$array_Ref}) { $ct++ } $ct; } 1;
and a calling script,
#!/usr/bin/perl use strict; use warnings; use Test::More; #BEGIN:{ use_ok('CST2'); } use CST2; my @patternList = qw(one two three); my $count = ArrayCount(@patternList); print "$count\n";

With use CST2 everything works fine.

But if I change to BEGIN:{ use_ok('CST2'); }, then I get an error: 'Can't use string ("one") as an ARRAY ref while "strict refs" in use at CST2.pm line 12.'

To make the error go away, I have to change the call in the script to explicitly pass a reference my $count = ArrayCount(\@patternList);

Why is this happening? What is it I've missed, and should have done differently?

For what it's worth, perl --version tells me I'm running perl 5, version 14, subversion 2 (v5.14.2) built for cygwin-thread-multi-64int (with 7 registered patches, see perl -V for more detail); and it's version 0.98 of Test::More

Thanks in advance,

JH.

Replies are listed 'Best First'.
Re: Prototypes being ignored with use_ok() from Test:More ? (colon)
by tye (Sage) on Nov 13, 2012 at 16:52 UTC

    BEGIN { use_ok( ... ) } uses a BEGIN block. BEGIN:{ use_ok( ... ) } uses "BEGIN" as a useless label. Drop the colon.

    Or just stop using use_ok(). I see no value in it.

    - tye        

      Bingo! Thank you very much. I hadn't noticed the pesky colon, nor realised it would make so much difference.
Re: Prototypes being ignored with use_ok() from Test:More ?
by choroba (Cardinal) on Nov 13, 2012 at 16:26 UTC
Re: Prototypes being ignored with use_ok() from Test:More ?
by Corion (Patriarch) on Nov 13, 2012 at 16:28 UTC

    use_ok works at runtime, but prptotype parsing has alteady happened at compile time. My recommendation is to simply stop using use_ok, as it solves no problem and actually creates pitfalls.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1003647]
Approved by Tanktalus
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-03-19 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found