Author image Sergey Poznyakoff


File::Backup - create a backup of the file.


    use File::Backup;

    $backup_name = backup($file_name);

    $backup_name = backup($file_name, BACKUP_NUMBERED);


The File::Backup module provides functions for creating backup copies of files. Normally, the name of the backup copy is created by appending a single ~ character to the original file name. This naming is called simple backup. Another naming scheme is numbered backup. In this scheme, the name of the backup is created by suffixing the original file name with .~N~, where N is a decimal number starting with 1. In this backup naming scheme, the backup copies of file test would be called test.~1~, test.~2~ and so on.


    $backup_name = backup($orig_name)
    $backup_name = backup($orig_name, $scheme)

The backup function is the principal interface for managing backup copies. Its first argument specifies the name of the existing file for which a backup copy is required. Optional second argument controls the backup naming scheme. Its possible values are:


Don't create backup.


Create simple backup (FILE~).


Create numbered backup (FILE.~N~).


Automatic selection of the naming scheme. Create numbered backup if the file has numbered backups already. Otherwise, make simple backup.

If the second argument is omitted, the function will consult the value of the environment variable VERSION_CONTROL. Its possible values are:

none, off

Don't create any backups (BACKUP_NONE).

simple, never

Create simple backups (BACKUP_SIMPLE).

numbered, t

Create numbered backups (BACKUP_NUMBERED).

existing, nil

Automatic selection of the naming scheme (BACKUP_AUTO).

If VERSION_CONTROL is unset or set to any other value than those listed above, BACKUP_AUTO is assumed.

The function returns the name of the backup file it created (undef if called with BACKUP_NONE). On error, it calls croak().

The following functions are available for using a specific backup naming scheme. These functions must be exported explicitly.


    use File::Backup qw(backup_simple);
    $backup_name = backup_simple($orig_name);

Creates simple backup.


    use File::Backup qw(backup_numbered);
    $backup_name = backup_numbered($orig_name);

Creates numbered backup.


    use File::Backup qw(backup_auto);
    $backup_name = backup_auto($orig_name);

Creates numbered backup if any numbered backup version already exists for the file. Otherwise, creates simple backup.