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

Monks,

I've encountered the following weirdness:

#!/usr/bin/perl #use Switch; print while <DATA>; __DATA__ some data here

If I uncomment the "use Switch" line, it prints nothing. Bug, or am I missing something? perl v5.12.2.

Replies are listed 'Best First'.
Re: Switch and __DATA__
by Perlbotics (Archbishop) on Jun 16, 2011 at 13:16 UTC

      Aha, so that's how you do it nowadays! Guess I don't use switch statements all too often. :) Thank you.

Re: Switch and __DATA__
by MidLifeXis (Monsignor) on Jun 16, 2011 at 12:53 UTC

    Confirmed on the CB (thanks Corion) - this is a problem with a source filter. Switch does some funky things with the source (via regex, I believe), and sometimes it fails.

    --MidLifeXis

      Thank you, I'll revert to a hand crafted switch construct then.

Re: Switch and __DATA__
by Tux (Canon) on Jun 16, 2011 at 13:36 UTC

    Pne more (very good) reason to always use strict and warnings, as that would have clearly pointed you to who's fault it is:

    $ perl5.12.2 -we'use Switch' Switch will be removed from the Perl core distribution in the next maj +or release. Please install it from CPAN. It is being used at -e, line + 1. $

    Switch is from 2009, and it has been deprecated in 5.12 and has been removed from 5.14 and up:

    $ perl5.14.0 -we'use Switch' Can't locate Switch.pm in @INC (@INC contains: /media/Tux/perls/lib/si +te_perl/5.14.0/i686-linux-64int /media/Tux/perls/lib/site_perl/5.14.0 + /media/Tux/perls/lib/5.14.0/i686-linux-64int /media/Tux/perls/lib/5. +14.0 .) at -e line 1. BEGIN failed--compilation aborted at -e line 1. $

    As already noted, don't use Switch. With recent perl versions, use given/when or for/when.

    FWIW Switch makes your whole script barf on unpredictable points when you are using defined-or // which is way more useful than Switch.


    Enjoy, Have FUN! H.Merijn

      My original script has use warnings and strict, but perl does not produce any warnings with "use Switch". I'm on Windows (Active State Perl) atm, I might add.

        I just tried with Strawberry, and it also does not issue a warning. It must be a Windows problem. I'll ping their maintainers.

        I hope my other information was useful for you though.


        Enjoy, Have FUN! H.Merijn