#!/usr/bin/perl use strict; use warnings; my $token="DATA"; while (){ next if /^__(\w+)__\s*$/ and $token=$1; print "$token:$_" } __DATA__ data1 __HEHE__ hehe1 hehe2 __HUHU__ huhu1