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

first, some code:
for my $artistkey (0..$#is_guest) { # Don't do anything if the artist is not a guest next if ($is_guest[$artistkey] == 0); # IF the artist exists, we need only to update some information my $sth = $dbh->prepare("SELECT FROM artists (alias) WHERE alias = +?"); $sth->execute($artist_alias[$artistkey]); # The Part In Question: if ($sth->rows >= 1) { my $sth = $dbh->prepare("UPDATE artists SET email=? WHERE alias= +?"); $sth->execute($email[$artistkey], $artist_alias[$artistkey]); } # Now for Redundancy next if ($sth->rows >= 1); my $sth = $dbh->prepare("INSERT INTO artists (alias, email, is_gues +t) VALUES (?, ?, ?)"); $sth->execute($artist_alias[$artistkey], $email[$artistkey], $is_gu +est[$artistkey]); }

I'd like to avoid that little bit about redundancy. If at all possible, i'd like to consolidate the 'if' statement and the 'next if' into one, while still doing what's in the brackets. In an effort to reduce redundancy, I made the following changes. The following change gave me problems at compile time:

next if ($sth->rows >= 1) { my $sth = $dbh->prepare("UPDATE artists SET email=? WHERE alias= +?"); $sth->execute($email[$artistkey], $artist_alias[$artistkey]); }

Any ideas?

--Coplan

Replies are listed 'Best First'.
Re: Test break, but still doing
by danger (Priest) on Feb 21, 2001 at 11:18 UTC

    Forgive me if I've completely misunderstood your question, but why not just shove the next into the if-block?:

    if ($sth->rows >= 1) { my $sth = $dbh->prepare("UPDATE artists SET email=? WHERE alias=?" +); $sth->execute($email[$artistkey], $artist_alias[$artistkey]); next; }
      If I stuck next; at the end of the if loop, that breaks the if loop, would it not? It's the for loop that I want to break. --Coplan

        Did you try it? next doesn't apply to if-blocks, it applies to the enclosing loop-block (for or while, or a naked block which is a loop executed once) ... if-blocks are not loops. Similarly for redo and last.

Re: Test break, but still doing
by archon (Monk) on Feb 21, 2001 at 11:20 UTC
    the next function doesn't require a condition:
    if ($sth->rows >= 1) { my $sth = ...; $sth->execute(...); next; }

    See also the perlsyn manpage.

Re: Test break, but still doing
by magnus (Pilgrim) on Feb 21, 2001 at 13:53 UTC
    just as an addendum to what danger was saying, i name all my loop blocks; for example:

    WHILE_BLOCK_1: while (stuff) { more stuff; next WHILE_BLOCK_1; }

    this way i can always be very clear on what my  next, redo and  last statement are doing their stuff on: ... it also helps to point out where my code could be failing because i might be thinking that my  next statement applies to the wrong thing...

    this is especially helpful on long loops...

    magnus

      Personally, if I ever find myself wanting to label a loop block, I seriously look into refactoring.

      last, next, and redo, have a lot more in common with the hated goto then they do with structured programming.

      In some ways they are worse than goto because it takes more effort to find where they are transfering control to (you have to work your way out from nested blocks checking whether each is a loop or not and the editors I use can easily jump to "MyLabel:" but don't have a shortcut for even "jump to top of enclosing loop" (and then to the bottom in case the top is "do {" and we can't tell from that whether we are in a loop or not), much less "jump to top (or bottom) of enclosing Perl loop".

      I don't feel too guilty using last, next, and redo sparingly for fairly small loops (I've even experimented with out-denting such lines, but I'm not sure the effect works). I prefer to refactor into a subroutine so I can use return (or, for a complex loop I'll refactor parts into subroutines so that I'm left with a small loop that uses last, next, or redo).

              - tye (but my friends call me "Tye")