Problem accessing instance variables in object's method. 
Author Message
 Problem accessing instance variables in object's method.

   Colleagues, I am new to Perl and I am having the following problem. It seems
that my methods are not able to properly access the instance variables
declared in the constructor. The following are two Perl5 fragments and I am
following the technique for creating instance variables as per the BOT.

#!/pkg/perl5/bin/perl

package TestObject;

sub new
{
    my $self = {};

    print "-> $self{'myName'} \n";
    bless $self;

Quote:
}

sub Print
{
   my $self = shift;         # this should give me the object or package?

   print "this is me: $self{'myName'} and this is the message $theMessage \n";

Quote:
}

1

---
(second file)
#!/pkg/perl5/bin/perl

package main;

use TestObject;

$x = "andrew";

$a = new TestObject($x);
$b = new TestObject("francis");
$c = new TestObject("testing");

$a->Print("message one");
$b->Print("message two");
$c->Print("message three");

but unfortunately this is the result I get

-> andrew
-> francis
-> testing
this is me: testing and this is the message message one
this is me: testing and this is the message message two
this is me: testing and this is the message message three

   I was expecting, andrew, francis, testing respectively. I tried using
the de{*filter*}. Unfortunately, the de{*filter*} does not seem to work with "my"
so I had to change to local(). Still, what puzzled me is that the value of
$self would be that of a HASH pointing to a different value, presumably
each different object. Still, I keep getting "testing" which should not be.
What is going wrong and what approach should I be taking?

Thanks in advance,

Andrew Francis



Fri, 02 May 1997 04:05:20 GMT  
 Problem accessing instance variables in object's method.

