This is a repost, to correctly start a new thread.
Apologies to all for the duplication.

/R

Rob Richardson wrote:
> Greetings!

Hi Rob.

> I have the following class in a .pm file:
>
> use warnings;
> use strict;
> package ScheduleDay;
>
> use Train;
>
> sub new
> {
>     my $self = {};  # $self is a reference to an anonymous, empty (for now) hash
>     bless $self;
>     return $self;

No need for the return statement. The 'bless' function returns the correct
value for a constructor precisely so that it can be used as the last
executable line.

> }
>
> sub AddTrain
> {
>     my $self = shift;
>     my $trainName = $_[0];

It would be nice to check that a non-empty parameter has been
passed:

    my $trainName = $_[0] || 'The Little Engine That Could';

>     # $self->{$trainName} = new Train;
>     # $self->{$trainName}->Load(@_);
>     print "   Added train named ", $trainName, "<br>";
> }
>
> "Where no one has gone before!";
>
>
> When I run perl -c on this, I get the following messages:
> Subroutine new redefined at Train.pm line 106.

It's telling you that you have two definitions of ScheduleDay::new.
Presumably Train.pm has no 'package Train;' statement?

> Useless use of a constant in void context at scheduleday.pm line 24.
> scheduleday.pm syntax OK

As long as you have

    use warnings 'void';

in effect, Perl will issue a warning if you use a constant other than the
'standard' true/false values (0, '0', '', undef for false and 1, '1' for true).
Replace

    "Where no one has gone before!";

with

    1;

and you will be OK.

> What is going on here?  Why can't I have a class that uses another
> class and have both classes have a "new" method?  What do I have to do
> to get this scoped correctly?
>
> Also, I am getting strange behavior when running this through Internet
> Explorer.  If the print statement in AddTrain() is modified to avoid
> the use of $trainName, the program runs as expected and IE displays a
> page with one line of print for every train I try to add.  However, if
> I try to print the name of the train as contained in $printName

$trainName ?

>(and as shown above), the result age is never displayed and IE just waits
> and waits and waits for something that never happens.

Again, your problem is at a higher plain than we mere mortals can
comprehend.

There's nothing wrong here. All I can suggest is that either there's
something in $trainName that upsets something else somewhere
or you're changing more than just adding the print statement.
We really need to see the calling code, but you may get some
mileage in printing the train name in hex so that you can see what's
actually in it.

    print "Added train named ";
    printf "%02X&nbsp;", ord $_ foreach split //, $trainName;
    print "<br>\n";

but that's a bit of a long shot.

HTH,

Rob








-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to