Is that sample data you real data? How do you plan to solve the dependency loops (Task1 requires Task3, but Task3 requires Task1)? Also, Task2 requires Task2?
Anyways, my first thought is build a hash structure of the form
$deps{$task}{$requiredTask} .. so with the above you would have something like:
%deps = (
1 => { 2 => undef, 3 => undef },
2 => { 2 => undef, 4 => undef },
3 => { 1 => undef, 4 => undef },
5 => { 2 => undef, 3 => undef },
);
So now, to see if X requires Y, you can just do
exists $deps{$X}->{$Y}, or to list the deps (1 level) do
keys %{$deps{$X}} . From there you can loop through listing dependencies or make a recursive rountine to find all levels (but be very careful of the cyclic deps).
To build the above data structure, it could just be something like:
my %deps;
while(<STDIN>){
my @cols = split;
my $task = shift @cols;
$deps{$task}->{$_} = undef for @cols;
# or another way:
%{$deps{$task}} = map { $_ => undef } @cols;
}