Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Learning Perl CGI and OOP, not printing @c

Reply
Thread Tools

Learning Perl CGI and OOP, not printing @c

 
 
Nene
Guest
Posts: n/a
 
      10-20-2011
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;
 
Reply With Quote
 
 
 
 
Nene
Guest
Posts: n/a
 
      10-21-2011
On Oct 20, 4:33*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Nene <(E-Mail Removed)>:
>
> > 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:se.


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


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-21-2011
Nene <(E-Mail Removed)> wrote:
>On Oct 20, 4:33*pm, Ben Morrow <(E-Mail Removed)> wrote:
>> Quoth Nene <(E-Mail Removed)>:
>> > * * * * * * * * * * *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
 
Reply With Quote
 
Nene
Guest
Posts: n/a
 
      10-21-2011
On Oct 20, 9:26*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Nene <(E-Mail Removed)>:
>
> > On Oct 20, 4:33*pm, Ben Morrow <(E-Mail Removed)> wrote:
> > > Quoth Nene <(E-Mail Removed)>:

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's wrong calling a Perl/CGI script in Perl/CGI script under Tomcat server? kath Perl Misc 4 04-09-2007 09:21 PM
Python-cgi or Perl-cgi script doubt praba kar Python 1 07-30-2005 08:25 AM
learning perl and cgi timh Perl 6 12-19-2003 02:12 AM
Learning CGI, PERL, etc. Matt Beckwith HTML 21 08-01-2003 04:03 PM
Re: CGI Perl "use CGI" statement fail Jürgen Exner Perl 0 07-31-2003 02:00 PM



Advertisments