Skip to content

Instantly share code, notes, and snippets.

@ahgood
Last active November 15, 2016 01:24
Show Gist options
  • Save ahgood/b7278c91916fac1c69f54c4c8cbbad36 to your computer and use it in GitHub Desktop.
Save ahgood/b7278c91916fac1c69f54c4c8cbbad36 to your computer and use it in GitHub Desktop.
Perl read write file example, each time access it will plus one.
#!/usr/local/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser set_message);
my $q = CGI->new;
my $filename = '/gsa/a23gsa/.projects/p1/aheweb23/projects/wwwstage/content/innovation/cn/like/db.txt';
my $like = 0;
print $q->header('text/plain');
# Read like count and plus one
open(my $fh, '<', $filename) or die "Can't open $filename: $!";
while (my $line = <$fh>) {
$like = $line + 1;
}
# Write new like count
open(my $fh, '>', $filename);
print $fh $like;
close $fh;
# Read like count
open( my $fh, '<', $filename ) or die "Can't open $filename: $!";
while ( my $line = <$fh> ) {
print $line;
}
close $fh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment