in reply to Pseudo-switch question

Well, the example given in perlfaq7 (perldoc -q switch) is a bit easier for me to read:

SWITCH: for (ref $whatchamacallit) { /^$/ && die "not a reference"; /SCALAR/ && do { print_scalar($$ref); last SWITCH; }; /ARRAY/ && do { print_array(@$ref); last SWITCH; }; /HASH/ && do { print_hash(%$ref); last SWITCH; }; /CODE/ && do { warn "can't print function ref" +; last SWITCH; }; # DEFAULT warn "User defined type skipped"; }

Seems pretty much the same as what you're doing, as far as functionality, just a little prettier...

Update: Added more thoughts:

I can't see any performance hit. All you're really doing is assigning your test to $_ so that you can make testing it prettier.

--
3dan

Replies are listed 'Best First'.
Re^2: Pseudo-switch question (yuck)
by tye (Sage) on Jul 23, 2003 at 07:57 UTC

    For what's it's worth, I prefer either of the versions in the original node to this by a factor of about 1000.

    I'd love to see the look on your replacement's face when they see such code.

    You prefer having to come up with a label and then repeating "last LABEL" all over the place to a simple elsif ?? My mind boggles. Really.

    And that indentation scheme is atrocious! (Okay, now I sound like that guy in Airplane!... Johnny, talking to the captain's wife, ya know?)

    But, in all sincerity, I find it hard to express how distasteful I find that code. I boggled at it when it showed up in the documentation (I think it predates 'pod'). It has just gotten uglier over the years.

    If there were some powerful, compelling advantage to it, then I might have a small amount of appreciation for it. I realize that it allows you to "fall through" somewhat like a switch/case, but it doesn't even emulate that very well.

    [ And even beyond all of the awful style problems, the code isn't a very good example. The regexen are unanchored (and a nice 'eq' makes more sense anyway). ref should be replaced with isa or something from Scalar::Util. And these just point out the limitations with this syntax: Try to fix the code and this syntax will fight you. And then passing the hash to the print function as a big list? ... ]

                    - tye