<?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>The Effective Perler &#187; miscellany</title>
	<atom:link href="http://www.effectiveperlprogramming.com/blog/category/book/chapters/miscellany/feed" rel="self" type="application/rss+xml" />
	<link>http://www.effectiveperlprogramming.com</link>
	<description>Effective Perl Programming - write better, more idiomatic Perl</description>
	<lastBuildDate>Sat, 28 Jan 2012 02:19:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Create your own dualvars</title>
		<link>http://www.effectiveperlprogramming.com/blog/1470</link>
		<comments>http://www.effectiveperlprogramming.com/blog/1470#comments</comments>
		<pubDate>Sun, 11 Dec 2011 13:36:37 +0000</pubDate>
		<dc:creator>brian d foy</dc:creator>
				<category><![CDATA[item]]></category>
		<category><![CDATA[miscellany]]></category>

		<guid isPermaLink="false">http://www.effectiveperlprogramming.com/?p=1470</guid>
		<description><![CDATA[Perl&#8217;s basic data type is the scalar, which takes its name from the mathematical term for &#8220;single item&#8221;. However, the scalar is really two things. You probably know that a scalar can be either a number or a string, or a number that looks the same as its string, or a string that can be [...]]]></description>
			<content:encoded><![CDATA[<p>Perl&#8217;s basic data type is the scalar, which takes its name from the mathematical term for &#8220;single item&#8221;. However, the scalar is really two things. You probably know that a scalar can be either a number or a string, or a number that looks the same as its string, or a string that can be a number. What you probably don&#8217;t know is that a scalar can be two separate and unrelated values at the same time, making it a <i>dualvar</i>.</p>
<p>You&#8217;re already using a dualvar without knowing it. The <code>$!</code> variable, which holds the value of the last system error, is most often used in its string form:</p>
<pre class="brush:perl">
open my $fh, '>', $filename or die "Error: $!";
</pre>
<p>If something goes wrong with that <code>open</code>, you&#8217;ll get an error such as these:</p>
<pre class="brush:plain">
No such file or directory
Permission denied
</pre>
<p>Both of those error messages have numbers associated with them. You can output their numeric value</p>
<pre class="brush:perl">
open my $fh, '>', $filename or die $! + 0;
</pre>
<p>Now the errors are numbers, which correspond to the <code>errno</code> value for the system call:</p>
<pre class="brush:plain">
2
13
</pre>
<p>These numbers are keys in the <code>%!</code>, and the value for that key is true if that was the last system error.</p>
<p>Perl, on its own, doesn&#8217;t give you a way to create this sort of variable yourself. When you assign a new value to a scalar, whether string or number, Perl clears the previous values it has. When you use a number as a string, Perl converts it to a string, and the same the other way around.</p>
<p>You can watch this with <a href="https://www.metacpan.org/module/Devel::Peek">Devel::Peek</a>. Here&#8217;s a program that sets a string value:</p>
<pre class="brush:perl">
use Devel::Peek;

my $value = 'abc';

Dump( $value );
</pre>
<p>In the scalar record, the <code>POK</code> flag is set, indicating the variable has a string form, and the <code>PV</code> slot has a value (see <a href="http://perldoc.perl.org/perlguts.html">perlguts</a> for more details):</p>
<pre class="brush:plain">
SV = PV(0x100801070) at 0x100827810
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0x100202870 "abc"\0
  CUR = 3
  LEN = 16
</pre>
<p>If you set a numeric value, the flags are different:</p>
<pre class="brush:perl">
use Devel::Peek;

my $value = 137;

Dump( $value );
</pre>
<p>Now there&#8217;s an <code>IOK</code> flag, and one of the numeric slots, in this case the <code>IV</code>, is set:</p>
<pre class="brush:plain">
SV = IV(0x100827800) at 0x100827810
  REFCNT = 1
  FLAGS = (PADMY,IOK,pIOK)
  IV = 137
</pre>
<p>However, if you take the numeric value and use it in a string context, Perl also creates a string version. Now the scalar has both the <code>IOK</code> and <code>POK</code> flags, and both the <code>IV</code> and <code>PV</code> slots have values:</p>
<pre class="brush:plain">
137
SV = PVIV(0x100809208) at 0x100827840
  REFCNT = 1
  FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
  IV = 137
  PV = 0x100202870 "137"\0
  CUR = 3
  LEN = 16
</pre>
<p>If you change the variable, some of those flags disappear, even though the values don&#8217;t necessarily disappear:</p>
<pre class="brush:perl">
use v5.10;
use Devel::Peek;

my $value = 137;

$value = 'Buster';

Dump( $value );
</pre>
<p>Now the scalar&#8217;s value should be just <code>Buster</code>, but the <code>IV</code> slot still has the old <code>137</code>. However, the <code>IOK</code> flag is gone:</p>
<pre class="brush:plain">
SV = PVIV(0x100809208) at 0x100827840
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  IV = 137
  PV = 0x100202870 "Buster"\0
  CUR = 6
  LEN = 16
</pre>
<p>When you set the new value, and Perl hadn&#8217;t used it in a numeric context yet, there was no need to go through the work to translate the string value to the number value. Instead, it just unset the flag that denotes its okay to use the value as a number. You have to use it as a number to again:</p>
<pre class="brush:perl">
use v5.10;
use Devel::Peek;

my $value = 137;

$value = 'Buster';

say $value + 0;

Dump( $value );
</pre>
<p>Now Perl converts it to a number and sets the numeric flags again:</p>
<pre class="brush:plain">
0
SV = PVNV(0x100801e30) at 0x100827840
  REFCNT = 1
  FLAGS = (PADMY,POK,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x100202870 "Buster"\0
  CUR = 6
  LEN = 16
</pre>
<p>That&#8217;s how it works if you go through the Perl interface, but if you play it a scalar through the XS interface, you can set whatever flags and values that you like. That&#8217;s exactly what <a href="https://www.metacpan.org/module/Scalar::Util">Scalar::Util</a>&#8216;s <code>dualvar</code> does that for you:</p>
<pre class="brush:perl">
use v5.10;
use Devel::Peek;
use Scalar::Util qw(dualvar);

my $value = dualvar 137, 'Buster';

Dump( $value );

say "$value";
say $value + 0;
</pre>
<p>Now you have a scalar which has unrelated numeric and string values, <i>and</i> the flags for both values are set:</p>
<pre class="brush:plain">
SV = PVNV(0x100802010) at 0x1008277c8
  REFCNT = 1
  FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
  IV = 137
  NV = 0
  PV = 0x100202870 "Buster"\0
  CUR = 6
  LEN = 16
Buster
137
</pre>
<h2>Things to remember</h2>
<ul>
<li>Scalars can have both numeric and string values at the same time
<li>Those two values can be unrelated
<li>You can create your own dualvar with <a href="https://www.metacpan.org/module/Scalar::Util">Scalar::Util</a>
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Create+your+own+dualvars+http://tinyurl.com/6pxrro3" title="Post to Twitter"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-twitter2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Create+your+own+dualvars+http://tinyurl.com/6pxrro3" title="Post to Twitter"> </a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1470&amp;title=Create+your+own+dualvars" title="Post to Delicious"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="Post to Delicious" /></a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1470&amp;title=Create+your+own+dualvars" title="Post to Delicious"> </a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1470&amp;title=Create+your+own+dualvars" title="Post to Digg"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1470&amp;title=Create+your+own+dualvars" title="Post to Digg"> </a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1470&amp;t=Create+your+own+dualvars" title="Post to Facebook"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-facebook.png" alt="Post to Facebook" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1470&amp;t=Create+your+own+dualvars" title="Post to Facebook"> </a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1470&amp;title=Create+your+own+dualvars" title="Post to Reddit"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-reddit.png" alt="Post to Reddit" /></a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1470&amp;title=Create+your+own+dualvars" title="Post to Reddit"> </a></p>]]></content:encoded>
			<wfw:commentRss>http://www.effectiveperlprogramming.com/blog/1470/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turn off autovivification when you don&#8217;t want it</title>
		<link>http://www.effectiveperlprogramming.com/blog/1256</link>
		<comments>http://www.effectiveperlprogramming.com/blog/1256#comments</comments>
		<pubDate>Sun, 31 Jul 2011 22:01:20 +0000</pubDate>
		<dc:creator>brian d foy</dc:creator>
				<category><![CDATA[item]]></category>
		<category><![CDATA[miscellany]]></category>

		<guid isPermaLink="false">http://www.effectiveperlprogramming.com/?p=1256</guid>
		<description><![CDATA[Autovivification, although a great feature, might bite you when you don&#8217;t expect it. I explained this feature in Understand autovivification, but I didn&#8217;t tell you that there&#8217;s a way to control it and even turn it off completely. The autovivification pragma, which you can get from CPAN, lets you decide how autovivification works, or doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Autovivification, although a great feature, might bite you when you don&#8217;t expect it. I explained this feature in <A href="http://www.effectiveperlprogramming.com/blog/1247">Understand autovivification</a>, but I didn&#8217;t tell you that there&#8217;s a way to control it and even turn it off completely.</p>
<p>The <a href="http://search.cpan.org/dist/autovivification">autovivification</a> pragma, which you can get from CPAN, lets you decide how autovivification works, or doesn&#8217;t work. As you saw in <A href="http://www.effectiveperlprogramming.com/blog/1247">Understand autovivification</a>, there are several ways that autovivification comes into play, specifically when you dereference an undefined value to:</p>
<ul>
<li>fetch a value
<li>store a value
<li>test an undefined value with <code>exists</code>
<li><code>delete</code> an element
</ul>
<p>The pragma lets you control each of these independently.</p>
<p>The simplest use is simply <i>unimport</i> the module with <code>no</code>. This gets a bit tricky because this pragma is more interesting for what you disallow rather than enable:</p>
<pre class="brush:perl">
no autovivification;
</pre>
<p>With no arguments, this turns off autovivification for fetching a value, or using <code>delete</code> or <code>exists</code>. This <i>does not</i> turn off autovivification for storing a value. You&#8217;re more likely to make mistakes with those first three, and most likely to get what you want with the last one.</p>
<p>None of these work, in that they don&#8217;t create any more of the data structure:</p>
<pre class="brush:perl">
no autovivification;

my $cats;
delete $cats{'Buster'};
my $foo = $cats{'Buster'}; 

if( exists $cats{'Buster'} ) { 1 }

use Data::Dumper;
print Dumper( $cats );
</pre>
<p>You don&#8217;t get an error or a warning, but nothing happens to <code>$cats</code>, which is still undefined at the end. The pragma just leaves the undefined value as it is and your program continues. In those cases, you still get the answers that expect. When the element isn&#8217;t there, you still get undef when you try to access it. It stills returns false when you try <code>exists</code> and the element isn&#8217;t there after a <code>exists</code>. Your program continues without a warning.</p>
<p>If you want to know when you&#8217;ve skipped at possible autovivification, you can also tell <a href="http://search.cpan.org/dist/autovivification">autovivification</a> to warn you, or if you&#8217;re really paranoid, kill your program.</p>
<p>Including the <code>warn</code> option gives you warnings. As with any import list, when you specify something you override all defaults. If you still want those defaults, you have to list them explicitly:</p>
<pre class="brush:perl">
no autovivification qw(fetch delete exists warn);

use Data::Dumper;

my $cats;

delete $cats->{Buster};

print Dumper( $cats );
</pre>
<p>Now you get a warning:</p>
<pre class="brush:plain">
Reference was vivified at auto.pl line 7.
$VAR1 = undef;
</pre>
<p>If you want to stop the program, you use <code>strict</code> instead of <code>warn</code>:</p>
<pre class="brush:perl">
no autovivification qw(fetch delete exists strict);

use Data::Dumper;

my $cats;

delete $cats->{Buster};

print Dumper( $cats );
</pre>
<p>Your program stops at the point that Perl tries to autovivify something and gives you an file and line where <a href="http://search.cpan.org/dist/autovivification">autovivification</a> stopped your program:</p>
<pre class="brush:plain">
Reference vivification forbidden at auto.pl line 7.
</pre>
<p>When you store a value, however, <a href="http://search.cpan.org/dist/autovivification">autovivification</a> doesn&#8217;t get in your way, even if you create a deep one:</p>
<pre class="brush:perl">
no autovivification;

use Data::Dumper;

my $cats;

$cats->{Buster}{count} = 9;

print Dumper( $cats );
</pre>
<p>You still autovivified the structure:</p>
<pre class="brush:plain">
$VAR1 = {
          'Buster' => {
                        'count' => 9
                      }
        };
</pre>
<p>If you want to turn off autovification for storing a variable, you have to explicitly disallow it. As before, you have to provide the other default options that you&#8217;d like to keep:</p>
<pre class="brush:perl">
no autovivification qw(fetch delete exists store strict);

use Data::Dumper;

my $cats;

$cats->{Buster}++;

print Dumper( $cats );
</pre>
<p>Now, when you try to autovivify a hash reference to store a value in the <code>Buster</code> key, your program stops (because you are also using <code>strict</code>:</p>
<pre class="brush:plain">
Reference vivification forbidden at auto.pl line 7.
</pre>
<h2>Lexical scope</h2>
<p>You can limit the effect of <a href="http://search.cpan.org/dist/autovivification">autovivification</a> pragma to part of your program by using it in only in the scope where you need it. There are two ways that you can do this, depending on what you need.</p>
<p>First, you can turn off all autovivification for the entire file (which is itself a scope, see <a href="http://www.effectiveperlprogramming.com/blog/48">Know what creates a scope</a>), but re-enable it for the bits where you need it:</p>
<pre class="brush:perl">
no autovivification qw(fetch delete exists store strict);

# can't do it in here

{
use autovivification qw(store);

...; # allow some of it in here
}

# can't do it in here, either
</pre>
<p>Second, you can leave Perl&#8217;s normal autovivification features in place for the entire file, but turn it off in the sensitive parts of the program, just as in <span class="item">Item 100. Use lexical warnings to selectively turn on or off complaints</span> (but in this case, engaging safety features):</p>
<pre class="brush:perl">
...; # program with possible autovivification

{
# can't do it in here
no autovivification qw(fetch delete exists store strict);

}

...; # program with possible autovivification
</pre>
<h2>Things to remember</h2>
<ul>
<li>The <a href="http://search.cpan.org/dist/autovivification">autovivification</a> pragma lets you disable Perl&#8217;s automatic data structure creation
<li>The defaults do not prevent you from assigning to an autovivified reference
<li>You can enable warnings or errors to catch autovivification events
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Turn+off+autovivification+when+you+don%E2%80%99t+want+it+http://tinyurl.com/4ygnnoa" title="Post to Twitter"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-twitter2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Turn+off+autovivification+when+you+don%E2%80%99t+want+it+http://tinyurl.com/4ygnnoa" title="Post to Twitter"> </a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1256&amp;title=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Delicious"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="Post to Delicious" /></a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1256&amp;title=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Delicious"> </a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1256&amp;title=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Digg"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1256&amp;title=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Digg"> </a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1256&amp;t=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Facebook"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-facebook.png" alt="Post to Facebook" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1256&amp;t=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Facebook"> </a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1256&amp;title=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Reddit"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-reddit.png" alt="Post to Reddit" /></a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1256&amp;title=Turn+off+autovivification+when+you+don%E2%80%99t+want+it" title="Post to Reddit"> </a></p>]]></content:encoded>
			<wfw:commentRss>http://www.effectiveperlprogramming.com/blog/1256/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set the line number and filename of string evals</title>
		<link>http://www.effectiveperlprogramming.com/blog/1338</link>
		<comments>http://www.effectiveperlprogramming.com/blog/1338#comments</comments>
		<pubDate>Sun, 05 Jun 2011 17:46:36 +0000</pubDate>
		<dc:creator>brian d foy</dc:creator>
				<category><![CDATA[item]]></category>
		<category><![CDATA[miscellany]]></category>

		<guid isPermaLink="false">http://www.effectiveperlprogramming.com/?p=1338</guid>
		<description><![CDATA[Errors from a string eval can be tricky to track down since perl doesn&#8217;t tell you where the eval was. It treats each of the string evals as a separate, virtual file because it doesn&#8217;t remember where the string argument came from. Since perl compiles that during the run phase (see Know the phases of [...]]]></description>
			<content:encoded><![CDATA[<p>Errors from a string <a href="http://perldoc.perl.org/functions/eval.html">eval</a> can be tricky to track down since <code>perl</code> doesn&#8217;t tell you where the <a href="http://perldoc.perl.org/functions/eval.html">eval</a> was. It treats each of the string <a href="http://perldoc.perl.org/functions/eval.html">eval</a>s as a separate, virtual file because it doesn&#8217;t remember where the string argument came from. Since <code>perl</code> compiles that during the run phase (see <a href="Know the phases of a Perl program’s execution">Know the phases of a Perl program’s execution</a>), the information the compiler dragged along for filenames and line numbers is so longer around.</p>
<p>In general, a string <a href="http://perldoc.perl.org/functions/eval.html">eval</a> is a dangerous and tricky feature, and most good Perl programmers will tell you not to use it (see <a href="http://www.effectiveperlprogramming.com/blog/965">Know the two different forms of eval</a>). That, in general, is good advice. If you can reasonably get the job done without the string <a href="http://perldoc.perl.org/functions/eval.html">eval</a>, you&#8217;re probably better off. Sometimes, however, you need the string <a href="http://perldoc.perl.org/functions/eval.html">eval</a>, and for the rest of this Item I&#8217;ll assume you have a good reason.</p>
<p>Consider this bit of code that issues a warning and has a fatal error that the <a href="http://perldoc.perl.org/functions/eval.html">eval</a> catches:</p>
<pre class="brush:perl">
use v5.14;
use warnings;
say 'Hello';

eval q(
	my $string;
	print $string;

	die 'Dying in eval';
	);
print $@;
</pre>
<p>The output shows the warning and the error, and each has a line number. However, there&#8217;s no filename and the line number doesn&#8217;t correspond either to the actual line of the statement of the line of the <a href="http://perldoc.perl.org/functions/eval.html">eval</a>:</p>
<pre class="brush:plain">
Hello
Use of uninitialized value $string in say at (eval 1) line 3.
Dying in eval at (eval 1) line 5.
</pre>
<p>You can solve this with a preprocessor comment. Few people know that Perl looks for a couple of special pre-processor commands, but it&#8217;s right there in <a href="http://perldoc.perl.org/perlsyn.html">perlsyn</a>. You can change what <code>perl</code> thinks the line number and filename are with a line that starts with <code># line</code>. You follow that with the line number and the filename (optionally in double quotes):</p>
<pre class="brush:perl">
use warnings;

my $string; # no value there!

print $string; # gets right line and file in warning

# line 58976 "Buster.pm"
print $string;

# line 65783 "Mimi.pm"
print $string;

# line 137
print $string;
</pre>
<p>Now, the warnings make it look like there are at least two modules having problems even though there is a single file. Without a filename in the command, <code>perl</code> uses the previous filename, as in the last line of this output:</p>
<pre class="brush:plain">
Use of uninitialized value $string in print at line.pl line 5.
Use of uninitialized value $string in print at Buster.pm line 58976.
Use of uninitialized value $string in print at Mimi.pm line 65783.
Use of uninitialized value $string in print at Mimi.pm line 137.
</pre>
<p>You should do this only when Perl expects a new statement. If you place one of those pre-processor commands in the middle of a statement, odd things can happen:</p>
<pre class="brush:perl">
use warnings;

my $string; # no value there!

print $string; # gets right line and file in warning

print
# line 58976 "Buster.pm"
	$string;

# line 65783 "Mimi.pm"
print $string;

# line 137
print $string;
</pre>
<p>The output shows that <code>perl</code> changed the current filename to <i>Buster.pm</i> but did not change the line number. For that warning, the line number is the actual line number:</p>
<pre class="brush:plain">
Use of uninitialized value $string in print at line.pl line 5.
Use of uninitialized value $string in print at Buster.pm line 7.
Use of uninitialized value $string in print at Mimi.pm line 65783.
Use of uninitialized value $string in print at Mimi.pm line 137.
</pre>
<p>Once you know that, you can use this in your string <a href="http://perldoc.perl.org/functions/eval.html">eval</a>, which <code>perl</code> compiles just like any other Perl code, including this pre-processor command. You can use the special literals <code>__FILE__</code> and <code>__LINE__</code> (documented in <a href="http://perldoc.perl.org/perldata.html">perldata</a>) to get the current values instead of hard-coding them. You can only use those as separate tokens, which makes their use a bit messy.</p>
<p>This code is the same as the first example in this Item, put with the addition of a pre-processor line as the first line in the <a href="http://perldoc.perl.org/functions/eval.html">eval</a> string:</p>
<pre class="brush:perl">
use v5.14;
use warnings;
say 'Hello';

eval '# line '. __LINE__ . ' "' . __FILE__ . qq("\n) . q(
	my $string;
	print $string;

	die 'Dying in eval';
	);
say $@;
</pre>
<p>Now the <a href="http://perldoc.perl.org/functions/eval.html">eval</a> warnings and error messages show a filename and line number, and the line number corresponds to its actual location in the file:</p>
<pre class="brush:plain">
Hello
Use of uninitialized value $string in print at eval.pl line 7.
Dying in eval at eval.pl line 9.
</pre>
<p>Note that changing the line number and filename also change <code>__LINE__</code> and <code>__FILE__</code>, meaning there&#8217;s no way to get back to the actual line number and filename. As such, you probably only want to use these to represent the actual values:</p>
<pre class="brush:perl">
use v5.14;
use warnings;

# line 58976 "Buster.pm"
say "line ", __LINE__, " file ", __FILE__
</pre>
<pre class="brush:plain">
line 58976 file Buster.pm
</pre>
<h2>Things to remember</h2>
<ul>
<li>String <a href="http://perldoc.perl.org/functions/eval.html">eval</a> act like their own virtual file for line numbering
<li>You can modify the current line number and filename with the <code># line</code> pre-processor command
<li>You can use <code># line</code> in your string <a href="http://perldoc.perl.org/functions/eval.html">eval</a>
<li>The special literals <code>__LINE__</code> and <code>__FILE__</code> report the line number and filename.
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Set+the+line+number+and+filename+of+string+evals+http://tinyurl.com/3uo2m55" title="Post to Twitter"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-twitter2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Set+the+line+number+and+filename+of+string+evals+http://tinyurl.com/3uo2m55" title="Post to Twitter"> </a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1338&amp;title=Set+the+line+number+and+filename+of+string+evals" title="Post to Delicious"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="Post to Delicious" /></a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1338&amp;title=Set+the+line+number+and+filename+of+string+evals" title="Post to Delicious"> </a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1338&amp;title=Set+the+line+number+and+filename+of+string+evals" title="Post to Digg"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1338&amp;title=Set+the+line+number+and+filename+of+string+evals" title="Post to Digg"> </a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1338&amp;t=Set+the+line+number+and+filename+of+string+evals" title="Post to Facebook"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-facebook.png" alt="Post to Facebook" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1338&amp;t=Set+the+line+number+and+filename+of+string+evals" title="Post to Facebook"> </a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1338&amp;title=Set+the+line+number+and+filename+of+string+evals" title="Post to Reddit"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-reddit.png" alt="Post to Reddit" /></a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1338&amp;title=Set+the+line+number+and+filename+of+string+evals" title="Post to Reddit"> </a></p>]]></content:encoded>
			<wfw:commentRss>http://www.effectiveperlprogramming.com/blog/1338/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Override die with END or CORE::GLOBAL::die</title>
		<link>http://www.effectiveperlprogramming.com/blog/1310</link>
		<comments>http://www.effectiveperlprogramming.com/blog/1310#comments</comments>
		<pubDate>Sun, 22 May 2011 23:15:02 +0000</pubDate>
		<dc:creator>brian d foy</dc:creator>
				<category><![CDATA[item]]></category>
		<category><![CDATA[miscellany]]></category>
		<category><![CDATA[object-oriented programming]]></category>

		<guid isPermaLink="false">http://www.effectiveperlprogramming.com/?p=1310</guid>
		<description><![CDATA[Perl lets you override the effects of warn and die by redefining the signals that Perl sends when you call those functions. You probably don&#8217;t want to use the signal from die, though, since it might mean a couple of different things. You handle these special signals by setting values in the %SIG hash just [...]]]></description>
			<content:encoded><![CDATA[<p>Perl lets you override the effects of <a href="http://perldoc.perl.org/functions/warn.html">warn</a> and <a href="http://perldoc.perl.org/functions/die.html">die</a> by redefining the signals that Perl sends when you call those functions. You probably don&#8217;t want to use the signal from <a href="http://perldoc.perl.org/functions/die.html">die</a>, though, since it might mean a couple of different things.</p>
<p>You handle these special signals by setting values in the <code>%SIG</code> hash just like you would for any other signal. Since these are internal to <code>perl</code>, the signal names have underscores around them to distinguish them from external signal names:</p>
<pre class="brush:perl">
$SIG{__WARN__} = \&#038;some_Sub;
$SIG{__DIE__} = sub { ... };
</pre>
<p>I&#8217;ll treat the <code>__WARN__</code> in a different Item, because even though these look like they should do the same sort of thing, they really don&#8217;t. The <a href="http://perldoc.perl.org/functions/warn.html">warn</a> prints a message and lets the program continue. The <a href="http://perldoc.perl.org/functions/die.html">die</a> is designed to kill the program (hence, it&#8217;s morbid name) by starting a sequence of events that leads to the program&#8217;s shutdown. However, <a href="http://perldoc.perl.org/functions/die.html">die</a> became Perl&#8217;s fake exception mechanism, now used more to signal an error but without the intent to stop the program. Most of what <a href="http://perldoc.perl.org/functions/die.html">die</a> is supposed to do is discarded by catching it in an <a href="http://perldoc.perl.org/functions/eval.html">eval</a> now:</p>
<pre class="brush:perl">
eval {
	...;
	die( ... );
	...;
	};
if( $@ ) { ... }
</pre>
<p>This still triggers the <code>__DIE__</code> signal even though Perl knows that you aren&#8217;t going to exit the program. This means that if you want to handle <code>__DIE__</code> on your own, you need to distinguish between those cases where you should shut down the program, which seem to be the rare uses now, and the ones inside an <a href="http://perldoc.perl.org/functions/eval.html">eval</a> that should not shutdown the program. You don&#8217;t want to, for instance, start cleaning up database connections, logging, and other resources if the program is going to magically recover saying &#8220;I&#8217;m not dead yet!&#8221;.</p>
<p>There&#8217;s a Perl special variable that can help. The documentation for <a href="http://perldoc.perl.org/functions/die.html">die</a> recommends that you start your <code>__DIE__</code> handler by checking <code>$^S</code> before you continue. Inside the handler, the special handler is turned off so <a href="http://perldoc.perl.org/functions/die.html">die</a> inside <code>$SIG{__DIE__}</code> is the real <a href="http://perldoc.perl.org/functions/die.html">die</a>. The value of <code>$^S</code> will be true if it came from within an <a href="http://perldoc.perl.org/functions/eval.html">eval</a>, so this just re-throws the exception without handling it:</p>
<pre class="brush:perl">
$SIG{__DIE__} = sub {
	die @_ if $^S;
	...;
	};
</pre>
<p>This way, you&#8217;ll skip anything you might want to do to handle during a real shutdown.</p>
<p>There&#8217;s an additional twist. Perl might throw the <code>__DIE__</code> signal if there&#8217;s a problem during parsing. That is, you can actaully catch compilation errors:</p>
<pre class="brush:perl">
use strict;
use warnings;
use 5.010;

BEGIN { $SIG{__DIE__} = sub { print "Caught error: [@_]\n" } }

while( <> ) {
	next if m/(/; # compile-time error
	}
</pre>
<p>Now you get a double error message. You might not have meant to catch those sorts of errors, but you might.</p>
<pre class="brush:plain">
Caught error: [Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at test line 8.
]
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at test line 8.
</pre>
<p>The problem with these errors is that the parser might not have done everything you thought it should have done by tht point. Relying on anything, such as a subroutine definition is dangerous. The value of <code>$^S</code> will be undefined in this case, so your handler might now become:</p>
<pre class="brush:perl">
$SIG{__DIE__} = sub {
	die @_ if( $^S or not defined $^S );
	...;
	};
</pre>
<p>Skipping those two cases, the <a href="http://perldoc.perl.org/functions/eval.html">eval</a> and compile-time parsing, leaves you just with the case of actually exiting the program. However, there's another mechanism that might work better for you: the <code>END</code> block, which I covered in <a href="http://www.effectiveperlprogramming.com/blog/1068">Know the phases of a Perl program’s execution</a>. Anything in the <code>END</code> block executes as the program shuts down:</p>
<pre class="brush:perl">
END {
	...
	}
</pre>
<p>However, if you really need to catch calls to <code>die</code>, you can define <code>CORE::GLOBAL::die</code> (see <a href="http://www.effectiveperlprogramming.com/blog/514">Use CORE when you need the real thing</a>). This won't catch parsing errors, and will only affect calls to <code>die</code> (even in an <a href="http://perldoc.perl.org/functions/eval.html">eval</a>). To use it, you need to declare it before <code>perl</code> parses any of the <code>die</code> statements that you want to replace. You can do this in a <code>BEGIN</code> block:</p>
<pre class="brush:perl">
use strict;
use warnings;
use 5.010;

BEGIN { *CORE::GLOBAL::die = sub { say "In CORE::GLOBAL::die: [@_]" } }

eval {
	say "Inside eval";
	die 'Die-ing in eval';
	say "Past die in eval";
	};

die "This is the real die";

say "I got to the end!";
</pre>
<p>This version of <code>die</code> merely outputs a message, but without stopping the program. Thus, you see the message from the last <a href="http://perldoc.perl.org/functions/say.html">say</a> in the <a href="http://perldoc.perl.org/functions/eval.html">eval</a>:</p>
<pre class="brush:plain">
Inside eval
In CORE::GLOBAL::die: [Die-ing in eval]
Past die in eval
In CORE::GLOBAL::die: [This is the real die]
I got to the end!
</pre>
<p>Your new <code>die</code> doesn't stop the program, so you might put in an <a href="http://perldoc.perl.org/functions/eval.html">exit</a>:</p>
<pre class="brush:perl">
use strict;
use warnings;
use 5.010;

BEGIN {
*CORE::GLOBAL::die = sub {
	say "In CORE::GLOBAL::die: [@_]";
	exit(1);
	}
	}

eval {
	say "Inside eval";
	die 'Die-ing in eval';
	say "Past die in eval";
	};
if( $@ ) {
	say "Caught [$@]";
	}

die "This is the real die";

say "I got to the end!";
</pre>
<p>Now your program stops in the <a href="http://perldoc.perl.org/functions/eval.html">eval</a> because you call exit, but notice that it actually exits. That means there is no <code>$@</code> to catch. <a href="http://perldoc.perl.org/functions/eval.html">eval</a> does not catch an <a href="http://perldoc.perl.org/functions/eval.html">exit</a>:</p>
<pre class="brush:plain">
Inside eval
In CORE::GLOBAL::die: [Die-ing in eval]
</pre>
<p>If you want to get back to the real <a href="http://perldoc.perl.org/functions/die.html">die</a>, you can use <code>CORE::die</code>. For instance, you can die only if you are in an <a href="http://perldoc.perl.org/functions/eval.html">eval</a>, making it catchable, and <a href="http://perldoc.perl.org/functions/eval.html">exit</a> otherwise:</p>
<pre class="brush:perl">
use strict;
use warnings;
use 5.010;

BEGIN {
*CORE::GLOBAL::die = sub {
	say "In CORE::GLOBAL::die: [@_]";
	CORE::die( @_ ) if $^S;
	exit(1);
	}
	}

eval {
	say "Inside eval";
	die 'Die-ing in eval';
	say "Past die in eval";
	};
if( $@ ) {
	say "Caught: [$@]";
	}

die "This is the real die";

say "I got to the end!";
</pre>
<p>Now the program behavior is almost back to what you expect from the real <a href="http://perldoc.perl.org/functions/die.html">die</a>:</p>
<pre class="brush:plain">
Inside eval
In CORE::GLOBAL::die: [Die-ing in eval]
Caught: [Die-ing in eval at test line 8.
]
In CORE::GLOBAL::die: [This is the real die]
</pre>
<p>Now that you are this far, there are a few more tricks that you can play. For instance, you might want to catch all string arguments to <a href="http://perldoc.perl.org/functions/die.html">die</a> and turn them into objects. If they are already objects, you propagate them with some extra information. </p>
<p>Here's an example with a <code>MyError</code> class you can use as an error object. It knows how to stringify its object and it defines its own special <code>PROPAGATE</code> method to carry a message through several layers of <a href="http://perldoc.perl.org/functions/eval.html">eval</a>:</p>
<pre class="brush:perl">
use strict;
use warnings;
use Scalar::Util qw(blessed);
use 5.014;

package MyError {
	use overload '""' => \&#038;as_string;

	sub new {
		bless { message => $_[1] }, $_[0];
		}

	sub PROPAGATE {
		$_[0]->{message} .= "Passed through at file $_[1] line $_[2]\n";
		}

	sub as_string { "Stringified: <$_[0]->{message}>" }
	}

BEGIN {
*CORE::GLOBAL::die = sub {
	say "In CORE::GLOBAL::die: [@_]";
	$@ = do {
		if( blessed $@ and $@->can('PROPAGATE') ) {
			$@->PROPAGATE(__FILE__, __LINE__);
			}
		elsif( not blessed $@ ) {
			MyError->new( 'Made object: [' . join( ' ', @_) . "]\n" );
			}
		};
	CORE::die( $@ );
	}
	}

eval {
	eval {
		say "Inside eval";
		die 'Die-ing in eval';
		say "Past die in eval";
		};
	if( $@ ) {
		die $@;
		}
	};
if( $@ ) {
	say "Caught: [$@]";
	}

die "This is the real die";

say "I got to the end!";
</pre>
<p>Now the output is a layered mess of error messages within error messages, but you had quite a bit of control over those messages:</p>
<pre class="brush:plain">
Inside eval
In CORE::GLOBAL::die: [Die-ing in eval]
In CORE::GLOBAL::die: [Stringified: &lt;Made object: [Die-ing in eval]
>]
In CORE::GLOBAL::die: [Caught: [Made object: [Die-ing in eval]
Passed through at file test line 25
]]
Stringified: &lt;Made object: [Caught: [Made object: [Die-ing in eval]
Passed through at file test line 25
]]
>
</pre>
<h2>Things to remember</h2>
<ul>
<li><code>$SIG{__DIE__}</code> catches much more than a program's death
<li>Use <code>END</code> blocks for end-of-program cleanups if you can
<li>Override <code>CORE::GLOBAL::die</code> if you really need to replace <a href="http://perldoc.perl.org/functions/die.html">die</a>.
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie+http://tinyurl.com/3f38g7v" title="Post to Twitter"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-twitter2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie+http://tinyurl.com/3f38g7v" title="Post to Twitter"> </a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1310&amp;title=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Delicious"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="Post to Delicious" /></a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1310&amp;title=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Delicious"> </a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1310&amp;title=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Digg"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1310&amp;title=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Digg"> </a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1310&amp;t=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Facebook"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-facebook.png" alt="Post to Facebook" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1310&amp;t=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Facebook"> </a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1310&amp;title=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Reddit"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-reddit.png" alt="Post to Reddit" /></a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1310&amp;title=Override+die+with+END+or+CORE%3A%3AGLOBAL%3A%3Adie" title="Post to Reddit"> </a></p>]]></content:encoded>
			<wfw:commentRss>http://www.effectiveperlprogramming.com/blog/1310/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Git::CPAN::Patch to make quick patches to CPAN distributions</title>
		<link>http://www.effectiveperlprogramming.com/blog/1095</link>
		<comments>http://www.effectiveperlprogramming.com/blog/1095#comments</comments>
		<pubDate>Sun, 01 May 2011 21:09:20 +0000</pubDate>
		<dc:creator>brian d foy</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[item]]></category>
		<category><![CDATA[miscellany]]></category>

		<guid isPermaLink="false">http://www.effectiveperlprogramming.com/?p=1095</guid>
		<description><![CDATA[The Git distributed version control system is very popular in with Perlers, and even if you aren&#8217;t using it for your own project, you should know how to do simple things with it so you can interact with the most active parts of the community. It&#8217;s not that hard. Not only that, many Perl projects [...]]]></description>
			<content:encoded><![CDATA[<p>The <A href="http://www.git.org">Git distributed version control system</a> is very popular in with Perlers, and even if you aren&#8217;t using it for your own project, you should know how to do simple things with it so you can interact with the most active parts of the community. It&#8217;s not that hard. Not only that, many Perl projects are on  Github, and it&#8217;s something else you&#8217;ll know when you go to your next interview.</p>
<p>Git has many nice features, some of which people consider advantages over other source control paradigms. As a distributed system, you don&#8217;t need anyone&#8217;s permission to use it. You don&#8217;t have to ask anyone to set up a repository for you, and you can install the client software yourself. This isn&#8217;t a Git tutorial. You&#8217;ll find enough of those on the <a href="http://www.google.com/search?q=git+tutorial+scott+chacon">interwebs, especially by Scott Chacon</a>.</p>
<h2>Create your first repository</h2>
<p>Creating a git repository is easy. The repo itself carries everything that you need (that&#8217;s part of its distributed nature), so you don&#8217;t need to do much to create and use a repository. You don&#8217;t need anyone&#8217;s permission and you don&#8217;t need net access. Everything the repository needs is directly in the repositories directory, so once you are done with it, you can simply delete the directory without any additional cleanup.</p>
<p>Suppose that you wanted to start using Git with one of your Perl distributions. Change into its directory and create the repo: </p>
<pre class="brush:plain">
$ cd Some-Module-Distribution-Directory
$ git init
$ git add .
$ git commit -m 'initial import'
</pre>
<p>That&#8217;s it. Now you have a Git repository. The <code>init</code> creates the repository, the <code>add</code> tracks files (and the <Code>.</code> works on the current directory, tracking the directory and everything below). The <code>commit</code> makes it official.</p>
<p>The next step is to work on your Perl distribution. Isn't working on the Perl the point, anyway? You need to makes some changes to track changes. From that point, it's the basic Git workflow. You can <code>add</code> individual files and commit their changes:</p>
<pre class="brush:plain">
$ git add lib/Foo.pm
$ git commit -m 'Made things work'
</pre>
<p>You'll get some output:</p>
<pre class="brush:plain">
[master 3663fe3] Made things work
 1 files changed, 1 insertions(+), 0 deletions(-)
</pre>
<p>That number after <code>master</code> is the first part of the commit ID, a SHA-1 digest that not only identifies the particular commit, but everything that led up to it. Again, the Git tutorials will explain these things.</p>
<p>You just keep doing that until your software rules the world.</p>
<h2>Work on other distributions</h2>
<p>But you can use Git with Perl distributions that aren't yours. Since you don't have to do much work to create the repository, it makes sense to make temporary repositories for other people's distributions when you want to make a patch. IF you like, you can download the distribution, unpack it, and create the repository just like you did before:</p>
<pre class="brush:plain">
$ curl -O  http://www.cpan.org/authors/id/T/TU/TURNSTEP/Net-SSH-Perl-1.34.tar.gz
$ tar -xzf Net-SSH-Perl-1.34.tar.gz
$ cd Net-SSH-Perl-1.34
$ git init
$ git add .
$ git commit -m 'initial import of Net-SSH-Perl-1.34'
</pre>
<p>Now you have the module you want to patch in a Git repository, and you've saved the initial state of the repository. Make your changes, commiting as many times as you like along the way.</p>
<p>After you do your work, you can easily create patches. Check your log to find the commit where you want to start. The <code>--oneline</code> log format is useful just to see the commit digests and the short messages:</p>
<pre class="brush:plain">
$ git log --oneline
5cbbf58 Make delete part of the pipeline
33268fc Removed keyword
5efbedd Initial import of Net-SSH-Perl-1.34
</pre>
<p>Since you want to create the patches for the work you've done since the initial import, you tell <code class="binary">git</code> to create those patch files by specify where it should start by giving it the commit digest. There are many ways to specify how you want to create the patch, but this is easy enough:</p>
<pre class="brush:plain">
$ git format-patch 5efbedd
0001-Removed-keyword.patch
0002-Make-delete-part-of-the-pipeline.patch
</pre>
<p>There you are. You have one file per change (although you can do that differently as well). Those are the files you need to send off. If you're used to doing with with the <code class="binary">diff</code> tool, notice that you don't have to keep two copies of the distribution for comparison and you don't have to remember if the original directory comes first or second. Git does all of that work for you.</p>
<p>That's the basic idea, and that's easy, but it gets even easier.</p>
<h2>git-cpan</h2>
<p>Yanick Champoux created the <a href="http://search.cpan.org/dist/Git-CPAN-Patch/">Git::CPAN::Patch</a> distribution so you can easily interact with CPAN distributions with <code class="binary">git</code>, but especially so you can download the latest version, create a patch, and submit it while doing as little work as possible. Yanick first mentioned his work in a <A class="external" href="http://use.perl.org/~Yanick/journal/38180">Use.perl post</a> after he wrote an artile about it for <A href="http://www.theperlreview.com/"><i>The Perl Review</i> 5.1</a>.</p>
<p>His <a href="http://search.cpan.org/dist/Git-CPAN-Patch/">Git::CPAN::Patch</a> distribution installs several Perl programs that you'll use as regular <code class="binary">git</code> commands. You can even make your own <code class="binary">git</code> commands; installed in the right place (somewhere in PATH), a script named <i class="filename">git-foo</i> becomes the <code class="binary">git foo</code> you type at the command line.</p>
<table>
<tr>
<th>Script</th>
<th>Task</th>
</tr>
<tr>
<td>git-cpan-init</td>
<td>Create a git repository for a CPAN module</th>
</tr>
<tr>
<td>git-cpan-send-email</td>
<td>use git-send-email to submit patches to CPAN RT</th>
</tr>
<tr>
<td>git-cpan-sendpatch</td>
<td>create patch files and submit them to RT</th>
</tr>
</table>
<p>Suppose that you wanted to make a patch to <a href="http://search.cpan.org/dist/Module-Build">Module::Build</a>. Create a directory to hold your repository, change into it, then call <code class="binary">git cpan-init</code>:</p>
<pre class="brush:plain">
$ mkdir module-build
$ cd module-build
$ git cpan-init Module::Build
</pre>
<p>Now you have the latest version of <A href="http://search.cpan.org/dist/Module-Build">Module::Build</a> in a Git repository, making it easy for you to do work and later create the patch. So, do that. Perhaps you can fix one of the outstanding bugs in <a href="https://rt.cpan.org/Public/Dist/Display.html?Name=Module-Build">its RT queue</a>. As you go along, commit as normal.</p>
<p>After many commits, perhaps you're ready to submit a patch. Instead of creating a patch for each commit, you can combine them into one big commit. The <code class="binary">git cpan-squash</code> command takes your commits and turns them into a single commit in a new branch:</p>
<pre class="brush:plain">
$ git cpan-squash some_branch
</pre>
<p>After you do that, <code class="binary">git cpan-squash</code> leaves you on the branch that it created for you so you can do the final step: sending the patch to the RT queue. You don't even need to know anything about RT:</p>
<pre class="brush:plain">
$ git cpan-send-path
</pre>
<p>That's it. Now that you are done, you can keep the repository or get rid of it.</p>
<h2>Things to remember</h2>
<ul>
<li>You can use Git for lightweight, short term repositories.
<li>You can use Git to make quick patches to other people's distributions
<li><a href="http://search.cpan.org/dist/Git-CPAN-Patch/">Git::CPAN::Patch</a> installs git commands that interact with CPAN for you.
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions+http://tinyurl.com/3ehf6td" title="Post to Twitter"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-twitter2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions+http://tinyurl.com/3ehf6td" title="Post to Twitter"> </a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1095&amp;title=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Delicious"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="Post to Delicious" /></a> <a class="tt" href="http://delicious.com/post?url=http://www.effectiveperlprogramming.com/blog/1095&amp;title=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Delicious"> </a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1095&amp;title=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Digg"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://www.effectiveperlprogramming.com/blog/1095&amp;title=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Digg"> </a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1095&amp;t=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Facebook"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-facebook.png" alt="Post to Facebook" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://www.effectiveperlprogramming.com/blog/1095&amp;t=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Facebook"> </a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1095&amp;title=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Reddit"><img class="nothumb" src="http://www.effectiveperlprogramming.com/wp-content/plugins/tweet-this/icons/tt-reddit.png" alt="Post to Reddit" /></a> <a class="tt" href="http://reddit.com/submit?url=http://www.effectiveperlprogramming.com/blog/1095&amp;title=Use+Git%3A%3ACPAN%3A%3APatch+to+make+quick+patches+to+CPAN+distributions" title="Post to Reddit"> </a></p>]]></content:encoded>
			<wfw:commentRss>http://www.effectiveperlprogramming.com/blog/1095/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

