Learning Perl CGI and OOP, not printing @c

N

Nene

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;
 
N

Nene

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/
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
    my $object = Misc->new();


What do you do with @e?




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.
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;



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.
That is a typo error, sorry.


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 :)
 
J

Jürgen Exner

Nene said:
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
 
N

Nene

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



Jue has already pointed you to the FAQ for this one.



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.)



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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top