Day 12: Recipes I Dates, Times, Writing Files Suggested Reading: - - PowerPoint PPT Presentation

day 12 recipes i
SMART_READER_LITE
LIVE PREVIEW

Day 12: Recipes I Dates, Times, Writing Files Suggested Reading: - - PowerPoint PPT Presentation

Computer Sciences 368 Introduction to Perl Day 12: Recipes I Dates, Times, Writing Files Suggested Reading: Perl Cookbook (2nd Ed.) Chapter 3: Dates and Times Chapter 7: File Access (esp. 7.11, 7.19) 2012 Summer Cartwright 1 Computer


slide-1
SLIDE 1

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Day 12: Recipes I

Dates, Times, Writing Files Suggested Reading: Perl Cookbook (2nd Ed.) Chapter 3: Dates and Times Chapter 7: File Access (esp. 7.11, 7.19)

1

slide-2
SLIDE 2

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Homework Review

2

slide-3
SLIDE 3

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Homework Preview

3

slide-4
SLIDE 4

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Forecast Sample

4

··· <H1>Madison Forecast</H1>\n Local Madison Forecast\n 635 AM CDT THU JUL 26 2012\n <br><br><font size=+1><B>TODAY...</B></font>PARTLY

  • SUNNY. A 20 PERCENT CHANCE OF THUNDERSTORMS IN\n

THE AFTERNOON. HIGHS IN THE UPPER 80S. NORTHWEST WINDS 5 TO\n 10 MPH.\n <br><br><font size=+1><B>TONIGHT...</B> </font>PARTLY CLOUDY. CHANCE OF THUNDERSTORMS THROUGH AROUND\n MIDNIGHT...THEN SLIGHT CHANCE OF THUNDERSTORMS AFTER MIDNIGHT.\n LOWS IN THE MID 60S. NORTHWEST...\n

2012-07-26 06:35 UPPER 80S LOWER 60S

slide-5
SLIDE 5

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Dates and Times

5

slide-6
SLIDE 6

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

What Is So Hard About This?

6

Dates

  • Different calendars
  • Historical calendar changes
  • Y2K, 2038 January 19

Times

  • Coordinated Universal Time (UTC) vs. time zones
  • Daylight saving time
  • Leap years
  • Leap seconds
  • Indiana (http://en.wikipedia.org/wiki/Time_in_Indiana)
slide-7
SLIDE 7

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Unix/POSIX/Epoch Time Seconds since 1970 January 01 @ 0:00 (UTC)

(more or less) Remaining challenge Unix time <–> Other time formats

7

slide-8
SLIDE 8

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Standard Date/Time Functions

Caution: Read perldoc pages!!!

8

localtime

get local YMDHMS from Unix time

gmtime

get UTC YMDHMS from Unix time

Time::Local::timelocal create Unix time from local YMDHMS Time::Local::timegm

create Unix time from UTC YMDHMS

POSIX::mktime

create Unix time from local YMDHMS

POSIX::strftime

format a Unix time

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time); my $real_year = 1900 + $year;

slide-9
SLIDE 9

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Parsing Dates and Times

  • Use regular expressions

9

  • Use CPAN’s Date::Manip or Date::Manip::Date

if (m,(\d{1,2})/(\d{1,2})/(\d{4}),) { my $year = $3 - 1900; my $month = $1 - 1; my $mday = $2; } my $unixtime = timelocal(0, 0, 0, $mday, $month, $year);

slide-10
SLIDE 10

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Time Interval Calculations

  • Use Unix time

– Convert to Unix time – Do math in seconds – Convert to meaningful output manually

10

  • Use CPAN’s Date::Calc

use POSIX qw/floor/; use Time::Local qw/timelocal/; my $start = timelocal(0, 0, 11, 18, 6, 112); # Calculate interval since course started my $interval = time() - $start; my $minutes = floor($interval / 60); my $seconds = $interval - ($minutes * 60);

slide-11
SLIDE 11

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Timing Events

  • Unix times: second-level resolution

11

