Connecting Tech Pros Worldwide Forums | Help | Site Map

Why do classes output "1" to screen unexpectedly?

Newbie
 
Join Date: Aug 2007
Posts: 6
#1: Aug 24 '07
I have a parent and child class:

parent:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl -w   
  2. #class clFormMaker
  3. package clFormMaker;
  4.  
  5. sub new {
  6.     #constructor
  7.     my ($class_name, $name, $action, $method, $enctype) = @_;
  8.     my ($self) = {};
  9.     bless ($self, $class_name);
  10.     return $self;
  11. }
  12.  
  13. #functions
  14. sub makeform {
  15.     #make the form
  16.     my ($self) = @_;
  17.  
  18.     print "<form name='" . $self->{'_name'} . "' action='" . $self->{'_action'} .  
  19.                 "' method='" . $self->{'_method'} . "' enctype='" . $self->{'_enctype'} . "'>";
  20. }
  21.  
  22. sub closeform {
  23.     #close the form
  24.     print "</form>";
  25. }
  26.  
  27. return(1);
  28.  
and a child class

child:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl -w   
  2. #class clControlMaker
  3.  
  4. use clFormMaker;
  5. package clFormMaker::clControlMaker;
  6.  
  7. #use strict;
  8. BEGIN {@ISA = qw(clFormMaker);}
  9.  
  10. sub new {
  11.     #constructor
  12.     my ($class_name) = @_;
  13.     my ($self) = clFormMaker->new(@_);
  14.     #my ($self) = {};
  15.  
  16.     bless ($self, $class_name);
  17.     return $self;
  18. }
  19.  
  20. sub maketextbox {
  21.     #make a textbox with name=$name, style=$style
  22.     my ($self, $name, $style) = @_;  
  23.  
  24.     my $markup = "<input type='textbox' name='$name' style='$style'><br>";
  25.     return $markup;
  26. }
  27.  
  28. return(1);
  29.  
To use them I have the following code:

Expand|Select|Wrap|Line Numbers
  1. use clControlMaker;
  2. my $control = clFormMaker::clControlMaker->new('formname', 'polymorphism.pl'); 
  3. print $control->makeform();
  4. print $control->maketextbox('tester', 'width: 200px;');
  5. print $control->maketextbox('thename', 'width: 200px');
  6.  
If I just use the maketextbox method in the clControlMaker class and omit the makeform method (in the clFormMaker class) then the textboxes draw just fine. However, the problem I'm having is that every time I use one of the parent class methods (clFormMaker) a '1' is outputted to the screen. Presumably this is something to do with the return value 1 showing that the use statement worked.

How do I stop this outputting to the screen?

miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#2: Aug 25 '07

re: Why do classes output "1" to screen unexpectedly?


Greetings and welcome to TSDN,

I can't say for sure what is causing your problem. However, you should never use a return statement outside of a subroutine. I'm actually surprised that your code doesn't fail to compile because of your "return(1)" statements.

Obviously, you're trying to observe the rule that any package needs to include a true as the last statement to include successful execution. However, you should simply include a bare "1;" at the end instead of a return statement.

I don't know if this is the source of your problem, but it might be.

- Miller
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#3: Aug 25 '07

re: Why do classes output "1" to screen unexpectedly?


Also, along with the -w, which is equivelant to "use warnings" (personally, I ditch the -w and actually put in the use warnings pragma) you should also have the "use strict" pragma in your script as well.

Regards,

Jeff
Newbie
 
Join Date: Aug 2007
Posts: 6
#4: Aug 28 '07

re: Why do classes output "1" to screen unexpectedly?


Thanks for your suggestions guys, I've taken those on board. I'm new to Perl so all much appreciated.

However, I'm still getting these pesky '1's outputted and am rather perplexed as to why.....! Any other ideas - anyone?
Newbie
 
Join Date: Aug 2007
Posts: 6
#5: Aug 28 '07

re: Why do classes output "1" to screen unexpectedly?


Solved it and it was my stupid mistake.

Because I was using print in the class method as well as when calling the method, I presume it printed the 1 to signify the method completed successfully. Anyway, by simply removing print when calling the method it worked fine.

Mutter mutter, what a plonker I am etc.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#6: Aug 28 '07

re: Why do classes output "1" to screen unexpectedly?


That's funny! I completely missed your print statements before your function calls. Good catch on your own code.

Regards,

Jeff
Reply