By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

Why do classes output "1" to screen unexpectedly?

P: 6
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?
Aug 24 '07 #1
Share this Question
Share on Google+
5 Replies


miller
Expert 100+
P: 1,089
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
Aug 25 '07 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Aug 25 '07 #3

P: 6
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?
Aug 28 '07 #4

P: 6
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.
Aug 28 '07 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
That's funny! I completely missed your print statements before your function calls. Good catch on your own code.

Regards,

Jeff
Aug 28 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.