David Tonhofer
David Tonhofer

Reputation: 15338

Perl's "Switch" grabs a 'case' string inside a HERE document and gets confused?

I have a little Perl code, with a HERE document. Inside the HERE document's text, there is embedded the keyword case. This seems to unnerve the Switch statement mightily. Am I crazy?

#!/usr/bin/perl

use strict;
use warnings;
use utf8;  # Meaning "This lexical scope (i.e. file) contains utf8"

use Switch;

sub printUsage {
   print STDERR << "HERE";

                       +
  --rollback           | - In case of "--reallydo", perform a ROLLBACK instead of a COMMIT at
                       |   transaction end.
                       + 
HERE
}

Running this in Perl 5.16 gives:

Bad case statement (invalid case value?) near avo2.pl line 13

i.e. In case of isn't appreciated "here", literally. Some bug? Should I raise this at the Perl bug tracker?

Upvotes: 1

Views: 329

Answers (1)

JGNI
JGNI

Reputation: 4013

Don't use Switch if you can possibly avoid it, it's a source filter and you have found one of the bugs lurking in it's depths. given()/when() would be better, but that has problems and is now marked as experimental. If you want the equivalent of a case statement try

for ($test_this) {
    if ( ! /\D/ ) {
        say 'is numbers';
        last;
    }
    if ( $_ eq 'exit' ) {
        say 'exit found';
        last;
    }
    if (/^\p{Lu}/) {
        say 'Upper case letter';
        last;
    }
    # Default option
    say 'Default';
    last;
}

Upvotes: 6

Related Questions