Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

directory
perl
communitycreator

What is dirname() method in Perl?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The dirname() method in Perl is used to get the directory of the folder name of a file.

Syntax

dirname(file_path)

Parameters

file_path: The path of the file we want to get its directory name.

Return value

This method returns the folder or directory name or a symbolic link that contains the file.

Example

Let’s look at the code below:

# import sub routine
use File::Basename;
# get file paths
$f1 = dirname("hi/main.perl");
$f2 = dirname("main.perl");
$f3 = dirname("home/code/main.perl");
$f4 = dirname("~/main");
# print directories
print "$f1";
print "\n$f2";
print "\n$f3";
print "\n$f4";
Get the directory name of a file path in Perl

Explanation

  • Line 2: We import the FIle::basename subroutine, which helps us parse file paths.

  • Lines 5 to 8: We create some file paths and get their directory names using the dirname() method.

  • Lines 11 to 14: We print the directories to the console.

RELATED TAGS

directory
perl
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring