txb142
Posts: 11
Joined: Mon May 06, 2013 2:19 am

Perl and GPIO (?) issue/bug

Tue May 21, 2013 6:19 pm

I'm having an issue with concatenation and perl. Here is the beginning of my code

Code: Select all

#perl 5, version 14, subversion 2 (v5.14.2)

use HiPi::Interrupt;
use HiPi::Device::GPIO;
use HiPi::BCM2835;
use HiPi::Utils;
use HiPi::Constant qw( :raspberry );
use feature ':5.14';

#-------------------------------------------------------------

package MyInterruptHandler;

#-------------------------------------------------------------
# To handle interrupts we create a class deriving from
# HiPi::Interrupt::Handler and override the on_action_....
# methods.
#-------------------------------------------------------------

#use strict;
#use warnings;
use parent qw( HiPi::Interrupt::Handler );
use HiPi::Constant qw( :raspberry );
$| =1;
if (scalar @ARGV < 1) {print "Usage: <Command list>"; exit;}

  $bcm = HiPi::BCM2835->new();
  @leds = qw(0 1 4 7 8 9 10 11);
  foreach $leds (@leds) # setup then clear leds
   {$bcm->gpio_fsel( $leds, RPI_PINMODE_OUTP); $bcm->gpio_clr($leds)}

   #$state =0;

  #@switches = qw(15 17 18 21 22 23 24 25);
  #               0 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9 2 1 2 3 4 5 
  @switches = qw (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 3 4 5 6 7);

  open IN, "<", $ARGV[0] or die "!";  @lines = <IN>;  close IN;

  foreach $lines (@lines) {chomp $lines; if ($lines !~ /^[\#]/) {push @lines2, $lines;}}
  
  $current_prefix = ""; $current_counter = 0; %commands =();
  $current_prefix ="67"; $current_counter =0;  $current_counter++; $temp = $current_prefix.$current_counter; print $temp."\n"; 
  foreach $lines2 (@lines2)
   {#
     if ($lines2 =~ /^\+/){$lines2 =~ s/^.//; $current_prefix = $lines2; $current_counter =0;}
	  else
	   { 
	     $temp = $current_prefix.$current_counter; 
		 print $temp."\n"; $commands{$temp2}= $lines2; $current_counter++;		 
	   }
	 # print $current_prefix.$current_counter."\n"; 
   }

	print $commands{"672"};
	close IN;
The line

Code: Select all

$current_prefix ="67"; $current_counter =0;  $current_counter++; $temp = $current_prefix.$current_counter; print $temp."\n";
Prints the expected value "671". But the lines

Code: Select all

	     $temp = $current_prefix.$current_counter; 
		 print $temp."\n"; $commands{$temp2}= $lines2; $current_counter++;
prints the following:

Code: Select all

07
17
27
37
47
57
67
77
For some reason inside the foreach loop causes the counter to overwrite the first character in the prefix variable. I tried variations to get around this issue but none of them work. I even stuck in "use strict" and defined all variables but still didn't work properly.

Any ideas?

User avatar
jojopi
Posts: 3115
Joined: Tue Oct 11, 2011 8:38 pm

Re: Perl and GPIO (?) issue/bug

Tue May 21, 2013 10:04 pm

I suspect the input file you are processing has network line endings, "67\r\n". chomp removes only the "\n", so you are printing "67\r0\n", which looks like "07\n". Try after the chomp:

Code: Select all

$lines =~ s/\r$//;

txb142
Posts: 11
Joined: Mon May 06, 2013 2:19 am

Re: Perl and GPIO (?) issue/bug

Wed May 22, 2013 4:02 pm

jojopi wrote:I suspect the input file you are processing has network line endings, "67\r\n". chomp removes only the "\n", so you are printing "67\r0\n", which looks like "07\n". Try after the chomp:

Code: Select all

$lines =~ s/\r$//;
Oh thanks! I thought I tried that before but guess I didn't have that exact syntax. Usually I code in windows and I don't remember having issues with it, so I'll keep it in mind.

scrapheap
Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: Perl and GPIO (?) issue/bug

Wed Jun 05, 2013 12:18 pm

Instead of chomp I tend strip off carriage returns and new lines with

Code: Select all

$lines=~s/[\r\n]+$//;

Return to “Other programming languages”