Getting script name from the file

Top Japanese page




Overview

Recognize file name as script name and use it as $script in the script and use it in whereever in the script.

Flow

  1. Get the script name
  2. Get rid of the path information from the script name
  3. Put it into a variable

A sample code

 use File::Basename;
 
 my $script = basename($0);

Description of the code

 use File::Basename;

Load File::Basename;

 my $script = basename($0);

When the perl command is executed, $0 contains the script name itself. By using basename method, it extracts command name only. i.e. exclude the path name. As a result, $script will have the script name only.