Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

what does goto ""; mean?

by ambrus (Abbot)
on Dec 11, 2003 at 15:12 UTC ( [id://314046]=perlquestion: print w/replies, xml ) Need Help??

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

It seems that a computed goto to an empty string is a strange operation in perl.

For example perl -we 'warn 1;goto "";warn 2;' prints only one warning.

Also note that perl does not allow an empty label (a single colon before a command), but at the beginning of the whole -e program, it does allow a colon, but that does not seem to create a label: perl -we ':warn 1;goto "";warn 2;' is the same as the above; but perl -we 'eval q{:warn 1;goto "";warn 2;}; warn 3;' prints only the third warning, the same as perl -we 'eval q{:warn 1;warn 2;}; warn 3;'

If you can explain these beheaviours, answer me please.

Update: it seems that perl still executes the END blocks after it exits via goto "".

Replies are listed 'Best First'.
Re: what does goto ""; mean?
by Abigail-II (Bishop) on Dec 11, 2003 at 15:21 UTC
    The behaviour of the colon at the start of a program is documented. From 'man perlrun':
    To start up sh rather than csh, some systems may have to replace the #! line with a line contain- ing just a colon, which will be politely ignored by Perl.

    Abigail

Re: what does goto ""; mean?
by Abigail-II (Bishop) on Dec 11, 2003 at 15:31 UTC
    It seems that goto ""; and goto "\x00"; have the same behaviour. Peeking in the sources of Perl, one sees several *label tests (a test which is false if label points to a NUL character). This might explain why Perl isn't complaining (although the source for goto is long and most of it I don't understand), but it doesn't explain why the warn 2 isn't executed.

    Abigail

      Just to add on top of what Abigail-II mentioned (why it is worth to mention "\x00"?).

      If Perl, "" and "\x00" are not the same: (for people from c background, this is different in Perl and c.)

      use strict; use warnings; if ("\x00" eq "") { print "they are the same"; } else { print "they are not the same"; }

      But they are the same in c/c++:

      #include <iostream.h> int main() { if (!strcmp("", "\x00")) { cout << "they are the same" << endl; } else { cout << "they are different" << endl; } }
        Careful. They're treated the same in C, but they're not. The difference between "" and "\x00" is that the former is one byte long, whose value is null, while the latter is two bytes long - two nulls. But since everything in C treats a null as end-of-string, the difference is not apparent anywhere.

        Makeshifts last the longest.

      Thanks for explaining the empty label.

      About goto "", I've just noticed that $u= ""; goto $u; does not have this wierd beheaviour (it dies with the correct message), so maybe goto ""; isn't a computed goto at all. Notice that a computed last is illegal, but last "label"; or even last ("label".""); does work, as ("label"."") is a constant expression.

Re: what does goto ""; mean?
by melora (Scribe) on Dec 11, 2003 at 19:59 UTC
    I hate to seem obtuse, but why would you want to do a computed goto to an empty string? (NOTE: this is NOT sarcasm, I'm just curious.)
        Oh, okay. I thought it was Something Found on the way to The Other Thing. melora

      Well, in GNU sed, for example, you can jump to an empty label:

      For example, echo bungee | sed ':;s/\(.\)\(.*\)e/\2\1/;t;' prints ngub.

      UPDATE: I didn't give an answer to your question, so here it is. I saw that you can create an empty label (you can't), and wondered how to jump there. A simple goto; is not enough. So I tried goto "";.

Re: what does goto ""; mean?
by mr_mischief (Monsignor) on Dec 16, 2003 at 23:14 UTC
    "", not being a label, seems to just terminate the program.

    Even with warnings and strictures in place, the program below ends at the goto and returns a termination code of zero (normal termination).

    #!/usr/bin/perl -w use strict; $|++; print "foo!\n"; goto ""; print "Bar!\n"; print "baz!\n";

    The debugger doesn't seem to show anything strange, either. I haven't looked at the goto() code, but it seems that it's jumping to the end of the program when none of the labels match.

    What's curious is that there's a message about not finding the label printed if it's an ordinary label that's just not found. Any quoting construct which works out to null seems to just become an exit, though. For example, "\x0" or "\0" works as if there's nothing in the quotes. "\x0.*" works the same. Curiously, "\c" gives me an out of memory error.



    Christopher E. Stith

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://314046]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found