Learning Perl CGI and OOP, not printing @c

Discussion in 'Perl Misc' started by Nene, Oct 20, 2011.

  1. Nene

    Nene Guest

    I’m learning PERL CGI with OOP.

    Below is my code for the user to select the number of servers.

    print "<p><em>Servers</em><br>",
    checkbox_group(
    -name=>'sequence',
    -default=>'',
    -linebreak=>'true',
    -values=>['10.233.22.66',
    '10.233.22.67',
    '10.233.22.68',
    '10.233.22.69',
    '10.233.22.70',
    '10.233.22.71',
    '10.233.22.72',
    '10.233.22.73'],

    -labels=>\my %labels,
    -attributes=>\my %attributes);

    ###This is how process the selection of servers.

    @values = param('sequence');
    chomp(@values);

    foreach my $line (@values) {
    my $line =~ s/$_/$line:a/;
    push(@n, "$line");
    }


    Here’s my OOP code that is in the same file.

    my $object = new Misc();
    my @e = $object->setNodes(@n);
    my @c = $object->getNodes();
    print "\n", br;
    #######################################
    print @c, br;
    #######################################
    But it’s not printing at all.
    #######################################

    Below is my package:

    package Misc;

    sub new
    {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
    }

    sub setPse {
    my ( $self, @pse ) = @_;
    $self->{_pse} = @pse if defined(@pse);
    return $self->{_pse};
    }

    sub getPse {
    my( $self ) = @_;
    return $self->{_pse};
    }

    sub setStatus {
    my ( $self, $status ) = @_;
    $self->{_status} = $status if defined($pse);
    return $self->{_status};
    }

    sub getStatus {
    my( $self ) = @_;
    return $self->{_status};
    }

    sub setNodes {
    my ( $self, $nodes ) = @_;
    $self->{_nodes} = $pse if defined($nodes);
    return $self->{_nodes};
    }

    sub getNodes {
    my( $self ) = @_;
    return $self->{_nodes};
    }

    1;
    Nene, Oct 20, 2011
    #1
    1. Advertising

  2. Nene

    Nene Guest

    On Oct 20, 4:33 pm, Ben Morrow <> wrote:
    > Quoth Nene <>:
    >
    > > I’m learning PERL CGI with OOP.

    > <snip>
    >
    > > ###This is how process the selection of servers.

    >
    > >                      @values = param('sequence');
    > >                      chomp(@values);

    >
    > >                      foreach my $line (@values) {
    > >                      my $line =~ s/$_/$line:a/;

    >
    > This doesn't do what you expect. 'my $line' allocates a new variable, so
    > the s/// will always be applied to the empty string. (If you'd had
    > warnings on you would have got an uninitialised value warning, thereby
    > possibly proving that my hatred of that particular warning is
    > unjustified.)


    This chuck of code is actually doing what it's intended to do: my
    $line =~ s/$_/$line:a/

    >
    > >                      push(@n, "$line");

    >
    > Where do you declare @n? Are you using 'strict'?


    Yes, I'm declaring 'strict', the 'my @n;' is at the top of the file.
    >
    > Why are you stringifying $line? You don't need to.


    Again, it's doing what it's suppose to do--is it bad?, for example:
    10.223.22.66:a 10.223.22.67:a 10.223.22.68:a 10.223.22.69:a
    >
    > >                                               }

    >
    > > Here’s my OOP code that is in the same file.

    >
    > > my $object = new Misc();

    >
    >     my $object = Misc->new();
    >
    > > my @e = $object->setNodes(@n);

    >
    > What do you do with @e?
    >
    > > my @c = $object->getNodes();
    > > print "\n", br;
    > > #######################################
    > > print @c, br;
    > > #######################################
    > > But it’s not printing at all.
    > > #######################################

    >
    > > Below is my package:

    >
    > > package Misc;

    >
    > You are likely to get in trouble in the future if you give your packages
    > very generic names like 'Misc'. It's worth giving them a prefix that's
    > likely to be unique, so you might call it something like Nene::pse.


    I usually don't call it these names, this is just a practice package.
    >
    > > sub new
    > > {
    > >     my $class = shift;
    > >     my $self = {};
    > >     bless $self, $class;
    > >     return $self;
    > > }

    >
    > > sub setPse {
    > >     my ( $self, @pse ) = @_;
    > >     $self->{_pse} = @pse if defined(@pse);

    >
    > Don't apply defined to an array. It doesn't return a useful answer. The
    > condition you want is 'if @pse', which tells you if @pse has any
    > elements.


    Ok, but I used this example from a tutorial.

    >
    > Apart from that, this assignment probably doesn't do what you expect.
    > @pse is in scalar context, so it returns the number of elements in the
    > array. If you want to set the whole list as the value of the attribute
    > you need to take a reference:
    >
    >     $self->{_pse} = \@pse if @pse;
    >
    > >     return $self->{_pse};
    > > }

    >
    > > sub getPse {
    > >     my( $self ) = @_;
    > >     return $self->{_pse};

    >
    > If you're taking a reference, as above, and you want getPse to return a
    > list, you will need to dereference:
    >
    >     return @{ $self->{_pse} };
    >
    > I'm afraid the syntax is unavoidably rather awkward.
    >
    > > }

    >
    > > sub setStatus {
    > >     my ( $self, $status ) = @_;
    > >     $self->{_status} = $status if defined($pse);

    >

    That is a typo error, sorry.



    >
    > >     return $self->{_status};
    > > }

    >
    > > sub getStatus {
    > >     my( $self ) = @_;
    > >     return $self->{_status};
    > > }

    >
    > > sub setNodes {
    > >     my ( $self, $nodes ) = @_;
    > >     $self->{_nodes} = $pse if defined($nodes);

    >
    > Where does $pse come from? Assuming you meant $nodes, you are calling
    > ->setNodes with a list, but only using the first value in that list. If
    > you want to store the whole list you need


    Again, a typo. I changed the variables a little so that my boss won't
    catch me at Google groups :)
    >
    >     my ($self, @nodes) = @_;
    >     $self->{_nodes} = \@nodes if @nodes;
    >
    > as for setPse above. You'll also need the corresponding modification to
    > getNodes, of course.
    >
    > All these, taken together, mean that getNodes is always returning a
    > single 'undef', so it's not surprising you don't see anything when you
    > print it.
    >
    > Ben
    Nene, Oct 21, 2011
    #2
    1. Advertising

  3. Nene <> wrote:
    >On Oct 20, 4:33 pm, Ben Morrow <> wrote:
    >> Quoth Nene <>:
    >> >                      push(@n, "$line");

    >>
    >> Why are you stringifying $line? You don't need to.

    >
    >Again, it's doing what it's suppose to do--is it bad?, for example:
    >10.223.22.66:a 10.223.22.67:a 10.223.22.68:a 10.223.22.69:a


    See "perldoc -q quoting":
    What's wrong with always quoting "$vars"?

    jue
    Jürgen Exner, Oct 21, 2011
    #3
  4. Nene

    Nene Guest

    On Oct 20, 9:26 pm, Ben Morrow <> wrote:
    > Quoth Nene <>:
    >
    > > On Oct 20, 4:33 pm, Ben Morrow <> wrote:
    > > > Quoth Nene <>:

    >
    > > > >                      foreach my $line (@values) {
    > > > >                      my $line =~ s/$_/$line:a/;

    >
    > > > This doesn't do what you expect. 'my $line' allocates a new variable,so
    > > > the s/// will always be applied to the empty string. (If you'd had
    > > > warnings on you would have got an uninitialised value warning, thereby
    > > > possibly proving that my hatred of that particular warning is
    > > > unjustified.)

    >
    > > This chuck of code is actually doing what it's intended to do: my
    > > $line =~ s/$_/$line:a/

    >
    > Goodness me, you're right, it's actually got so many levels of Wrong it
    > comes out giving the right answer. However, what that piece of code does
    > is:
    >
    >     - allocate a variable called $line to hold the current item from
    >       @values,
    >     - allocate a second variable, *also* called $line, which is
    >       initialized to undef,
    >     - match the current value of $_ against that new variable: since $_
    >       happens to be empty and the new $line is undef, it matches,
    >     - replace the matched portion with "$line:a"; but since the second
    >       $line hasn't come into scope yet this uses the *first* $line,so
    >       you get the result you wanted.


    Thank you for the detailed explaination.
    >
    > I can't imagine how you came by that bit of code, but I'm sure the
    > sequence of steps above wasn't what you intended.
    >
    > If you add '$_ = "x";' above the loop you will see it breaks everything,
    > because the LHS of the s/// no longer matches the empty string. If you
    > had turned warnings on you would have seen three uninitialised value
    > warnings, which should have let you know something was not as you
    > expected.
    >
    > What you want is simply
    >
    >     for my $value (@values) {
    >         my $line = "$value:a";


    Thank you for cleaning up my code.

    >
    > > > >                      push(@n, "$line");

    >
    > > > Where do you declare @n? Are you using 'strict'?

    >
    > > Yes, I'm declaring 'strict', the 'my @n;' is at the top of the file.

    >
    > When posting code for review it's helpful if you can post all the bits
    > that matter. Ideally you should cut down your program as much as you can
    > while still leaving the bit that you're having a problem with, then post
    > all of what's left.
    >
    > > > Why are you stringifying $line? You don't need to.

    >
    > > Again, it's doing what it's suppose to do--is it bad?, for example:
    > > 10.223.22.66:a 10.223.22.67:a 10.223.22.68:a 10.223.22.69:a

    >
    > Jue has already pointed you to the FAQ for this one.
    >
    > > > > sub setPse {
    > > > >     my ( $self, @pse ) = @_;
    > > > >     $self->{_pse} = @pse if defined(@pse);

    >
    > > > Don't apply defined to an array. It doesn't return a useful answer. The
    > > > condition you want is 'if @pse', which tells you if @pse has any
    > > > elements.

    >
    > > Ok, but I used this example from a tutorial.

    >
    > Most Perl tutorials are complete rubbish. Stick to the docs which come
    > with perl and books written by reputable authors. (The list in perldoc
    > -q book is a good place to start, if you're looking for a book.)
    >
    > > > > sub setNodes {
    > > > >     my ( $self, $nodes ) = @_;
    > > > >     $self->{_nodes} = $pse if defined($nodes);

    >
    > > > Where does $pse come from? Assuming you meant $nodes, you are calling
    > > > ->setNodes with a list, but only using the first value in that list. If
    > > > you want to store the whole list you need

    >
    > > Again, a typo. I changed the variables a little so that my boss won't
    > > catch me at Google groups :)

    >
    > This isn't Google Groups, it's Usenet. It's been here a lot longer than
    > Google, not to mention the Web.


    Thank you for clarifying.
    >
    > I'm not really interested in the politics between you and your boss, but
    > until you know a bit more about what you are doing please don't post
    > code you haven't actually run. It just wastes everybody's time, which is
    > rather rude when you're asking for help.


    I did run the code many times, I try not post until I'm really stuck.
    However, if I have posted both the
    package and the script, it would make your job easier. I have learned,
    thank you.
    >
    > Ben
    Nene, Oct 21, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. timh

    learning perl and cgi

    timh, Dec 16, 2003, in forum: Perl
    Replies:
    6
    Views:
    641
    Leif K-Brooks
    Dec 19, 2003
  2. LenS

    Newbie learning OOP

    LenS, May 29, 2005, in forum: Python
    Replies:
    5
    Views:
    325
    Terry Hancock
    Jun 1, 2005
  3. LenS

    Newbie learning OOP 2nd ?

    LenS, May 30, 2005, in forum: Python
    Replies:
    2
    Views:
    298
  4. kath
    Replies:
    4
    Views:
    585
    J. Gleixner
    Apr 9, 2007
  5. Nene

    Learning OOP

    Nene, Jun 15, 2012, in forum: Perl Misc
    Replies:
    1
    Views:
    500
    Jim Gibson
    Jun 15, 2012
Loading...

Share This Page