Click here to get back home

strategys other than subroutine and OO?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
strategys other than subroutine and OO? Ela 03-20-2008
Get Chitika Premium
Posted by Ben Morrow on March 20, 2008, 3:58 pm
Please log in for more thread options

>
> Who the hell uses goto today???

Depends on the language. While I have never used goto in Perl, it is
common in C to see a function structured like the one below: see, for
instance, much of the perl source. This is a consequence of having to
de-allocate everything manually in C. Of course, one has to be careful:
it is all to easy for such a function to degenerate into an
incomprehensible mess.

Ben

int
do_something(...)
{
Foo *foo;
Bar *bar;
int rv;

foo = allocate_Foo();
if (!foo) {
rv = ENOFOO;
goto out;
}

bar = allocate_Bar();
if (!bar) {
rv = ENOBAR;
goto out_foo;
}

rv = try_something(foo, bar);
if (rv < 0)
goto out_bar;

do_something_else();

out_bar:
free_Bar(bar);

out_foo:
free_Foo(foo);

out:
return rv;
}

Posted by szr on March 21, 2008, 2:13 am
Please log in for more thread options
Ben Morrow wrote:
>>
>> Who the hell uses goto today???
>
> Depends on the language. While I have never used goto in Perl

Just a sort of a semi-tangent question, isn't using next and last with
labels (to, say, break out of a parent loop from an inner loop in a set
of nested loops) implicitly using `goto` ? If you think about it, the
syntax is the same:

Loop1: while (...) {
Loop2: until (...) {
if (some_condition) { last Loop1; }
elsif (some_other_cond_a) { next Loop1; }
elsif (some_other_cond_b) { redo Loop1; }
}
}

I'm sure most everyone has used this sort of construct at some point or
another to break from an outer loop from within an inner one.

Correct me if I'm wrong, but are not last/next/redo with labels basic
`goto` statements that enact a last, next, or redo on the loop
associated with the label?

Does this constitute a necessary evil, or more rather, demonstrates an
instance where `goto` (in some form) has a place?

--
szr



Posted by Joost Diepenmaat on March 21, 2008, 5:06 am
Please log in for more thread options
> Correct me if I'm wrong, but are not last/next/redo with labels basic
> `goto` statements that enact a last, next, or redo on the loop
> associated with the label?

Not exactly. Since you can only use them to go up the call stack another
way to think of them is as special exceptions with built-in handlers.

For real fun, see common lisp's condition/restart system.

In any case everything that alters the program flow can ultimately be
seen as a (conditional) goto. Even such basic operators as && and if()

> Does this constitute a necessary evil, or more rather, demonstrates an
> instance where `goto` (in some form) has a place?

It just demonstrates a useful pattern for program flow, which IMO is
better handled by having explicit operators with predefined behaviour
than by goto. The main problem with goto for me is that it's so
unrestricted it *can* lead to code that's very hard to understand and
bugs that are hard to analyze, not that it's inherently evil or anything
like that.

Perl has enough useful flow control operations and other features that
it's rare to run into situations where goto() is the cleanest / clearest
/ obvious choice.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by szr on March 21, 2008, 11:36 am
Please log in for more thread options
Joost Diepenmaat wrote:
>> Correct me if I'm wrong, but are not last/next/redo with labels basic
>> `goto` statements that enact a last, next, or redo on the loop
>> associated with the label?
>
> Not exactly. Since you can only use them to go up the call stack
> another way to think of them is as special exceptions with built-in
> handlers.

Yeah but they seem to use the goto-label system in a way still (when
using loops with labels.. see two paragraphs down.)

> For real fun, see common lisp's condition/restart system.

Will do.

> In any case everything that alters the program flow can ultimately be
> seen as a (conditional) goto. Even such basic operators as && and if()

True. Though my point was a somewhat about the fact that they use the
same labels that goto uses for it's jump destination so next/last/redo +
label seemed like a goto with an additional instruction that acts on the
loop belonging to that label's context.


>> Does this constitute a necessary evil, or more rather, demonstrates
>> an instance where `goto` (in some form) has a place?
>
> It just demonstrates a useful pattern for program flow, which IMO is
> better handled by having explicit operators with predefined behaviour
> than by goto.

Agreed.

> The main problem with goto for me is that it's so unrestricted it
> *can*
> lead to code that's very hard to understand and bugs that are hard to
> analyze, not that it's inherently evil or anything like that.

True. I've just always seen it something that does have it's place like
anything else, and must be handled with much are when used (though I do
not really explicitly use goto in my programs, just never found the
need, to be perfecly honest :-)

> Perl has enough useful flow control operations and other features that
> it's rare to run into situations where goto() is the cleanest /
> clearest / obvious choice.

And this is probably /why/ I've never really needed to use goto in Perl
(though I also never needed to use it and c/c++ and the likes.)

--
szr



Posted by Ben Morrow on March 21, 2008, 8:38 pm
Please log in for more thread options

> Ben Morrow wrote:
> >>
> >> Who the hell uses goto today???
> >
> > Depends on the language. While I have never used goto in Perl
>
> Just a sort of a semi-tangent question, isn't using next and last with
> labels (to, say, break out of a parent loop from an inner loop in a set
> of nested loops) implicitly using `goto` ?

Of course. The principle difference, for me, is that next/last/redo
always jump to the top (or right out) of a block, so blocks are always
executed straight through up to the point where you jump out. Even
though it's logically the same (and operationally very slightly slower),
I would prefer

begin_stuff();

FOO: {
do_stuff();
redo FOO if condition();
}

end_stuff();

to

begin_stuff();

FOO:
do_stuff();
goto FOO if condition();

end_stuff();

just because the potentially-repeated section gets its own block.

Ben


Similar ThreadsPosted
help me pass argument to the subroutine and then return the value from that subroutine to another. October 14, 2006, 1:35 am
A subroutine for gcd July 17, 2006, 9:38 am
subroutine July 20, 2006, 5:36 pm
"Undefined subroutine" November 21, 2004, 8:48 pm
How to tell if a subroutine arg is a constant February 24, 2005, 9:13 am
Subroutine Function March 3, 2005, 5:14 pm
subroutine explanation April 22, 2005, 8:40 am
problam in subroutine? December 5, 2005, 1:40 pm
calling subroutine January 3, 2006, 6:35 pm
subroutine doesn't seem to run after calling it. May 9, 2006, 4:04 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap