There are a bunch of errors and dodgy practices there. The first thing that you need to do is add strictures:

use strict; use warnings;

Then you need to declare all the variables you use by prefixing them with my. For example:

my $input_file = "dump.vcd";

You should always use the three parameter version of open to improve code readability and reduce possible errors of various sorts:

open INPUT, '<', "$input_file";

The while loop is reading one line at a time so you don't need a for loop (see perlsyn - foreach) to iterate over anything - $efile is not an array.

In your regex you use a character set ([!,\#,\",\$,\%,&]), but it isn't what you possibly expect. The characters included in the set are !,#"$%& which includes comma, that may surprise you. Note that none of the quoted characters in the set need to be quoted.

The complete code rewritten and modified to take itself (on my system) as the input looks like:

#!/usr/bin/perl use strict; use warnings; my $input_file = "noname1.pl"; open INPUT, '<', "$input_file"; while (my $efile = <INPUT>) { $efile =~ s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$var $1 $2 +/; print $efile; } close INPUT; __DATA__ $var1 1 , x $end $varx 2 ! _ $end $vary 3 # aa $end

prints:

#!/usr/bin/perl use strict; use warnings; my $input_file = "noname1.pl"; open INPUT, '<', "$input_file"; while (my $efile = <INPUT>) { $efile =~ s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$var $1 $2 +/; print $efile; } close INPUT; __DATA__ $var , x $var ! _ $vary 3 # aa $end

Note that I added the __DATA__ (see perldata - Special Literals) section so I could include something for the regex to match.


DWIM is Perl's answer to Gödel

In reply to Re: Someone please verify this. by GrandFather
in thread Someone please verify this. by ksangam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.