I
ioneabu
#!/usr/bin/perl
use strict;
use warnings;
package main;
my $q = new MYCGI;
print $q->h1("Hello, World!"),"\n";
package MYCGI;
use CGI;
our @ISA = ('CGI');
produces output:
Can't locate object method "new" via package "MYCGI" at ./test6.pl line
8.
If I put the MYCGI package before main it works (produces expected
output of CGI.pm h1 sub) and if I do this:
#!/usr/bin/perl
use strict;
use warnings;
use MYCGI; #this solves the problem. note: there is no MYCGI.pm
module, just the code below...
my $q = new MYCGI;
print $q->h1("Hello, World!"),"\n";
package MYCGI;
use CGI;
our @ISA = ('CGI');
It produces expected output.
I did not see anything like this in my reading specifically stating
that there has to be a forward declaration of the package or the
package has to come first for the @ISA inheritance mechanism to work
properly.
If I do not bother with inheritance and just implement the needed
function myself, it works fine:
#!/usr/bin/perl
use strict;
use warnings;
#no need to use MYCGI here now that I am not inheriting from CGI in the
package
#why the difference?
my $q = new MYCGI;
print $q->h1("Hello, World!"),"\n";
package MYCGI;
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub h1
{
my ($self, $text) = @_;
$text = '<h1>'.$text.'</h1>';
return $text;
}
I was wondering why this is with @ISA use, that I have to 'declare' the
package with the @ISA at the top of the main package with a 'use'
statement or put the package ahead of main when, if there is no
inheritence involved in the package, I don't have to do this. I never
would have noticed if I had not tried putting the MYCGI package in the
same file since making it a separate module would have forced my to use
the 'use' statement and it would have just worked as expected.
Thanks!
wana
(OT: maybe rendering problem with groups.google.com and me due to the
fact that I am using OS X at home and sometimes cut and paste from
terminal and sometimes from TextEdit. I did indent properly, but
Google takes my indentation out. I am open to suggestions for other
usenet services that are low cost or free.)
use strict;
use warnings;
package main;
my $q = new MYCGI;
print $q->h1("Hello, World!"),"\n";
package MYCGI;
use CGI;
our @ISA = ('CGI');
produces output:
Can't locate object method "new" via package "MYCGI" at ./test6.pl line
8.
If I put the MYCGI package before main it works (produces expected
output of CGI.pm h1 sub) and if I do this:
#!/usr/bin/perl
use strict;
use warnings;
use MYCGI; #this solves the problem. note: there is no MYCGI.pm
module, just the code below...
my $q = new MYCGI;
print $q->h1("Hello, World!"),"\n";
package MYCGI;
use CGI;
our @ISA = ('CGI');
It produces expected output.
I did not see anything like this in my reading specifically stating
that there has to be a forward declaration of the package or the
package has to come first for the @ISA inheritance mechanism to work
properly.
If I do not bother with inheritance and just implement the needed
function myself, it works fine:
#!/usr/bin/perl
use strict;
use warnings;
#no need to use MYCGI here now that I am not inheriting from CGI in the
package
#why the difference?
my $q = new MYCGI;
print $q->h1("Hello, World!"),"\n";
package MYCGI;
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub h1
{
my ($self, $text) = @_;
$text = '<h1>'.$text.'</h1>';
return $text;
}
I was wondering why this is with @ISA use, that I have to 'declare' the
package with the @ISA at the top of the main package with a 'use'
statement or put the package ahead of main when, if there is no
inheritence involved in the package, I don't have to do this. I never
would have noticed if I had not tried putting the MYCGI package in the
same file since making it a separate module would have forced my to use
the 'use' statement and it would have just worked as expected.
Thanks!
wana
(OT: maybe rendering problem with groups.google.com and me due to the
fact that I am using OS X at home and sometimes cut and paste from
terminal and sometimes from TextEdit. I did indent properly, but
Google takes my indentation out. I am open to suggestions for other
usenet services that are low cost or free.)