Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Perl Code Changes Behavior if Two Subroutine definitions are swapped

by Athanasius (Archbishop)
on Jul 27, 2016 at 16:05 UTC ( [id://1168656]=note: print w/replies, xml ) Need Help??


in reply to Perl Code Changes Behavior if Two Subroutine definitions are swapped

Hello rkabhi,

As haukex says, the Switch module is deprecated and therefore best avoided. But the problem here is not with the module, but rather with the way it is being used.

First, by giving the switch statement an empty argument, you are matching against the empty string:

use strict; use warnings; use Switch; for ('abc', 'def', 'ghi') { switch () { case /bc/ { print "$_: matches 'bc'\n"; } case /de/ { print "$_: matches 'de'\n"; } case '' { print "EMPTY STRING\n"; } else { print "$_: no match found\n"; } } }

Output:

1:51 >perl 1675_SoPW.pl EMPTY STRING EMPTY STRING EMPTY STRING 1:51 >

You need to specify the string to be matched against: in this case, switch ($_) {.

Second, the next statements in your case clauses do not force another iteration of the enclosing while loop, as you expect. That’s because the Switch module uses next to implement fall-through, as explained in the “Allowing fall-through” section of the Switch documentation. You could avoid this by specifying an explicit target for the next statements; for example (untested):

MAIN_LOOP: while (<READ_NETLIST>) { chomp; s/^\s+//; # Remove leading blanks (if any) in the line # ignore line if it contains comments or initializing words for sp +ectre switch ($_) { case /^[*\/]/i { next MAIN_LOOP; } case /simulator\s+lang/i { next MAIN_LOOP; } case /^include/i { next MAIN_LOOP; } } ... }

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Perl Code Changes Behavior if Two Subroutine definitions are swapped
by rkabhi (Acolyte) on Jul 28, 2016 at 05:36 UTC
    Hi Athanasius,

    Thanks for your response.

    Regarding the empty string. I tried running your example code using perl 5.10.1 and got following output:

    OUTPUT WITH EXAMPLE CODE:
    abc: matches 'bc' def: matches 'de' ghi: no match found

    So, somehow we get different outputs. Not sure why. Since in my case it tries to match with $_ when nothing is specified in switch(), I could not identify any issue with such use of switch and therefore I continued using it.

    Thanks for mentioning things to take care as best practice about using empty arguments and next statements. I will try your suggestions and get back if the main issue persists. For now I am not going to use switch as it is not necessary for my code.

    Best Regards,
    Abhishek
      I tried running your example code using perl 5.10.1 and got following output...

      Interesting. Experimenting with the Strawberry Perl versions I have on my system, it appears the change occurred between Perl 5.12.3 and 5.14.4. Running with -MO=Deparse reveals only one difference:

      &Switch::switch(); # v5.12.3 vs. &Switch::switch(()); # v5.14.4

      And calling switch explicitly with an empty list — switch(()) — under 5.12.3 gives the same result as calling switch() under 5.14.4.

      I’ve skimmed through the deltas, but so far haven’t found a plausible explanation. Possibly related to the change in handling the (;$) prototype,1 since that is the prototype of sub switch in the Switch module, but I don’t see how that would account for the observed behaviour. :-/

      1See “Change in parsing of certain prototypes” in perl5140delta.

      Cheers,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Isnt switch noncore? I doubt perl version plays any part, aside from deparse version

      Welcome, rkabhi.

      Is there any particular reason why you are not running the latest version of Perl?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found