my $start = time(); sleep(rand(10)); my $end = time(); printf "Slept %f seconds\n", $end - $start;

  • Time::HiRes: much higher resolution

use Time::HiRes qw/gettimeofday/; my $start = gettimeofday(); sleep(rand(10)); my $end = gettimeofday(); printf "Slept %f seconds\n", $end - $start;

slide-12
SLIDE 12

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Writing Files

12

Seriously?

What is so hard about writing a file?

>_<

slide-13
SLIDE 13

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Writing Files Robustly

13

aka What if the power goes off in the middle of a file write?

slide-14
SLIDE 14

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

  • pen(OUT, '>', $filename) or die("…");

print OUT "Header\n"; # Do some long calculation => @things [1000ms] foreach my $thing (@things) { # Format $thing => $display_thing [5ms each] print OUT $display_thing; } close(OUT);

$filename (OLD) $filename (NEW)

  • pen(OUT, '>', $filename) or die("…");

print OUT "Header\n"; # Do some long calculation => @things [1000ms] foreach my $thing (@things) { # Format $thing => $display_thing [5ms each] print OUT $display_thing; } close(OUT);

  • pen(OUT, '>', $filename) or die("…");

print OUT "Header\n"; # Do some long calculation => @things [1000ms] foreach my $thing (@things) { # Format $thing => $display_thing [5ms each] print OUT $display_thing; } close(OUT);

Writing Files: The Problem

14

slide-15
SLIDE 15

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Perl Output Buffers

15

  • pen(OUT, '>', $filename) ...

print OUT "first line\n"; sleep(30); foreach my $thing (@things) { sleep(10); print OUT calculate($thing); } close(OUT);

FILE Perl Output Buffer

×

slide-16
SLIDE 16

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Flushing Your … Buffers

  • Set $| (dollar-pipe) to true
  • Affects all output buffers
  • Can significantly affect performance
  • Not a general solution, but sometimes useful

16

#!/usr/bin/perl use strict; use warnings; $| = $ARGV[0]; # try 0, then try 1 print "Start of output... "; sleep(2); print "and now we are done!\n";

slide-17
SLIDE 17

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Atomic File Writes

17

Key idea: Write to separate file, move into place

sub write_file { my ($filename, $contents) = @_;

  • pen(NEW, '>', "$filename.NEW") or return;

print NEW $contents or return; close(NEW) or return; rename($filename, "$filename.BAK") or return; rename("$filename.NEW", $filename) or return; return 1; }

slide-18
SLIDE 18

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Better Temporary Files

Use File::Temp to open file and give name

18

use File::Temp qw/tempfile/; my $temp_fh = tempfile(); my ($fh, $temp_filename) = tempfile(); print $fh $contents; close($fh); rename($filename, "$filename.BAK"); rename($temp_filename, $filename);

slide-19
SLIDE 19

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Homework

19

slide-20
SLIDE 20

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Forecast Sample

20

··· <H1>Madison Forecast</H1>\n Local Madison Forecast\n 635 AM CDT THU JUL 26 2012\n <br><br><font size=+1><B>TODAY...</B></font>PARTLY

  • SUNNY. A 20 PERCENT CHANCE OF THUNDERSTORMS IN\n

THE AFTERNOON. HIGHS IN THE UPPER 80S. NORTHWEST WINDS 5 TO\n 10 MPH.\n <br><br><font size=+1><B>TONIGHT...</B> </font>PARTLY CLOUDY. CHANCE OF THUNDERSTORMS THROUGH AROUND\n MIDNIGHT...THEN SLIGHT CHANCE OF THUNDERSTORMS AFTER MIDNIGHT.\n LOWS IN THE MID 60S. NORTHWEST...\n

2012-07-26 06:35 UPPER 80S LOWER 60S

slide-21
SLIDE 21

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Weather Analysis, Part I

  • Download current forecast
  • Parse forecast timestamp and convert to Unix time
  • Parse temperature forecasts
  • Save the data

– Use safe file-write pattern with tempfile() – Filename contains date of forecast timestamp – Record forecast timestamp and high/low predictions – If script is run twice in one day, overwrite

21