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

This is a question of style rather than success or failure...

I was recently producing HTML image galleries.

There were thumbnails which linked to pages featuring the full-size images. The full-size-image pages had "next" and "previous" links on them.

I found myself testing for "is this the first page?", "is this the last page?" and "is this neither the first or last page?" (in order to have "< previous | next >" for the middle sections) in what I felt was a rather clumsy way.

I was working from the lines of tab-separated text files, but it's essentially the same problem I've come across in a lot of other situations.

Is there a better way than

if($. != 1){ # we're not on the first page # "make 'previous' link" code } if($. != 1 && $. != $total_number_of_lines){ # we're not on the first or last page # add neat little " | " to separate # my 'next' and 'last' links } if($. != $total_number_of_lines){ # we're not on the last page # "make 'next' link" code }


($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Testing for one of three conditions
by eric256 (Parson) on Apr 26, 2004 at 23:55 UTC

    What about pushing them onto an array and then joining it. Join already has this logic for needing the | or not, and then you could put pages in the middle easily.

    my @links = (); push @links, $firstlink if $. != 1; # push @links, pagelink($page) foreach my $page (@pages); push @links, $lastlink if $. != $total_number_of_lines; print join("|",@links);

    Easy to ready and you can have some added functions (maybe, dunno how your code works.) (:


    ___________
    Eric Hodges

      Very neat, I didn't think of that.



      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
      =~y~b-v~a-z~s; print
      my @links = ( $firstlink, map { pagelink($_) } (@pages), $lastlink ); shift @links if $. == 1; pop @links if $. == $total_number_of_lines; print join('|', @links);

      --
      [ e d @ h a l l e y . c c ]

Re: Testing for one of three conditions
by matija (Priest) on Apr 26, 2004 at 23:19 UTC
    Yes, there is a better way:
    if($. != 1){ # we're not on the first page # "make 'previous' link" code if ($. != $total_number_of_lines){ # we're not on the first or last page # add neat little " | " to separate # my 'next' and 'last' links } } if ($. != $total_number_of_lines){ # we're not on the last page # "make 'next' link" code }
    It's not much, but it is an improvement, IMHO.
Re: Testing for one of three conditions
by davido (Cardinal) on Apr 26, 2004 at 23:30 UTC
    Three conditions may not be enough to warrant this, but it's also possible to construct the Perlish equivilant of a SWITCH statement. perlsyn goes into detail on loop and block control, which are the building blocks you use within Perl to hand-roll a switch construct. Or for the daring souls you can also use Switch; ...see Switch for details on that. Beware that Switch documentation states, "There are undoubtedly serious bugs lurking somewhere in code this funky." So in production code you're probably better off creating your own switch construct as described in perlsyn.

    Sometimes logical short circuit operators or trinary operators can make the code more readable (and sometimes less, so use good judgement). They're discussed in perlop, though I see you're already using '&&' as a logical operator. Just remember that it can be used to control program flow too.

    Update: Added quote from the Switch documentation as recommended by Enlil.


    Dave

Re: Testing for one of three conditions
by dragonchild (Archbishop) on Apr 27, 2004 at 01:16 UTC
    eric256's solution is probably the best for your actual situation. I'd go with his.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Testing for one of three conditions
by flyingmoose (Priest) on Apr 27, 2004 at 02:07 UTC
    Speaking of image galleries (in Perl), I use "igal" as a static generator and it works great.

    Available in the Debian archives as (igal), or otherwise here (stanford.edu)

    I'd like to see people rolling their own to contribute to some of the existing tools, like igal, so that more than one person can benefit... after all, open source is about sharing.... there are probably other good DB-based solutions to look for as well.