1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#! /usr/bin/perl use strict; use warnings; use Tie::File; # Filename must be the script's first argument my $filename = shift; die "Please specify filename to be modified!\n" if ! defined $filename; die "'$filename' does not exist...\n" if ! -e $filename; die "'$filename' is a directory...\n" if -d _; die "File '$filename' is not writable...\n" if ! -w _; tie my @lines, 'Tie::File', $filename or die "Cannot tie '$filename': $!\n"; # remove last 5 elements of @lines my @move = splice @lines, -5, 5; # add removed elements in the beginning of @lines unshift @lines, @move; untie @lines;
perl -wE'@l=qw(1 2 3 4 5 6 7); splice(@l,5,0,"x", "y");say "@l"'
Guest wer(Tippfehler in Code-Zeile von mir korrigiert.)ersetze
unshift @lines, @move;
durch
splice(@lines,11,0,@move);