Question

Two pieces of ASCII text (one L = long and one S = short) were read into @arrayOne and @arrayTwo, respectively, for comparison. The following subroutine &analyse gets two array references from the smart.pl code, but throws an error when checked via perl -c smart.pl. Unfortunately I can't figure out why:

68  sub analyse {
69      my $arraysize ; my $arrLref ; my $arrSref ; my $item_L ; my $item_S ; my $value ;
70
71      $arrSref = shift ; $arrLref = shift ;
72      $item_S = shift @{ $arrSref } ;
73      $item_L = shift @{ $arrLref } ;
74
75      $arraysize = $#{ $arrSref } ;
76      while ( $arraysize > 0 ) {
77          $value = ( $item_S cmp $item_L ) ;
78          given ( $value ) {
79              when ( -1 ) {
80                  push ( @mergedArray , $item_S ) ;
81                  $item_S = shift @{ $arrSref }
82              }
83              when ( 0 ) {
84                  push ( @mergedArray , $item_L ) ;
85                  $item_S = shift @{ $arrSref } ;
86                  $item_L = shift @{ $arrLref }
87              }
88              when ( 1 ) {
89                  push ( @mergedArray , $item_L ) ;
90                  $item_L = shift @{ $arrLref }
91              }
92              default { &die }
93          }
94      }
95  }

Compilation is aborted with the following statements:

    $ perl -c smart.pl 
    syntax error at smart.pl line 78, near ") {"
    syntax error at smart.pl line 83, near ") {"
    syntax error at smart.pl line 88, near ") {"
    Global symbol "$item_L" requires explicit package name at smart.pl line 89.
    Global symbol "$item_L" requires explicit package name at smart.pl line 90.
    Global symbol "$arrLref" requires explicit package name at smart.pl line 90.
    syntax error at smart.pl line 91, near "}"
    smart.pl had compilation errors.

Maybe someone else has a clue? Thx in advance –DrP-

Was it helpful?

Solution

According to the Perl documentation, in order to use given and when, two conditions need to be met:

  1. You need to use feature "switch";
  2. You need Perl 5.10.1+

That should explain what you're seeing on lines 78, 83, and 88.

Regarding the warnings you're seeing on lines 89 and 90, these are tied to the use of use strict;, and an excellent explanation of these warnings can be found here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top