Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Storing Experience for Posterity

by morgon (Deacon)
on Jun 13, 2014 at 08:17 UTC ( #1089790=note: print w/ replies, xml ) Need Help??

in reply to Storing Experience for Posterity

I would prefer to generate the timestamp in Perl rather than using backticks. One possibilty (among many):

my $date = POSIX::strftime "%Y%m%d", localtime;
And what you totally disregard is error-handling.

Your get could fail (e.g. no internet-connection), your parsing of the page could fail (the website changed it's layout) and your open could fail (some permission problem maybe).

You should handle these conditions and report appropriate errors, rather than fail silently and maybe loose the history of your meteoric raise...

Comment on Re: Storing Experience for Posterity
Download Code
Re^2: Storing Experience for Posterity
by GotToBTru (Chaplain) on Jun 18, 2014 at 19:02 UTC


    • Added error message if web page get fails
    • Use strftime instead of backtics and Windows date symbols
    • Test to see if all 3 values were found before logging
    • Using Tie::File for easy read-write access to file
    • Replace latest line with new data if Level has not changed
    • Added error message if regex could not find all 3 values
    use strict; use warnings; use LWP::Simple; use URI::URL; use POSIX qw/strftime/; use Tie::File; my $date= strftime "%Y%m%d", localtime; # YYYYMMDD my ($url, $content, $experience, $level, $posts); $url = url(''); $content = get($url) or die "Get failure"; $content =~ s/\cJ//g; $content =~ s/\cM//g; ($experience, $level, $posts) = ($content =~ /Experience:\D+(\d+).+ Level:.+([A-Z][a-z]+\s+\(\d+\)).+ Writeups:.+>(\d+)</x); if ($experience && $level && $posts) { tie my @log, 'Tie::File', 'perl_xp.csv'; if ($level eq (split ',',$log[-1])[3] || '') { pop @log; } push @log, join ',',$date,$experience,$posts,$level; untie @log; } else { die "Could not find values on web page"; }
    1 Peter 4:10

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1089790]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-09-18 18:03 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (120 votes), past polls