Hi Marco, see below for my response. On Thu, 18 Jun 2015 15:17:27 +0200 Marco <ma...@marcobaldo.ch> wrote:
> Hello. > > I need some help in understanding why the first > > my ($Count,$Saw)=$self->{_Portobj}->read(1); > > is executed without any problem , however the second identical statement > inside the while loop fails with the error > > Can't call method "read" on an undefined value at wt800.pm line 121 > The problem is that you misspelled "_Portobj" as "_PortObj". This is easier to catch if you use accessors: * http://perl-begin.org/tutorials/bad-elements/#accessing_object_slots_directly > The idea behind this coding is to be able to use any object providing a > "write" and a "read" method inside my object. (i.e. > messageDevice::SerialPort) > > I'm using Perl 2.7 on a OpenSuSE 13.2 system. > What do you mean by "Perl 2.7"? We're way past both Perl 2 and Perl 5.002. > Any hint? > > Tnx > > Marco > Some more comments on your code: > > sub get_answer { > > my ($self,$Orig_ref) = @_; > I personally don't like the "$Orig_ref" identifier style. "$orig_ref" or less preferably "$origRef" are better. > print $self,"\n"; > > my $stat=$self->{_Portobj}->write("aaaa") or die "pippo"; > my ($Count,$Saw)=$self->{_Portobj}->read(1); > You need some whitespace here (before/after the "=", etc.) > my $Count=9999; > my $Saw=""; > my @Resp; > > print $self,"\n"; > > while (($Saw == chr(10)) or ($Count == 0)) { $Saw == chr(10) should probably be "$Saq eq chr(10)". > print $self,"\n"; > my ($Count,$Saw)=$self->{_PortObj}->read(1); # will read > 1 char Misspelling here. > push(@Resp,ord($Saw)); Missing whitespace. > print ord $Saw,"\n"; > } > > } > > _Portobj is initialized in myObject as: > > sub new { > > my $class = shift; > my $Tpobj = shift; > > my $self = { > _Portobj => $Tpobj, > .... > You could separate the identifiers' components using underscores. > And the main () is > > my $PortObj = new Device::SerialPort ($Portname, $Quiet) > or die "Can't open device $Portname: $!\n"; > > my $Object = new myObject($Portobj); > Indirect object notation: http://perl-begin.org/tutorials/bad-elements/#indirect-object-notation Regards, Shlomi Fish (NOTE: perl-begin.org is a site I initiated and maintain) -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Escape from GNU Autohell - http://www.shlomifish.org/open-source/anti/autohell/ When Chuck Norris drops a cat, it falls on its back so it won’t lose eye contact with Chuck. — http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/ Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/