#! /usr/bin/perl use strict; use warnings; use Tie::File; my $file = 'data.txt'; tie my @file, 'Tie::File', $file or die "tie '$file' failed.\n"; while ( defined $file[-1] && $file[-1] !~ m/\{$/ ) { pop @file; } untie @file;