SPUG: must dumpvar.pl be required BEGIN

Jacinta Richardson jarich at perltraining.com.au
Wed Apr 1 17:12:36 PDT 2009


Michael R. Wolf wrote:

> require 'dumpvar.pl';
> 
> I could only get it to work by wrapping it in BEGIN
> 
> BEGIN {
>      require 'dumpvar.pl';
> }

require happens at run-time.  Thus the contents (and hints) from required files
are not available at compile time.  Wrapping a require in a BEGIN block means
that it is instead compiled at the start of the compile process, and the symbol
table is populated appropriately.  It's worth being aware that "use" which we
use with modules (such as in "use strict;") is roughly equivalent to:

BEGIN {
	require 'module.pm';
	module::import;
}

> Is this standard?  Is there a better (for the 'shorter' definition of
> 'better') way to get the module to make &main::dumpValue available?

Files which are required are rarely modules.  Require isn't very common these
days, as making modules is usually better.  For example you just need to create
a package of the same name as your file, use Exporter and @EXPORT_OK if you want
that functionality and "use" it instead of requiring.  For example:

# File: Dumpvar.pm   # Non-pragmatic modules usually start with a capital
package Dumpvar;
use strict;
use warnings;
use base 'Exporter';

our @EXPORT_OK = qw(dumpValue);

sub dumpValue {
	...
}

1;

# Main code
#!/usr/bin/perl -w
use strict;
use Dumpvar qw(dumpValue);

my @should_pass;

print "These should pass\n";
dumpValue \@should_pass;

> Is there a procedural module that I could include with a 'use' instead
> of having to 'require' it?  I found the Dumpvalue module, but would
> prefer a non-OO one if it exists.

It doesn't appear, from what you've shown us that Dumpvalue is OO, it takes a
reference, but if it were OO I'd expect something more like:

	my $dumper = Dumpvar->new;
	$dumper->dump(\@should_pass);

Without knowing what dumpValue is doing this question is very hard to answer.
Although perhaps you just want Data::Dumper:

use Data::Dumper;

print "These should pass\n";
print Dumper \@should_pass;

Data::Dumper comes standard with Perl which is an additional plus.

> print "These should fail\n";
> dumpValue \@should_fail;

As Yitzchak pointed out, this can be fixed trivially by giving Perl a better
hint that you're referring to a subroutine it hasn't seen yet by using parens:

	dumpValue(\@should_fail);

It's a good idea to *always* use parentheses on all non-core functions,
especially when you're not passing in an argument:

	noArgs();

This avoids the problem you've seen here, and a few more.

All the best,

	J

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the spug-list mailing list