:>sub new
:>{
:>    my $self = {};

:
:You're confusing the SCALAR $self with the HASH %self. As %self is not
:declared a my() variable, it is global and stays the same throughout
:the program.
:
:Try $self->{'myName'} instead of $self{'myName'} in all three places.
:

:slice of length 1 and may sometimes give you results you're not
:expecting.

     1  use strict;
     2  sub new  
     3  {
     4      my $self = {};

     6  }

If you used perl -w and use strict, all of these would have shown up;
piping stderr through splain, we'll learn:

     $ perl -wc /tmp/x 2>&1 | splain
     % perl -wc /tmp/x |& splain

Global symbol "self" requires explicit package name at /tmp/x line 5.
   (F) You've said "use strict vars", which indicates that all variables
   must either be lexically scoped (using "my"), or explicitly qualified
   to say which package the global variable is in (using "::").



   value of an array.  Generally it's better to ask for a scalar value
   (indicated by $).  The difference is that $foo[&bar] always behaves
   like a scalar, both when assigning to it and when evaluating its

   and provides a list context to its subscript, which can do weird things
   if you're only expecting one subscript.

--tom
--

    "What is the sound of Perl?  Is it not the sound of a wall that
     people have stopped banging their heads against?"



Fri, 02 May 1997 22:06:59 GMT  
 Problem accessing instance variables in object's method.

    T> If you used perl -w and use strict, all of these would have shown up;
    T> piping stderr through splain, we'll learn:

    T>      $ perl -wc /tmp/x 2>&1 | splain
    T>      % perl -wc /tmp/x |& splain

    T> Global symbol "self" requires explicit package name at /tmp/x line 5.
    T>    (F) You've said "use strict vars", which indicates that all variables
    T>    must either be lexically scoped (using "my"), or explicitly qualified
    T>    to say which package the global variable is in (using "::").



    T>    value of an array.  Generally it's better to ask for a scalar value
    T>    (indicated by $).  The difference is that $foo[&bar] always behaves
    T>    like a scalar, both when assigning to it and when evaluating its

    T>    and provides a list context to its subscript, which can do weird things
    T>    if you're only expecting one subscript.

Hey! What's this 'splain' thing? I can't find it in the docs!
--

 (301) 286-2396                          fax: (301) 286-1754

"Tut! Tut!" cried Sherlock Holmes. "You must act, man, or you are
lost. Nothing but energy can save you. This is no time for dispair."



Sat, 03 May 1997 03:06:30 GMT  
 Problem accessing instance variables in object's method.


[ snip ]

Quote:
>If you used perl -w and use strict, all of these would have shown up;
>piping stderr through splain, we'll learn:

>     $ perl -wc /tmp/x 2>&1 | splain
>     % perl -wc /tmp/x |& splain

My guess is that splain is a neat Perl script that parses Perl's
diagnostices and then referecnces the Perl manpage. Where can I (we)
get a copy of it?

Thanks
Joel
--
               T E C H N E   R E S E A R C H    L I M I T E D          

          Finchampstead, Wokingham   +   Phone: +44 (734) 730.260      
          Berkshire RG11 4YD (UK)    +   Fax:   +44 (734) 730.272      



Sat, 03 May 1997 18:13:56 GMT  
 Problem accessing instance variables in object's method.




:
:[ snip ]
:
:>If you used perl -w and use strict, all of these would have shown up;
:>piping stderr through splain, we'll learn:
:>
:>     $ perl -wc /tmp/x 2>&1 | splain
:>     % perl -wc /tmp/x |& splain
:
:My guess is that splain is a neat Perl script that parses Perl's
:diagnostices and then referecnces the Perl manpage. Where can I (we)
:get a copy of it?

ftp from ftp.perl.com in /pub/perl/scripts/splain.gz

--toim
--

When the dinosaurs are mating, climb a tree.  --Steve Johnson



Sun, 04 May 1997 14:28:08 GMT  
 Problem accessing instance variables in object's method.


Quote:

>   Colleagues, I am new to Perl and I am having the following problem. It seems
>that my methods are not able to properly access the instance variables
>declared in the constructor. The following are two Perl5 fragments and I am
>following the technique for creating instance variables as per the BOT.

>#!/pkg/perl5/bin/perl

>package TestObject;

>sub new
>{
>    my $self = {};


You're confusing the SCALAR $self with the HASH %self. As %self is not
declared a my() variable, it is global and stays the same throughout
the program.

Try $self->{'myName'} instead of $self{'myName'} in all three places.


slice of length 1 and may sometimes give you results you're not
expecting.

Hope this helps!
--andreas

Quote:
>    print "-> $self{'myName'} \n";
>    bless $self;
>}

>sub Print
>{
>   my $self = shift;         # this should give me the object or package?

>   print "this is me: $self{'myName'} and this is the message $theMessage \n";
>}

>1

>---
>(second file)
>#!/pkg/perl5/bin/perl

>package main;

>use TestObject;

>$x = "andrew";

>$a = new TestObject($x);
>$b = new TestObject("francis");
>$c = new TestObject("testing");

>$a->Print("message one");
>$b->Print("message two");
>$c->Print("message three");

>but unfortunately this is the result I get

>-> andrew
>-> francis
>-> testing
>this is me: testing and this is the message message one
>this is me: testing and this is the message message two
>this is me: testing and this is the message message three

>   I was expecting, andrew, francis, testing respectively. I tried using
>the de{*filter*}. Unfortunately, the de{*filter*} does not seem to work with "my"
>so I had to change to local(). Still, what puzzled me is that the value of
>$self would be that of a HASH pointing to a different value, presumably
>each different object. Still, I keep getting "testing" which should not be.
>What is going wrong and what approach should I be taking?

>Thanks in advance,

>Andrew Francis



Fri, 02 May 1997 15:11:01 GMT  
 Problem accessing instance variables in object's method.
(Usual pre-emptive appology for my newsfeed's lag :-)


Quote:

>   Colleagues, I am new to Perl and I am having the following problem. It seems
>that my methods are not able to properly access the instance variables
>declared in the constructor. The following are two Perl5 fragments and I am
>following the technique for creating instance variables as per the BOT.

>#!/pkg/perl5/bin/perl

>package TestObject;

>sub new
>{
>    my $self = {};

>    print "-> $self{'myName'} \n";
>    bless $self;
>}

>sub Print
>{
>   my $self = shift;         # this should give me the object or package?

>   print "this is me: $self{'myName'} and this is the message $theMessage \n";
>}

>1

Try using -w and "use strict;" in the package.

In the new you assign a reference to an anon. hash to $self, but don't
use -> to stuff the anon. hash.  Thus the global %self gets things put
into $self{'myName'} while the objects are left unmolested.

#!/usr/bin/perl -w

use strict;

package TestObject;

sub new
{
    my $self = {};
    $self->{'myName'} = $_[1];
    print "-> $self->{'myName'} \n";
    bless $self;

Quote:
}

sub Print
{
   my ($self) = shift;     # this should give me the object or package?

   print "this is me: $self->{'myName'} and this is the message $theMessage \n";

Quote:
}

package main;

no strict;

$x = "andrew";

$a = new TestObject($x);
$b = new TestObject("francis");
$c = new TestObject("testing");

$a->Print("message one");
$b->Print("message two");
$c->Print("message three");

__END__

produces the results I expect you were expecting.

Hope this helps,

Mike

--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | 130C Baker Ave. Ext

Meiko tel: (508) 371 0088 x124    |



Tue, 06 May 1997 02:23:26 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Private instance variables in Objects ??

2. question about objects (modules) - instance variable

3. Filehandle as object instance variable?

4. NewBQ: Accessing list instance variable?

5. Instance Data / Instance Variables

6. Having problem with object instances - question

7. help: object instance data problem with hashes (example included)

8. Object value access methods

9. Problem with anonymous array as instance variable?

10. Can't locate object method fetchrow_hashref()

11. Can't locate object method with @ISA

12. What is: Can't locate object method?

 

 
Powered by phpBB® Forum Software