LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2009, 01:33 PM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Perl: Uninitialized value in pack


Hi. I'm trying to use the LEGO::NXT modules to control my NXT bot (over bluetooth). I installed just last week with CPAN. But my first run of the example program fails:
Code:
choward@adler /scratch/choward/robots/nxt $ ./test.pl 
Use of uninitialized value in pack at /usr/lib/perl5/site_perl/5.8.8/LEGO/NXT.pm line 498.
Can't call method "type" without a package or object reference at /usr/lib/perl5/site_perl/5.8.8/LEGO/NXT.pm line 1442.
Here is the sample program:
Code:
#!/usr/bin/env perl                                                                                                                                    

use LEGO::NXT;

         # Create a new Bluetooth/NXT object by connecting to                                                                                          
         # a specific bluetooth address and channel.                                                                                                   
my $nxt = LEGO::NXT->new( '00:67:22:AB:32:98', 1 );

$nxt->play_sound_file($NXT_NORET, 0,'! Attention.rso');

$res  = $nxt->get_battery_level($NXT_RET);

         # Turn on Motor 1 to full power.                                                                                                              
         $res = $nxt->set_output_state(
             $NXT_RET,
             $NXT_SENSOR1,
             100,
             $NXT_MOTORON|$NXT_REGULATED,
             $NXT_REGULATION_MODE_MOTOR_SPEED, 0,
             $NXT_MOTOR_RUN_STATE_RUNNING, 0,
    );
Here is the first offending function. Line 498 is the "my $ret = $this->_do_cmd(" line.

Code:
sub play_sound_file
{
  my ($this,$needsret,$repeat,$file) = @_;
  my $strlen = 1+length($file);
  my $ret    = $this->_do_cmd(
    pack("CCCZ[$strlen]",$needsret,$NXT_PLAY_SOUND_FILE,$repeat,$file),
    $needsret
  );

  return if $needsret==$NXT_NORET;

  $this->_parse_generic_ret($ret);
}
Here is the second. The offending line is "if ($comm->type eq 'usb')"


Code:
sub _do_cmd
{
  my ($this,$msg,$needsret) = @_;

  my $comm = $this->{comm};
  my $res;

  if ($comm->type eq 'usb')
  {
    $res = $comm->do_cmd( $msg, $needsret );

    #parsing functions expect prepended length bytes ala bluetooth                                                                                     
    if (defined $res )
    {
      $res = pack('v',length($res)).$res;
    }
  }

  if ($comm->type eq 'blue')
  {
    #bluetooth comm requires prepended length bytes                                                                                                    
    $res = $comm->do_cmd( pack( "v", length($msg) ) . $msg, $needsret );
  }

  $res;
}
My Perl is a little rusty, and I can't even remember what a pack is. Could somebody point me in the right direction?

[Edit: It is version 2.00 of the library... sample program was taken from the MAN pages installed by CPAN.]

Last edited by CoderMan; 12-21-2009 at 01:46 PM.
 
Old 12-21-2009, 02:17 PM   #2
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by CoderMan View Post
Here is the second. The offending line is "if ($comm->type eq 'usb')"

Weird- the example clearly shows passing a string as the first argument to the constructor, but looking at new() itself, it looks like it expects you to pass it an instance, i.e:

Code:
 $nxt = LEGO::NXT->new( new LEGO::NXT:BlueComm('xx:xx:xx:xx',0));                                                                        
  
or
                                                    
  $nxt = LEGO::NXT->new( new LEGO::NXT:USBComm() );

The error message (the second one is the fatal one) means that the program is trying to call a method on a scalar that is not an object, which the string you're passing it now isn't.

Give it another whirl using one of those examples.

.andy
 
1 members found this post helpful.
Old 12-21-2009, 06:55 PM   #3
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

Rep: Reputation: 43
Okay, I think I figured it out (seems to be working now). I think this is a case of documentation being out of sync with code. Part of the problem was what you described: The tutorial code did not properly describe how to create the object. Another part of the problem was that the tutorial code had mis-spelled some of the constants. Another part of the problem was that (strangely enough) it seems that it is also necessary in the test script to include (excuse me, "use") the LEGO::NXT::BlueComm and LEGO::NXT::Constants packages.

Here was the script that finally connected and did the job:

Code:
#!/usr/bin/env perl                                                                                                                                    

use LEGO::NXT;
use LEGO::NXT::BlueComm;
use LEGO::NXT::Constants;

$nxt = LEGO::NXT->new( new LEGO::NXT::BlueComm('00:67:22:AB:32:98',1));
$nxt->play_sound_file($NXT_NORET, 0,'! Attention.rso');

$res  = $nxt->get_battery_level($NXT_RET);

         # Turn on Motor 1 to full power.                                                                                                              
         $res = $nxt->set_output_state(
             $NXT_RET,
             $NXT_SENSOR_1,
             100,
             $NXT_MOTOR_ON|$NXT_REGULATED,
             $NXT_REGULATION_MODE_MOTOR_SPEED, 0,
             $NXT_MOTOR_RUN_STATE_RUNNING, 0,
    );
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl -- pack Hewson Programming 4 11-11-2009 11:46 PM
How to pack data into a 7z recursive + having autounpack EXE pack for windows? frenchn00b Linux - General 5 05-02-2009 01:59 PM
please help me with this perl code getting error "use of uninitialized value" haxpak Programming 10 03-01-2009 10:55 PM
LXer: Sun Announces the Latest in Open Source Tools with the Availability of NetBeans Visual Web Pack and NetBeans C/C++ Development Pack LXer Syndicated Linux News 1 12-31-2006 07:37 AM
perl pack function fvgestel Programming 2 10-27-2005 11:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration