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

Hi, I need a help with renaming files which begins "sniffer" in directory.
opendir(DIR, '.') || die "Couldn't opendir: $!\n"; foreach my $filename (reverse((grep /^sniffer/, readdir(DIR)))) { my $new_name = $filename; if($new_name =~ m/(\d+)/) { my $value = $1 +1; $new_name =~ s/.*/"sniffer$value\.log"/; } else { $new_name =~ s/.*/sniffer1.log/; } rename($filename, $new_name) unless $filename eq $new_name; close(DIR);

My aim is to increment number in files which begin "sniffer". In example: sniffer.log will be sniffer1.log, sniffer1.log will be sniffer2.log, etc. The oldest file (with the highest number) must be changed at the beginning - this is the reason of using reverse function in foreach loop.

Unfortunately I received this error:

Insecure dependency in rename while running with -T switch at ...

If anyone knows the solution of this problem or the easiest way how to increment files number ?

Thanks in advance ;)

Replies are listed 'Best First'.
Re: Problem with renaming files in directory
by Ratazong (Monsignor) on Jan 03, 2012 at 15:40 UTC

    Not related to the error-message you get. But why do you use

    $new_name =~ s/.*/sniffer1.log/;
    instead of
    $new_name = "sniffer1.log";
    And why do you close DIR inside the foreach-loop?

    Rata (scratching his head)
Re: Problem with renaming files in directory
by toolic (Bishop) on Jan 03, 2012 at 15:50 UTC
    If I don't use the -T switch on my shebang line, the script renames files without that error. Do you really need -T (perlsec)?

    Also, I get quotes in the file names. To avoid that, you should change:

    $new_name =~ s/.*/"sniffer$value\.log"/;
    to:
    $new_name = "sniffer$value.log";
    Also, readdir may not return the order you expect if you have double digits.
      ok, thanks for replies guys :) Right now my code is:
      foreach my $filename (reverse((grep /^sniffer/, <*>))) { my $new_name = $filename; if($new_name =~ m/(\d+)/) { my $value = $1 +1; $new_name = "sniffer$value.log"; } else { $new_name = "sniffer1.log"; } rename($filename, $new_name) unless $filename eq $new_name; }

      I didn't run with -T switch and I have all the time the error. Do you have any other hints ? :)

        Using diagnostics reveals this info:
        Insecure dependency in rename while running with -T switch at (F) You tried to do something that the tainting mechanism didn't l +ike. The tainting mechanism is turned on when you're running setuid or setgid, or when you specify -T to turn it on explicitly. The tainting mechanism labels all data that's derived directly or indi +rectly from the user, who is considered to be unworthy of your trust. If + any such data is used in a "dangerous" operation, you get this error. + See perlsec for more information. Uncaught exception from user code: Insecure dependency in rename while running with -T switch at
        Since you are not explicitly using -T, look into setuid or setgid.

        By the way, changing readdir to glob will not solve the double-digit filename issue.