Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl
file
rename
communitycreator

How to rename a file in Perl

okwudili onyejiaku

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 results
print rename("main.perl", "app.perl");
print "\n";
print rename("test.txt", "newtest.txt");
Renaming a file in Perl

Explanation

  • We have already created our application code file main.perl.
  • In line 2, we try renaming our main.perl using the rename() function and print the result. This is successful. Hence, 1 is returned.
  • In line 3, we try renaming a file we did not create. This returns 0 because it was not successful.

RELATED TAGS

perl
file
rename
communitycreator
RELATED COURSES

View all Courses

Keep Exploring