1 / 10

Perl

Perl. Day 2. Party Time!. What time is it? Perl has a handy little function called time $Now=time; print(&quot;$Now<br>&quot;); Humm…that returns something like: 1252614575 What the? That is the number of seconds since January 1 st , 1970 at midnight GMT;

freira
Download Presentation

Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Day 2

  2. Party Time! • What time is it? • Perl has a handy little function called time $Now=time; print("$Now\n"); • Humm…that returns something like: 1252614575 • What the? • That is the number of seconds since January 1st, 1970 at midnight GMT; • Er, that’s not especially helpful…but localtime is: ($sec,$min,$hour,$monthday,$month,$year,$weekday,$yearday,$dst)=localtime(time); • Unfortunately, some of those are not what you’d expect. • Month: Jan is 0. Dec is 11. • The year, tells you how many years its’ been since 1900…so this year it return 109. Thus you usually have to add 1900 to it. • Weekday: 0 is Sunday, 1 is Monday, … 6 is Saturday.

  3. WhatTime.pl #!C:\perl\bin\perl.exe ($sec,$min,$hour,$monthday,$month,$year,$weekday,$yearday,$dst)=localtime(time); $year=$year+1900; $month=$month+1; if($hour<10) { $hour=“0”.$hour; } if($min<10) { $min=“0”.$min; } if($sec<10) { $sec=“0”.$sec; } print(“It’s $hour:$min:$sec on $month-$monthday-$year\n”);

  4. join • If you have a bunch of stuff in an array, and you want to print them out in a pretty way: @Fruits=(‘apple’,’grape’,’bananna’,’orange’); $Pretty=join(“ **\n”,@Fruits); Print(“$Pretty\n”); apple ** grape ** bananna ** orange **

  5. Getting to a particular folder • Perhaps you want perl to look in a folder for a file • First you must tell it to go there chdir(“C:\\temp”); • But what if that directory doesn’t exist? chdir(“C:\\temp”) || die “Unable to change to C:\\temp\n”;

  6. Testing Files • Once in a directory you may wish to see if particular files exist, are readable, writable, or executable: $FileName=“test.txt”; if(-r $FileName) { print(“$FileName is readable\n”); } if(-w $FileName) { print(“$FileName is writable\n”); } • There are many available tests, but the most common are: -f (Is this a file) -d (Is this a directory) -r (Is this file readable) -w (Is this file writable) -e (Is this file executable, ie, a script)

  7. File Facts • Sometimes knowing a file exists isn’t enough, maybe you want to know how big it is? • stat returns a bunch of info. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)= stat($filename); • Most of those don’t matter, but some are useful: • 7: $size total size of file, in bytes • 8: $atime last access time in seconds since the epoch • 9: $mtime last modify time in seconds since the epoch

  8. Looking inside a file • First you must open the file: open(myfile,”$FileName”) || die “unable to open $FileName\n”; • This creates a “file handle” called “myfile” • Now we can see whats in there, this will read the first line of the file and print it out: $FirstLine=<myfile>; print(“$FirstLine\n”); • The above example gave you 1 line because you assigned it to a scalar, if you assign it to an array you get all lines: @AllLines=<myfile>; $Output=join(“\n”,@AllLines); print(“$Output\n”); • When you are done, you should close the file handle Close(myfile);

  9. What if I want to make a file? • To make a file, you open it for writing: open(myfile,”>$FileName”) || die “unable to write to $FileName\n”; • Now you can simply print to the file: print(myfile “Hello World\n”); • Note: If the file exists before you start, it is overwritten (with no warning) • If you wish to leave whats currently in the file and just add more to the bottom of the file, you’d do this: open(myfile,”>>$FileName”) || die “unable to write to $FileName\n”; • Note the 2 >>’s • > means overwrite the file with a new file (lose existing stuff) • >> means append to the file, don’t delete anything. • If the file doesn’t exist >> still creates it.

  10. Directories • You may also want to get a list of all files in a directory: opendir(mydir,”$CurrentDir”); @Files=readdir(mydir); closedir(mydir); • Now the Array @Files has a list of all the files in the current directory. • Note, the first 2 elements in this array will always be . and ..: • . Is the current directory • .. Is the current directories parent.

More Related