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

Hi Monks,

I am running 5.10.0 (on Mac Os), and I get a weird behavior. I wan't able to find any explanation so far, and I am quite puzzled. Before I file a bug report, I would like to seek your immense wisdom.

The following works fine :

#!/usr/bin/perl default(); sub default { }
If I simply add the use line, as below, I get a syntax error :
#!/usr/bin/perl use 5.10.0; default(); sub default { }

syntax error at t.pl line 5, near "default("

Of course the problem doesn't appear if I use 'foo' instead of 'default', and disappears if I use the & sign: &default().

However, I find this puzzling, since in all cases I run the same version (5.10.0), and as far as I can tell "default" is not mentioned as a reserved keyword or anything (but may have overlooked some explanation in the quite voluminous doc). I hope this has not been asked before, but, as you will understand, SuperSearch with keywords sush as "default" and "use" is a bit tricky, and couldn't find anything there either.

Thanks in advance for your help.

Replies are listed 'Best First'.
Re: "use 5.10.0" creates weird syntax error on sub named "default"
by afoken (Chancellor) on Nov 27, 2010 at 09:11 UTC

    Switch statements (given / when) use the new default keyword.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      That must be it ! Thanks, Alexander.

      I thought of that feature active only with use Switch 'Perl6'; (http://perldoc.perl.org/5.10.0/Switch.html), but the keyword is obviously reserved all the time.

      Still not clear to me why writing explictly use 5.10.0 change the rules of the game, since I'm running 5.10.0 anyway, but allright...

      Many thanks again !

        I thought of that feature active only with use Switch 'Perl6';

        The experimental Switch module has nothing to do with the built-in switch statement Perl introduced in 5.10.

        but the keyword is obviously reserved all the time.

        Not so. That would have broken backwards compatibility. It's only reserved when use feature qw( switch ); is in use, which is done by use 5.10.0; for you (since you're obviously not worried about backwards compatibility).

Re: "use 5.10.0" creates weird syntax error on sub named "default"
by ikegami (Patriarch) on Nov 27, 2010 at 09:26 UTC

    use 5.10.0; implies the use of feature. Specifically, you are activating the switch feature which introduces the default keyword.

      Understood! Wonderful wisdom. Many thanks!
Re: "use 5.10.0" creates weird syntax error on sub named "default"
by AnomalousMonk (Archbishop) on Nov 28, 2010 at 00:51 UTC

    Just FYI and not really apropos of much,  ::default() works as well and avoids possible &-ish complications:

    >perl -wMstrict -le "use 5.10.0; ::default(); sub default { print 'works' } given ('x') { when ('y') { print 'y' } default { print 'default' } } " works default

      ::default() only works well if you've declared default() in main.