---
title: "Perl 字符串定长分割"
author: "fcmfcm01"
date: "2012-02-24T14:34:22Z"
tags: ["Perl", "分割", "字符串"]
categories: ["编程语言"]
slug: "perl"
draft: false
lastmod: "2012-02-24T14:34:22Z"
id: 1422137
url: "https://fcmfcm01.iteye.com/blog/1422137"
---

**Perl 字符串按照给定的长度分割并返回数组**

 

```
sub splitStr {
	my ( $strtmp, $length ) = @_;
	my $strLength = length $strtmp;
	my @results;
	for ( my $i = 0 ; $i < $strLength ; $i += $length ) {
		#if length reach the bound , just resturn the left ones 
		if ( $strLength < ( $i + $length ) ) {
			push @results, substr( $strtmp, $i );
		}
		else {
			push @results, substr( $strtmp, $i, $length );
		}
	}
	return \@results;
}
```