<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Unix Tutorial &#187; Perl</title>
	<atom:link href="http://www.unixtutorial.org/category/unix/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.unixtutorial.org</link>
	<description>Learn UNIX</description>
	<lastBuildDate>Wed, 03 Mar 2010 22:55:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Converting date and time to Unix epoch in Perl</title>
		<link>http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/</link>
		<comments>http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 23:08:26 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[epoch]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=452</guid>
		<description><![CDATA[This post will show you how to convert standard dates into Unix epoch times in Perl.]]></description>
			<content:encoded><![CDATA[<p>Today I was working on a script, and one of the subroutines needed simple seconds-based arithmetics with time. As you probably remember from  my <a title="time and date in Unix" href="http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/">date and time in Unix scripts </a>article, the easiest way to approach this task is to deal with the raw representation of date and time in Unix &#8211; the Unix epoch times. This post will show you how to convert standard dates into Unix epoch times in Perl.</p>
<h3>Why would you want to convert timestamps to Unix epoch time?</h3>
<p><a title="Unix epoch" href="http://www.unixtutorial.org/glossary/#unix_epoch">Unix epoch time</a> is an ever-growing counter which increments every second and shows the number of seconds elapsed since 00:00:00 UTC on January 1, 1970. Such numbers are pretty useless in their raw form for the tasks of confirming current time and date, but they're perfect for measuring between two points in time.</p>
<h3>Current epoch time in Perl</h3>
<p>First things first. If you want to confirm the current Unix epoch time in Perl, here's how you do it: just use the <strong>time()</strong> subroutine.</p>
<pre>#!/bin/perl
print "Current (epoch) time: " . time() . "\n";</pre>
<h3>Converting regular date and time into epoch time</h3>
<p>It's equally easy to convert time into Unix epoch representation, for this you should use <strong>timegm() </strong>or <strong>localtime()</strong> subroutine from the <strong>Time::Local</strong> module.</p>
<p><strong>timegm()</strong> has the following syntax:</p>
<pre>$current = timegm($sec,$min,$hours,$day,$month,$year);</pre>
<p><strong>Important:</strong> While most of the parameters are self-explanatory, it is probably worth reminding that $year should be specified as a number like 109 and not 2009.<br />
Also, the $month is not the number of a month, but a number of months since January. So, 0 will mean January itself, 1 will mean February, and 11 means December.</p>
<p>Here's a fully working example, showing the countdown in seconds until midnight on the New Year's Eve 2009:</p>
<pre>#!/usr/bin/perl
#
use Time::Local;
#
$sec=59;
$min=59;
$hours=23;
$day=31;
$month=11;
$year=109;
#
$current = time();
print "Current (epoch) time: $current\n";
#
$newyear = timegm($sec,$min,$hours,$day,$month,$year);
print "New Year's Eve 2009: $newyear\n";
#
print "Only " .($newyear-$current) . " seconds to go\n";</pre>
<p>When you save this script as epoch.pl and make the file executable, here's what you'll get if you run it:</p>
<pre># <strong>./epoch.pl</strong>
Current (epoch) time: 1234911622
New Year's Eve 2009: 1262303999
Only 27392377 seconds to go</pre>
<p>That's it for today! Hope this little post will save you time when tacking next scripting challenge with Perl.</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="Time and date" href="http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/">Time and date in Unix scripts</a></strong></li>
<li><strong><a title="gnu date calculations" href="http://www.unixtutorial.org/2008/09/easy-date-calculations-in-unix-scripts-with-gnu-date/">Date calculations with GNU date command</a></strong></li>
<li><strong><a title="unix glossary" href="http://www.unixtutorial.org/glossary">Glossary of Unix terms</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Perl Scripting: Check If a File Exists</title>
		<link>http://www.unixtutorial.org/2009/01/perl-scripting-check-if-a-file-exists/</link>
		<comments>http://www.unixtutorial.org/2009/01/perl-scripting-check-if-a-file-exists/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 13:22:55 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=428</guid>
		<description><![CDATA[It's not often that I write about Perl Scripting on Unix Tutorial, but that's just because I don't script in Perl this much on a regular basis. Today, however, I'd like to share one of the building blocks &#8211; a really basic piece of functionality which you'll find really useful in almost any Perl script. [...]]]></description>
			<content:encoded><![CDATA[<p>It's not often that I write about <a title="perl scripting" href="http://www.unixtutorial.org/category/unix/perl/">Perl Scripting</a> on <strong><a title="UNIX tutorials" href="http://www.unixtutorial.org">Unix Tutorial</a></strong>, but that's just because I don't script in Perl this much on a regular basis. Today, however, I'd like to share one of the building blocks &#8211; a really basic piece of functionality which you'll find really useful in almost any Perl script.</p>
<h3>How to check if a file exists in Perl</h3>
<p>First of all, we need to create one file for our experiments:</p>
<p>ubuntu$ <strong>touch /tmp/file.try</strong></p>
<p>Now, create a new file with our script: <strong>/tmp/file-try.pl</strong> (don't forget to<strong> chmod a+x /tmp/file-try.p</strong>l afterwards to make it executable):</p>
<pre>#!/usr/bin/perl
#
$FILE1="/tmp/file.try";
$FILE2="/tmp/file2.try";
#
if (-e $FILE1) {
        print "File $FILE1 exists!\n";} else {
        print "File $FILE1 is not found!\n";
}
if (-e $FILE2) {
        print "File $FILE2 exists!\n";
} else {
        print "File $FILE2 is not found!\n";
}</pre>
<p>Now, as you can see from the example, the actual condition checking for the existence of a file is this:</p>
<pre>if (-e $FILE)</pre>
<p>where <strong>$FILE</strong> is the string containing the filename.</p>
<p>In my example, there's two filenames (<strong>$FILE1</strong> and <strong>$FILE2</strong>) and two conditions, so that you can see for yourself how one of them confirms the existence of a file and another one proves that there's no such file found.</p>
<p>To see the script work, simply run it without any parameters:</p>
<pre>ubuntu$ <strong>/tmp/file-try.pl</strong>
File /tmp/file.try exists!
File /tmp/file2.try is not found!</pre>
<p>That's all for today! Enjoy your Perl scripting!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="file permissions unix perl" href="http://www.unixtutorial.org/2008/02/file-type-and-permissions-in-perl/">How to find out file permissions in Perl</a></strong></li>
<li><strong><a title="walking directory in perl" href="http://www.unixtutorial.org/2007/11/perl-searching-through-directory-trees/">Walking a directory tree in Perl</a></strong></li>
<li><a title="unix glossary" href="http://www.unixtutorial.org/glossary"><strong></strong></a><strong><a href="http://www.unixtutorial.org/glossary">Unix glossary</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/01/perl-scripting-check-if-a-file-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Find Out a File Type and Permissions in Perl</title>
		<link>http://www.unixtutorial.org/2008/02/file-type-and-permissions-in-perl/</link>
		<comments>http://www.unixtutorial.org/2008/02/file-type-and-permissions-in-perl/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 23:00:19 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/2008/02/file-type-and-permissions-in-perl/</guid>
		<description><![CDATA[A few months ago, I've given a really simple example of using Perl for parsing directory trees in Unix. If you looked closer at it, you would have noticed that the script was working fine, but showing file modes as strange large numbers which didn't look like the usual file permissions you would expect. Today [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago, I've given a really simple example of <a href="http://www.unixtutorial.org/2007/11/perl-searching-through-directory-trees/">using Perl for parsing directory trees in Unix</a>. If you looked closer at it, you would have noticed that the script was working fine, but showing file modes as strange large numbers which didn't look like the usual file permissions you would  expect. Today I'm going to explain why this happens, and show you how to find out a user type in Perl.</p>
<p><strong>lstat </strong>and <strong>stat </strong>functions, return, among other things, the file mode value. While it looks confusing initially, it is in fact quite simply a combination field, which includes both the file type and all the permissions for it. If you print this field as a single decimal number, you will not recognize it, but if you simply convert it to octal, you will immediately start seeing the pattern:</p>
<p>Mysterious mode <em>33261 </em>from the example below becomes <em>100755 </em>when converted into octal, and you can easily see then the permission part of it: <em>0755</em>.</p>
<p>In order to find out a file type in Perl, you need to be able to separate the type part from the permissions part of the file mode value. You can do this manually, but applying the bitmasks, but the easier way is to use  the <strong>Fcntl ':mode'</strong> Perl module.</p>
<p><strong>Fcntl':mode'</strong> module contains all the constants and functions you need to extract the file type and permissions without complicating the task too much.</p>
<p><strong>S_IMODE</strong> is the function which helps you extract the permissions:</p>
<pre>        $permissions = S_IMODE($mode);</pre>
<p>and <strong>S_IFMT</strong> is the function which extracts the file type:</p>
<pre>        $filetype = S_IFMT($mode);</pre>
<p>Now, obviously these extracted values are just the raw numbers, that's why you'll likely want to convert the permissions into octal, and to somehow connect the file type with the common symbolic notation, that's exactly what I'm doing in today's code example.</p>
<p>Here's how it looks (I have the empty lines commented out just so that the code can be correctly shown in WordPress):</p>
<pre>#!/usr/bin/perl
use Fcntl ':mode';
#
if ($ARGV[0] ne "") {
        $filename = $ARGV[0];
} else {
        print "Please specify a file!\n";
        exit;
}
#
if (($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($filename)) {
        $user = getpwuid($uid);
        $group = getgrgid($gid);
#
        $ftypes[S_IFDIR] = "d";
        $ftypes[S_IFCHR] = "c";
        $ftypes[S_IFBLK] = "b";
        $ftypes[S_IFREG] = "-";
        $ftypes[S_IFIFO] = "p";
        $ftypes[S_IFLNK] = "l";
        $ftypes[S_IFSOCK] = "s";
#
        $permissions = sprintf "%04o", S_IMODE($mode);
        $filetype = S_IFMT($mode);
#
        $ftype = $ftypes[$filetype];
#
        print "File: $filename\n";
	printf "File mode: %o (%d)\n", $mode, $mode;
        printf "File type: %s (%o)\n", $ftype, $filetype;
        print "File permissions: $permissions\n";
        print "File size: $size\n";
        print "File owner user: $user\n";
        print "File group: $group\n";
} else {
        print "Please specify an EXISTING file!\n";
}</pre>
<p>And this is how it works:</p>
<pre>root@ubuntu:/usr/include# <strong>/tmp/stat.pl /tmp/stat.pl</strong>
File: /tmp/stat.pl
File mode: 100755 (33261)
File type: - (100000)
File permissions: 0755
File size: 918
File owner user: greys
File group: greys</pre>
<h3>See also:</h3>
<ul>
<li><a href="http://www.unixtutorial.org/2007/11/perl-searching-through-directory-trees/">Directory tree in Perl</a></li>
<li><a href="http://perldoc.perl.org/functions/stat.html">stat function in Perl</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/02/file-type-and-permissions-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
