2

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-

3
  • You have while ( $arraysize > 0 ) { ... Don't you need $arraysize = $#{ $arrSref }; above the second to last }, so the loop terminates, or just while ( $#{ $arrSref } ) { ... and omit $arraysize = $#{ $arrSref }; altogether? Commented Dec 8, 2013 at 6:54
  • Which version of perl your using?Give when statement is available only in perl 5.18.0... Commented Dec 8, 2013 at 8:00
  • can you post here the output of perl -v Commented Dec 8, 2013 at 8:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

or do use 5.10.1; (or higher) which will automatically enable features
Thx, but Perl v5.16.2 says "Use of Switch is deprecated." So I chose the given-when syntax. -DrP-
@DrParzival, 1) 5.16.2 does not say Switch is deprecated. Perl doesn't know anything about Switch. 2) use feature 'switch' is not use Switch;. As John Saxton said, the former provides given/when. Note that given/when is marked as experimental (and 5.18+ will warn you about this).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.