It's a bad idea to save the count in the script, but it can be done. I wrote a CGI script to do this while exploring some other confusion.

Here are two console scripts. The first does the wrong thing by saving the count in itself. The second does the right thing by using an external data file.

As always in Perl, there are many other ways to increment a count. Neither of these two examples is the worst or best way to do it, they're merely bad and better.

WRONG

#!perl -w # self-contained counter script increments and prints value in DATA # set permissions to allow script to read and write itself - epoptai use strict; my$e = 0; my$d; while(<DATA>){$d .= $_} # read data $d++; # increment open (ME,"+>> $0") or die "$!"; # open self unless($^O=~/mswin/i){ flock(ME,2) or die "$!"} # lock unless win32 my@me = <ME>; # read self into array for(@me){ if($_=~m|_\_DATA_\_|){ $e = 1; next } # find last line if($e == 1){ $_ =~ s/\d+/$d/o } # and replace with incremented val +ue } seek ME, 0, 0; # goto top of self truncate ME, 0; # and clear it print ME @me; # write modified contents close ME or die "$!"; print $d; # print incremented value __DATA__ 0

RIGHT

#!perl -w # counter script increments and prints value in $file # set permissions to allow script to read and write $file - epoptai use strict; my$file = 'count.txt'; open (FILE,"+>> $file") or die "$!"; # open file unless($^O=~/mswin/i){ flock(FILE,2) or die "$!"} # lock unless win32 local $/ = undef; my$num = <FILE>; # read file into array $num++; seek FILE, 0, 0; # goto top of file truncate FILE, 0; # and clear it print FILE $num; # write modified contents close FILE or die "$!"; print $num; # print incremented value

In reply to Re: How do you save a perl script? by epoptai
in thread How do you save a perl script? by dswimboy

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.