473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why do classes output "1" to screen unexpectedly?

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
5 1228
miller
1,089 Expert 1GB
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
3,509 Expert Mod 2GB
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
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
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
3,509 Expert Mod 2GB
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

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

Similar topics

3
by: TheLetti | last post by:
Hy! I've just downloaded the latest Java 2 SDK (j2sdk1.4.2_01). For my surprise in this version the servlet-classes are not integrated (e.g. the class javax.servlet). So I found all the...
1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
1
by: francois | last post by:
I'm looking for C++ classes library to be used instead of STL. As I'm working on a tiny embedded system 16MB up to 32MB, I need to reduce memory footprint needed by the STL due to its genericity....
24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
9
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. ...
3
by: Giggle Girl | last post by:
I have inherited some CSS from a former employee that has code like this: onmouseover="this.className='highlight_on standard_border'" where it appears to activate two different classes,...
0
by: Daniel | last post by:
how to parse xsl:output ... with xpath? "./xsl:output" does not work. is there other notation for this?
2
by: jain_tj | last post by:
Could anyone please help me with the following problem My xml file is ============== <fig id="F0000001"> <caption>Caption text</caption> <image id="I0000001" image.class="halftone"...
4
by: DiffThinkr | last post by:
Hi, Is there anyway of saving and retrieving the output screen. I don't want to use the saved file outside the program but I need to be able to put the output that I saved back into output as it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.