perl_writing_modules

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Subject: [OT] Error reads: Do you need to predeclare new?
From: Kenneth Lee <kenneth.lee@alfacomtech.com>
Date: Tue, 01 Aug 2000 19:11:09 +0800

This may not be mod-perl related.

I wrote a module CGI::CommonObject which is a subclass of CGI.
If I preload them both in conf/startup.pl, error occurs when I try to 
instantiate CGI::CommonObject.

  bash$ perl test.pl
  Bareword found where operator expected at /www/perl/test.pl line 3, near
"new CGI::CommonObject"
	  (Do you need to predeclare new?)
  [Tue Aug  1 18:51:06 2000] [error] syntax error at /www/perl/test.pl line
3, near "new CGI::CommonObject"

Where test.pl is simply

  #!/usr/bin/perl
  use CGI::CommonObject;
  my $q = new CGI::CommonObject;

And conf/startup.pl (irrelevant parts snipped)

  ...
  use CGI qw(-compile :standard);
  use CGI::CommonObject qw(:CommonObject);
  ...

The error gone if I don't preload CGI before CGI::CommonObject, or if I 
run the script under shell.

The question is what has confused perl that it treats "new" as a bareword?
I couldn't find any hints both in the code of CGI.pm and my own module.

===

Subject: Re: [OT] Error reads: Do you need to predeclare new?
From: Kenneth Lee <kenneth.lee@alfacomtech.com>
Date: Tue, 01 Aug 2000 19:26:58 +0800

A few more tests confirmed that I can't preload CGI::CommonObject in 
startup.pl, otherwise the error occurs.
Everything works fine after I remove the line in startup.pl, and let 
Apache::Registry load it.

===

Subject: Re: [OT] Error reads: Do you need to predeclare new?
From: "Jeremy Howard" <jh_lists@fastmail.fm>
Date: Tue, 1 Aug 2000 21:49:14 +1000

From: "Kenneth Lee" <kenneth.lee@alfacomtech.com>
> > I wrote a module CGI::CommonObject which is a subclass of CGI.
> > If I preload them both in conf/startup.pl, error occurs when I try to
> > instantiate CGI::CommonObject.
> > ...
> >   my $q = new CGI::CommonObject;

Kenneth, _never_ use that style of calling class methods. To really
understand why, read Damien Conway's excellent 'Object Oriented Perl', in
which in gives an excellent overview of the many problems this causes with
inherited classes.

Instead, use:
my $q = CGI::CommonObject->new();

It's less pretty, perhaps, but _much_ more reliable.

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu