http://www.perlmonks.org?node_id=142710
Description:

This snippet was inspired by tye's post on this subject. The problem with most solutions is that they tend to be "all or nothing" unindenting. But what if you don't want unindenting (hint: use a regular HERE doc :), what if your code sometimes has tabs and sometimes has spaces for indentation? And some editors have tabs at four spaces, some at 8, etc. My solution is to provide an optional second argument to override the unindenting behavior. If you have better solutions, we could use 'em. Try the following samples with the sub:

#!/usr/bin/perl -w use strict; print unindent(<<'END'); test test2 END print unindent(<<'END', "(?: {4}|\t)"); test test2 END print unindent(<<'END', ''); I don't want this text to be unindented. END
sub unindent
{
    my ( $data, $whitespace ) = @_;
    if ( ! defined $whitespace )
    {
        ( $whitespace ) = $data =~ /^(\s+)/;
    }
    $data =~ s/^$whitespace//mg;
    $data;
}