in reply to Re^5: FEAR OF PERL
in thread FEAR OF PERL

thanks mwa and thanks for you tips also
iam now having a problem and i think it's stupidness to have such one
i posts some posts about how to comment zones in the zone file and i got a wonderful answers and a good code especially from shmem
by the way here is the code shmem wrote
#!/usr/bin/perl -w use strict; print " please enter the domain name: "; my $targetdomain = <STDIN>; chomp $targetdomain; my $file = "/home/blackice/hello"; rename $file, "$file.bak" or die "Can't rename file '$file': $!\n"; open my $in, '<', "$file.bak" or die "Can't read file '$file': $!\n"; open my $out, '>', $file or die "Can't write file '$file': $!\n"; my $comment = 0; my $block = 0; while(<$in>) { if (/^zone\s+"$targetdomain"/) { $comment++; $block += () = /(\{)/g; print $out '// '.$_; next; } if($comment) { $block += () = /(\{)/g; s!^!// ! if $comment or $block; $block -= () = /(\})/g; $comment = 0 unless $block; } print $out $_; }

and it works very well and i understand the way of that script works and it was agood idea
but i want to add some feature to this script i want to add the date at the top of the zone when that zone was commented on
i have tried to make some tries ,, i have creates my $date = `date -u "+%m/%d/%Y %H:%M:%S"`; this is a date string that i can print in the script and i tried to print it here
print "date"; print $out $_;

also i have tried to print it before this line  print $out '// '.$_; next; but it failed ,, i also have tried to print it in some other places and it also fails
so can you help me of how to think about that

thanks

Replies are listed 'Best First'.
Re^7: FEAR OF PERL
by firewall00 (Acolyte) on Oct 15, 2007 at 03:27 UTC
    i finally knows hot to do it :)
    first i would to say perl is great language and all perl people are helpful and iam going to learn , learn and learn GOD willing
    here is the code to make the whole task
    #!/usr/bin/perl -w use strict; print " please enter the domain name: "; my $targetdomain = <STDIN>; my $susdate = `date -u "+%m/%d/%Y %H:%M"`; my $char = "This Domain is Suspended at "; chomp $targetdomain; my $file = "/home/blackice/hello"; rename $file, "$file.bak" or die "Can't rename file '$file': $!\n"; open my $in, '<', "$file.bak" or die "Can't read file '$file': $!\n"; open my $out, '>', $file or die "Can't write file '$file': $!\n"; my $comment = 0; my $block = 0; while(<$in>) { if (/^zone\s+"$targetdomain"/) { print $out '// ' . $char; print $out ': ' . $susdate; $comment++; $block += () = /(\{)/g; print $out '// '.$_; next; } if($comment) { $block += () = /(\{)/g; s!^!// ! if $comment or $block; $block -= () = /(\})/g; $comment = 0 unless $block; } print $out $_; }

    Thanks for all

      Congratulations on modifying this script so that it meets your needs!

      Now take some time to comment the heck out of the script. Hit every line, and explain in plain language what it is you are doing and why.

      Now that you understand what all of the code is doing, is there anything redundant or broken in the code that should be removed or fixed?

      If I were reviewing this code, I would make these suggestions:

      • Add some comments.
      • use warnings.
      • Line 32 has a redundant condition in its if clause.
      • Use Perl's date functions instead of shelling out to `date` in line 9.
      • Use scalars in interpolated strings to simplify formatting of data. (Lines 24,25).

      It's clear that you are making progress. Keep up the good work and you will continue to build your skills.


      TGI says moo