How to rename a file in Perl
Overview
The rename() method renames a file in Perl.
Syntax
rename(currentName, newName)
Parameters
currentName: This is the current file’s name.newName: This is the new name we want to give the file.
Return value
The value returned is either 1 or 0. 1 is returned if the process was successful, and 0 is returned if it was unsuccessful.
Code example
# rename files and print resultsprint rename("main.perl", "app.perl");print "\n";print rename("test.txt", "newtest.txt");
Explanation
- We have already created our application code file
main.perl. - In line 2, we try renaming our
main.perlusing therename()function and print the result. This is successful. Hence,1is returned. - In line 3, we try renaming a file we did not create. This returns
0because it was not successful.