Hosen1989 has asked for the wisdom of the Perl Monks concerning the following question:

Dear ALL,

Here I have faced feature 'I think' I do not understand.

I was going to use constant module, and the three line below demonstrate it:

use constant WEEKDAYS => qw(Sunday Monday Tuesday Wednesday Thursday F +riday Saturday); print "".(WEEKDAYS)[1]; #case 1 print (WEEKDAYS)[1]; #case 2

@case 1: it work just fine and print the day of week!!!

@case 2: it crash with next error:

syntax error at time.pl line 3, near ")[" Execution of time.pl aborted due to compilation errors.

So can you help me here.

BR

Hosen

Replies are listed 'Best First'.
Re: use constant moudle
by Anonymous Monk on Feb 24, 2016 at 10:30 UTC

    In the second case, print is being interpreted as a function, i.e. print(WEEKDAYS)[1];, where the [1] doesn't work. Normally this gets a warning, but you've run into a case where it doesn't:

    $ perl -we 'print (3+4),5;' print (...) interpreted as function at -e line 1. Useless use of a constant (5) in void context at -e line 1. 7 $ perl -we 'print (WEEKDAYS)[1];' print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. $ perl -we 'print (WEEKDAYS)[1];' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

    Two workarounds in addition to your "case #1":

    $ perl -we 'print +(3+4),5;' # disambiguate 75 $ perl -we 'print((3+4),5);' # explicit parens 75

    Tip: B::Deparse comes in handy for debugging here:

    $ perl -MO=Deparse,-p -e 'print (3+4),5;' (print(7), '???');
      "Normally this gets a warning, but you've run into a case where it doesn't"

      Update: You only get the warning if you ask for it and put exactly one space between the print and the left parenthesis. Using zero or more than one space, and no warning is emitted.

      $ perl -we 'use constant WD => qw{Sun Mon}; print(WD)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. $ perl -we 'use constant WD => qw{Sun Mon}; print (WD)[1]' print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. $ perl -we 'use constant WD => qw{Sun Mon}; print (WD)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. $ perl -we 'use constant WD => qw{Sun Mon}; print (WD)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

      I tried up to five spaces: same result. Very weird!

      Original text:

      If you don't ask for warnings, you don't get warnings:

      $ perl -e 'use constant WD => qw{Sun Mon}; print (WD)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

      If you ask for warnings, you get warnings:

      $ perl -we 'use constant WD => qw{Sun Mon}; print (WD)[1]' print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

      There's nothing special something very strange about this case.

      — Ken

        Sorry but you're wrong, look closely:

        $ perl -we 'use constant WD => qw{Sun Mon}; print (WD)[1]' print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. $ perl -we 'use constant WD => qw{Sun Mon}; print (WD)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.