Last active
November 15, 2016 01:24
-
-
Save ahgood/b7278c91916fac1c69f54c4c8cbbad36 to your computer and use it in GitHub Desktop.
Perl read write file example, each time access it will plus one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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