I found myself needing to insert a variable number of spaces in a cgi script - its not complex, I just found it useful...
spacer(3);
...
spacer(7);
sub spacer { my $times=shift; for ( 0 .. $times ) { print br; } } # end sub spacer

Replies are listed 'Best First'.
Re: spacer()
by Tanktalus (Canon) on Jun 16, 2005 at 15:15 UTC
    sub spacer { br() x shift }? Like the existing CGI funcs, I'd suggest going with a function that returns a string over directly printing it out. For example, using $q->print(spacer(4)) allows this to move to mod_perl better, supposedly. Or, in my case, I've used CGI to create static pages directly to disk, so being able to print it to an arbitrary file would be handy.

Re: spacer()
by ghenry (Vicar) on Jun 16, 2005 at 15:17 UTC

    shouldn't that be print '<br/>';

    xhtml compliant an' all.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
      ghenry,
      shouldn't that be...

      What makes you think it isn't? I could be wrong, but my guess is that br is a function call and not a bareword literall. That function call is likely coming from CGI, which is xhtml compliant (which btw I thought had a space <br />).

      Cheers - L~R

        Oops.

        Regarding br, for backwards-compatibility with browsers that are not XML-enabled, there must be a space before the /> (for example, <br />, not <br/>).

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!
Re: spacer()
by GrandFather (Saint) on Jun 17, 2005 at 03:21 UTC

    Looks like the sub should be called liner. I'd expect spacer to look more {" " x shift}ish.


    Perl is Huffman encoded by design.