Why do classes output "1" to screen unexpectedly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pilsdumps
    New Member
    • Aug 2007
    • 6

    Why do classes output "1" to screen unexpectedly?

    I have a parent and child class:

    parent:
    [CODE=perl]
    #!/usr/local/bin/perl -w
    #class clFormMaker
    package clFormMaker;

    sub new {
    #constructor
    my ($class_name, $name, $action, $method, $enctype) = @_;
    my ($self) = {};
    bless ($self, $class_name);
    return $self;
    }

    #functions
    sub makeform {
    #make the form
    my ($self) = @_;

    print "<form name='" . $self->{'_name'} . "' action='" . $self->{'_action'} .
    "' method='" . $self->{'_method'} . "' enctype='" . $self->{'_enctype'} . "'>";
    }

    sub closeform {
    #close the form
    print "</form>";
    }

    return(1);
    [/CODE]

    and a child class

    child:
    [CODE=perl]
    #!/usr/local/bin/perl -w
    #class clControlMaker

    use clFormMaker;
    package clFormMaker::cl ControlMaker;

    #use strict;
    BEGIN {@ISA = qw(clFormMaker) ;}

    sub new {
    #constructor
    my ($class_name) = @_;
    my ($self) = clFormMaker->new(@_);
    #my ($self) = {};

    bless ($self, $class_name);
    return $self;
    }

    sub maketextbox {
    #make a textbox with name=$name, style=$style
    my ($self, $name, $style) = @_;

    my $markup = "<input type='textbox' name='$name' style='$style'> <br>";
    return $markup;
    }

    return(1);
    [/CODE]

    To use them I have the following code:

    [CODE=perl]
    use clControlMaker;
    my $control = clFormMaker::cl ControlMaker->new('formname' , 'polymorphism.p l');
    print $control->makeform();
    print $control->maketextbox('t ester', 'width: 200px;');
    print $control->maketextbox('t hename', 'width: 200px');
    [/CODE]

    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?
    Last edited by miller; Aug 24 '07, 06:04 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    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

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      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

      Comment

      • pilsdumps
        New Member
        • Aug 2007
        • 6

        #4
        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?

        Comment

        • pilsdumps
          New Member
          • Aug 2007
          • 6

          #5
          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.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

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

            Regards,

            Jeff

            Comment

            Working...