muizelaar has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks I would like to create symbolic links to a list of files within a directory but would like to give the symbolic links a different name to the files that are located in the directory. Here is what I have be playing around with so far. My error occurs around line 21 & 22 with an uninitialized value but cant seem to see what's wrong?
#!/usr/bin/perl -w use diagnostics; use strict; use Cwd; my $dir = getcwd; my $path = "/home/test/mytest"; my %ext_map = ( '.img' => '_trn.img' ); opendir(BIN, $path) || die "cant open folder: $!"; my @array = grep { -d "$path" } readdir BIN; foreach my $file (@array) { my $new_ext = $ext_map{$ext} || $ext; my $new_filename = $old_filename . '_new' . $new_ext; symlink("$path/$old_filename$ext", "$path/$new_filename" ) || d +ie "cant create link: $!\n"; }

Replies are listed 'Best First'.
Re: adding symbolic links with new name.
by Corion (Patriarch) on Feb 29, 2008 at 11:27 UTC

    The code you posted does not even have 21 lines, so it would be better to post the code you're actually using, or to use the code you actually post. The code as posted does not even compile - $old_filename is never declared. On the other hand, $file is never used.

    Most likely though, you will find some help if you change your error message to be more informative:

    symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "can't create link from $path/$old_filename$ext to $path/$n +ew_filename : $!\n"; }

    ... except that you don't even tell us whether your program works or not.

Re: adding symbolic links with new name.
by ikegami (Patriarch) on Feb 29, 2008 at 11:27 UTC

    My error occurs around line 21 & 22 with an uninitialized value

    There aren't that many lines, what you posted doesn't even compile. $ext and $old_filename are neither declared nor given a value. Try again...

Re: adding symbolic links with new name.
by jwkrahn (Abbot) on Feb 29, 2008 at 12:24 UTC
    • my @array = grep { -d "$path" } readdir BIN;

      $path is a directory so the expression -d "$path" is always true so @array will contain all directory entries including non-directories.    You need to include the file name in the test for it to work correctly:

      my @array = grep { -d "$path/$_" } readdir BIN;

    • my $new_ext = $ext_map{$ext} || $ext;

      You don't define the variable $ext anywhere or give it a value.