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

I am experiencing a problem with one of my newest projects. I have a form that has a bug in it, when the user adds a line (without spaces) larger than the table it prints in, the script crashes. Ie. I have a table width at 50 and some spammer comes in and types in a 51 character line, it defaces the page by breaking the tables and it actually goofs up the script. Is there a way a table can override the size so no matter what you put inside the table will not be forced to enlarge?

If not can someone show me a way to forcefully put in a hyphen (if those would make the table retain it's size) if someone posts a one-word message larger than a specified limit?

Replies are listed 'Best First'.
Re: Scalar size limits
by hardburn (Abbot) on Jun 26, 2003 at 16:21 UTC

    Is there a way a table can override the size so no matter what you put inside the table will not be forced to enlarge?

    No, but you can check your input instead:

    use CGI qw(:standard); my $text_field = param('text_field') || ''; $text_field =~ /\A( \w{0,50} # Only let through 50 word charcters, at most )/x; $text_field = $1;

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Scalar size limits
by artist (Parson) on Jun 26, 2003 at 16:20 UTC
    1. Show us your code to point you precisely.
    2. To put hyphen at 50th character for more than 50 characters:
    $_ = 'x' x 80; $_ .= 'y' x 100; s/((.){49})/$1-/g; print;
    3. To seperate the big word by a space
    s/((.){49})/$1 /g;
    substr($_,0,49) =~ s/$/-/ if length($_) > 50;

    Updated as per hardburn's comments

    artist

      To write a
      + in next line:

      substr($_,0,49) =~ s/$/<br><font color=red>+<\/font>/ if length($_) > 50;

      Now seriously: see Text::Wrap

        Ok, I have the same question for you as I do artist. You are s/// a $, where do I set my variable to this?
        substr($_,0,49) =~ s/$/<br><font color=red>+<\/font>/ if length($_) > +50;
        I still don't have the foggiest idea on how I would use that to do anything. How would I assign $description to that?

        Also, I checked out Text::Wrap and it's not going to work. "It formats a single paragraph at a time by breaking lines at word boundries", it sounds like this will only work if they person uses real words and spaces, my problem is someone can screw everything up by typing

        "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +zzzzzzzzzzzzzzzzzzzzzzz"
        and I need to find a way to break it up so it doesn't scroll the page (like it does here).

        update (broquaint): added <code> tags around the problem string

      What if the word isn't 51 characters long, but 101? Or 300? or 314159? Your code would break it into a word 50 chars long, then a hyphen, then the rest of the long string.

      When I first looked at this problem, I though inserting the hyphen would be the easiest solution. After I started working it out, I think its actually the hardest.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

        That's true...that could be a problem. I need it setup so no word can be larger than a specified limit and if it is, break it up into two words by a space or a hyphen. Whether it be 51 or a million, I have to get them broken up.

        Artist, I want to try your code but I don't quite understand how to implement it. Let's say I have $description, how would I use that with your example? Sorry, I'm just a little confused.