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

I'd like to run perltidy on some files that are sym links, but with the -b option, perltidy renames the sym link (appends .bak) and creates a new tidy'd up file.

This is a problem when the original sym link file is under version control, plus the documentation says -b modifies in place (not quite true in this case.)

I know one answer is "don't tidy sym links," are there other options?

Replies are listed 'Best First'.
Re: Perltidy with symbolic links
by ikegami (Patriarch) on Mar 10, 2010 at 19:14 UTC

    Resolve the link before passing it to perltidy.

    for q in * ; do perltidy $(readlink -e "$q") ; done

    You could also modify perltidy to resolve links using Cwd's realpath.

    In both cases, the backup will appear next to the linked file.

Re: Perltidy with symbolic links
by ikegami (Patriarch) on Mar 10, 2010 at 19:25 UTC

    plus the documentation says -b modifies in place (not quite true in this case.)

    Truly modifying in place can require lots of memory (since you can't insert into a file, just overwrite), and it fails bad (you lose data if an error occurs halfway through). You should expect "in place" to mean the final result is stored in a file by the same name as the original.

    This affects symlinks, and it can also affect permissions. Perhaps a better solution would be to copy the original, then read from the backup and overwrite the original. I know some were interested in changing Perl's -i, but I don't know if that's one of the changes they had in mind. It would slow things down.

Re: Perltidy with symbolic links
by zwon (Abbot) on Mar 10, 2010 at 19:12 UTC

    Something like

    perltidy script.pl && cat script.pl.tdy >script.pl && rm script.pl.tdy

    Note, that it doesn't create backup.

    Update: another approach is to dereference symlink:

    perltidy -b `readlink script.pl`
      Thanks, no backup isn't much of an issue since the file is under version control anyway.