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

Hello,
How can I condense this code? Due to me being a newb, this is the only way I could think of.
sub x1_3o { open my $in, '<', $thefile or die "Can't read old file: $!"; open my $out, '>', "$thefile.new" or die "Can't write new file: $!"; while (<$in>) { if ($_ =~ /replaceme/) { print $out $_; print $out " <FORM METHOD = \"post\" ACTION = \"$script_url\" name=\"login\"> <table border=0 cellpadding=4 cellspacing=4 width=470> <tr> <td> <table><tr><td><br>\n"; } next if /replaceme/../replaceme/; print $out $_; } close $out; close $in;

Replies are listed 'Best First'.
Re: Compress me
by moritz (Cardinal) on Nov 07, 2010 at 17:14 UTC
    How can I condense this code?

    Most of all I'd recommend a consistent whitespace usage (both indention and newlines); apart from that, your code looks mostly reasonable.

    Regarding the program length, please read The path to mastery

    Perl 6 - links to (nearly) everything that is Perl 6.
      Thanks for the link to The path to mastery That was good reading
Re: Compress me
by kcott (Archbishop) on Nov 07, 2010 at 17:37 UTC

    You could write the if statement like:

    if ($_ =~ /replaceme/) { print $out qq{$_ <FORM METHOD="post" ACTION="$script_url" name="login"> <table ...> ... }; }

    Using qq{} (see perlop - Quote and Quote-like Operators) you can dispense with escaping the embedded quotes, you won't need the terminal newline and there's only one print statement. You could also use a here document which is described in the same documentation.

    -- Ken

      Using <<EOF (aka here-doc) for the form is another option.
      Hi Ken,
      Your code change worked great . I did notice something odd though .
      The closing bracket on the qq can not have any spaces between it or the last character or the space is translated to the next line. I do not quite understand why?
      Thanks again Ken

        That's sort of what my "... you won't need the terminal newline ..." was referring to.

        When you have a quoted string that extends over more than one line, you have embedded newlines - these are the ones you typed at the keyboard (not explicit \n characters).

        Your original last two lines:

        <td> <table><tr><td><br>\n";

        are actually:

        <td>{embedded newline} <table><tr><td><br>\n";

        You could have achieved the same output with:

        <td> <table><tr><td><br> ";

        which is actually:

        <td>{embedded newline} <table><tr><td><br>{embedded newline} ";

        Although I used ellipses, what you see on the screen:

        <table ...> ... };

        is actually:

        <table ...>{embedded newline} ...{embedded newline} };

        I hope that explains it. If not, please show an example of exact output.

        -- Ken

Re: Compress me
by aquarium (Curate) on Nov 07, 2010 at 23:43 UTC
    i don't really get what the app is trying to do, however my observations are as follows. you're possibly printing many lines before finally rendering the form. it would be arguably better if the initial output was in a textarea or iframe with scrollbars, to make it easier to traverse the output (with option to copy that separately into an editor?) and the submit form. also the html could more closely reflect current best practises, i.e. follow xhtml standard, and use a stylesheet to set look/feel of the webpage instead of outdated html styling attributes. but these are not prerequisites as such, as your code will "work" as it is.
    the hardest line to type correctly is: stty erase ^H
      Hi,

      I also use stylesheets . I learned html a while back and I am trying to empty my head of the old school ways.
      Your ideas are good. That is just some example code. My use for that code is date thing I was testing for my program.
      Which I ended up doing it a different way.

      Thanks for your suggestions