in reply to Re^4: perl script to search and replace comment in .aspx file
in thread perl script to search and replace comment in .aspx file

temporal used the __DATA__ segment to avoid the need for an external file. You will need to learn how to open a file for reading and then read it in like temporal's code does. The code does not modify a file but it creates output that could be written to a new file. You will have to learn how to open a file for writing, too.

Replies are listed 'Best First'.
Re^6: perl script to search and replace comment in .aspx file
by stevieb (Canon) on Jun 08, 2012 at 07:24 UTC

    As Corion stated, you need to learn how to open a file for both reading and writing. In this post I personally went as far as to write my code to give an example of each.

Re^6: perl script to search and replace comment in .aspx file
by hector89 (Novice) on Jun 08, 2012 at 09:25 UTC

    i made some modification in reg exp in the code written by stevieb and it's working fine for this type of data data --- <script type="java/js"> foobie bletch </script> <%-- <script> zelgo mer </script> --> but problem is if i write endi script tag </script> not in the first column,it's not commenting or uncommenting that.

    #!/usr/bin/perl use warnings; use strict; open my $fh, '<', 'hii.txt' or die "Can't open the damn file for readi +ng!: $!"; my @file_content; while ( my $line = <$fh> ){ chomp $line; if ( $line =~ /^<script(.*)>/ ){ $line = "<%-- $line"; push @file_content, "$line\n"; next; } elsif ( $line =~ /^<\/script>$/ ){ $line .= " -->"; push @file_content, "$line\n"; next; } elsif ( $line =~ /^<%--\s+<script(.*)>/ ){ $line =~ s/^<%--\s+//; push @file_content, "$line\n"; next; } elsif ( $line =~ /^<\/script>\s+-->/ ){ $line =~ s/\s+-->//; push @file_content, "$line\n"; next; } push @file_content, "$line\n"; } close $fh; open $fh, '>', 'hii.txt' or die "Can't open the damned file for writin +g: $!"; print $fh @file_content; close $fh;

      Maybe now would be a good moment to look at perlre to learn about how the regular expression matching </script> works. YAPE::Regex::Explain contains the explain tool that does that:

      The regular expression: (?-imsx:^<\/script>\s+-->) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- < '<' ---------------------------------------------------------------------- \/ '/' ---------------------------------------------------------------------- script> 'script>' ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- --> '-->' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

      ... then you'll note that the regular expression only matches at the beginning of the string. Maybe you want to change that expression (and maybe the others as well) to allow for optional whitespace between the beginning of the string and the </script> tag. You would then add \s* between the "beginning of the string" anchor and the match for </script>.

Re^6: perl script to search and replace comment in .aspx file
by hector89 (Novice) on Jun 08, 2012 at 11:24 UTC
    <anyone plss write full working code as per my requirement.i need it urgently.thogh i'm going through docs of perl to learn some basic. the code should work on this type of data data -- <script type="main> </script> <script> </script>

      No. This is not a code writing service. If it is so urgent for you, consider hiring somebody. We will assist you in learning Perl, and we have already shown you the parts that are needed for a program that will do what you seem to want. But combining these parts and finding the errors is the thing you need to do yourself.

      I didn't know about YAPE. Nifty module. Thanks Corion, always seem to stumble into something new from reading your posts =)

      hector89, be sure to take some time to understand the code in all these replies it will help you write your own solution next time!

      This should do what you want:

      use strict; open my $input_fh, '<', 'your_input.aspx'; my $delim = $/; $/ = ''; my $file = <$input_fh>; $/ = $delim; close $input_fh; $file =~ s/(<%--)?\s*<script(.*?)>/($1 ? '' : "\n<%-- ") . "<script$2> +"/ige; $file =~ s=</script>\s*(-->)?='</script>' . ($1 ? '' : " -->\n")=ige; print $file; open my $output_fh, '>', 'your_output.aspx'; print $output_fh $file; close $output_fh;

      Forgot to turn off greedy matching for the script attributes group in my first post, which might be why you were having issues with input that's all on one line.

      Strange things are afoot at the Circle-K.

        thanx temporal for you support and helping me out..but still this code is not solving my purpose.this code does not work on this type of data: <script type=main ></script> and one more problem is this code replaces all things of file after modification. also this code is not able to uncomment the commented part.