Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

xsawyerx (8978)

  (email not shown publicly)

Journal of xsawyerx (8978)

Wednesday June 10, 2009
04:30 AM


[ #39110 ]

We can't stop laughing at this at $work. Apparently PHP decided to add goto control structure!

So you say "okay, on the plus side, at least they remembered a really basic control structure" but then you say "who the hell uses goto in anything that _isn't_ assembly or _perhaps_ condensed C [that isn't a really oldschool person]?!"

Some people mentioned a few things good with GOTOs, and here is my 2 cents on them:

  • Useful for error handling: did any of you heard of structured code? perhaps heard of logging?
  • Useful for writing parsers: I would use some better coding conventions for this like function referencing, dispatch tables and others to write a parser, but not goto. writing parsers with goto is just abusing an arcane control structure for something advanced ones are better used to maintain readability and workflow. besides, who the hell writes parsers in PHP?
  • Can be faster than other control structures: are you freaking kidding me? you're using a DYNAMIC LANGUAGE. you have performance hit by even echoing a hello world, but you're worried about a goto being a bit faster than a nested if()?

Remember when I started by asking "who the hell uses goto in anything that _isn't_ assembly or _perhaps_ condensed C?!"? After asking one of our PHP developers that question, I spit out another question without noticing: "Do they [the PHP developers themselves] have a lot of use for goto in PHP code they write?" and right after I thought "Do they write a lot of goto in C?"


$ grep goto -r php-5.2.9 | wc -l

But then I wondered...

$ grep goto -r perl-5.10.0 | wc -l

Here's some more info:

(php has also extensions and a ton-load of documentation)

$ find php-5.2.9 -type f -iname '*.c' -or -iname '*.cpp' -or -iname '*.h' -exec cat {} \; | wc -l
$ find php-5.2.9/win32/ php-5.2.9/Zend/ php-5.2.9/main/ -type f -iname '*.c' -or -iname '*.cpp' -or -iname '*.h' -exec cat {} \; | wc -l
$ find perl-5.10.0 -type f -iname '*.c' -or -iname '*.cpp' -or -iname '*.h' -exec cat {} \; | wc -l

note: SLOC does not equal productivity or quality

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Sadly goto is a useful construction in C, because the flow control primitives are, well, far more primitive than Perl. For example, you can only break out of the innermost loop.

    Perl's goto has something that isn't offered by C or PHP - goto &NAME []. That form is really the only form that anyone should be using in Perl code, and then only when they know what they're doing and why. I don't know PHP, but either it has flow control structures as powerful as Perl's, in which case goto is not needed, or the f

    • Thanks for providing more accurate (or.. much more accurate) finds

      I also liked the explanation you gave on the use of gotos. I had a discussion about that with a colleague (who is a Python programmer) and we got around to labels and I tried to explain that Perl has label-aware loop commands. That you can last to a label. In PHP, you can continue an amount of loops outside (continue 3 would exit 3 loops above) while apparently in Python you can't do either, only break and loop.

      I guess what I'm saying is that

    • For those who don't use ack, this is exactly the sort of thing ack was designed to do easily.

      find perl-5.10.0 -name ext -prune -o '(' -type f -iname '*.c' -or -iname '*.cpp' -or -iname '*.h' ')' -exec cat {} \; | egrep '\bgoto\b' | wc -l

      is just

      ack --cc --cpp -w goto perl-5.10.0 | wc -l

      --cc means "C source and headers only", --cpp is "C++ source and headers only", -w means "word only".

      Just install CPAN package App::Ack.



  • in the 5.8.9-rc1 that i had handy from a regression test

    of 1270 C-ish file hits using Andy's ack invocation

    nearly one third are in one file alone
      403 toke.c

    most of which and one quarter of the total being exactly
      343 toke.c: goto unknown;

    the long tail continues over 65 files, but a mere 5 files cover 50% in 5 a tie
      403 toke.c
        95 regexec.c
        54 pp_sys.c
        53 sv.c
        39 opmini.c
        39 op.c # tied for 5th
    (subtotal 683 53%)

    # I had a sig when sigs were cool
    use Sig;