Class using a class: "new" confusion and strange print behavi or

Class using a class: "new" confusion and strange print behavi or

Post by Bob Showalt » Fri, 21 Mar 2003 22:09:26




> Greetings!

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

n.b. this constructor is not inheritable if you ever derive a subclass from
ScheduleDay. You might want to consider using:

   sub new {
      my $class = shift;
      $class = ref $class || $class;
      bless {}, $class;
   }

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

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

This is likely a problem in Train.pm, which you didn't share with us. Do you
get the same message from perl -c Train.pm?

Quote:> Useless use of a constant in void context at scheduleday.pm line 24.

A warning for the phrase. If you don't like the warning, replace the phrase
with the more idiomatic literal 1;

Quote:> scheduleday.pm syntax OK

So scheduleday.pm compiles OK. But it should be named ScheduleDay.pm.

Quote:

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

You can have this. Your problems lie elsewhere.

Quote:

> Also, I am getting strange behavior when running this through Internet
> Explorer.

Running what through Internet Explorer? Assuming code not in evidence :~)

Quote:> 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 (and
> as shown above), the result age is never displayed and IE just waits
> and waits and waits for something that never happens.

???
 
 
 

Class using a class: "new" confusion and strange print behavi or

Post by Rob Richards » Fri, 21 Mar 2003 22:40:07


Greetings!

Thanks to all of you for your thoughts.  I like Rob's idea of ORing
$_[0] with 'The Little Engine That Could'.  I'll try that.  I'm pretty
darn sure that Train.pm has a line that says "package Train;", but I'll
double-check.  

A bit of background:  This program will eventually be used to manage
the process of volunteers signing up over the Internet to work on
trains run by the Cuyahoga Valley Scenic Railroad.  I have a Schedule
class to manage the overall process, a ScheduleDay class (whose file
name is "ScheduleDay.pm" to manage the group of trains that will run on
a particular day, and a Train class to manage an individual train.  I
am using the Apache server on my local machine, and running my test
program through Internet Exporer, as in
http://localhost/crew/traintest.cgi .  

Thanks again!

RobR



> > Greetings!

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

> n.b. this constructor is not inheritable if you ever derive a
> subclass from
> ScheduleDay. You might want to consider using:

>    sub new {
>       my $class = shift;
>       $class = ref $class || $class;
>       bless {}, $class;
>    }

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

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

> This is likely a problem in Train.pm, which you didn't share with us.
> Do you
> get the same message from perl -c Train.pm?

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

> A warning for the phrase. If you don't like the warning, replace the
> phrase
> with the more idiomatic literal 1;

> > scheduleday.pm syntax OK

> So scheduleday.pm compiles OK. But it should be named ScheduleDay.pm.

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

> You can have this. Your problems lie elsewhere.

> > Also, I am getting strange behavior when running this through
> Internet
> > Explorer.

> Running what through Internet Explorer? Assuming code not in evidence
> :~)

> > 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
> (and
> > as shown above), the result age is never displayed and IE just
> waits
> > and waits and waits for something that never happens.

> ???

> --



__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

 
 
 

Class using a class: "new" confusion and strange print behavi or

Post by Bob Showalt » Sat, 22 Mar 2003 06:37:47



> ...
> Does any call to a class method then have the reference to the object,
> or the object itself, as the first parameter.

An "object" is a reference that has been blessed into a class. In a method

So, if $obj is a reference blessed into class "MyClass", then

   $ret = $obj->Method($arg);
   MyClass::Method($obj, $arg);

are similar in that both result in two items being passed to Class::Method.
However, the second call does not do inheritance, while the first does.

All this is detailed in perldoc perlobj.

 
 
 

1. java "Main class" error; what's a "main class"?

I'm using CW pro release 5; Mac OS 8.1

The "Targeting the Java VM" pdf file says that the "cannot find Main
Class" error when building a java applet is due to the Main Class name
being entered incorrectly in the Java Target settings.

My question: (don't laugh)

1. In the "Trivial App", what IS the main class?  Please spell it out
exactly so I can recognize the concept in the future

(I'm trying to learn java by hacking sample apps --this would help me greatly!)

Thanks,

--
Howard Salmon
Tucson, AZ

2. windows performance

3. How to transfer "actor" attribute to "class" diagram?

4. MAC UPGRADE

5. Bad Box

6. Newbie Problem W/7.0: "Couldn't create the new class"

7. Playing MP3s or WMAs in C# on Pocket PC

8. "Using ArcInfo ODE and Visual Basic" Class

9. !!!!!"""""IMPORTANTE"""""!!!!!

10. K7s5a """"ich krieg die KRIESE"""""

11. "HIM error class 8" x86 install

12. VBA Error: "Unable to set the Value property of the Range Class"