Practical Extraction and Reporting Language or Perl is an interpreter based language. Hash-bangs or shebangs are useful when we are executing Perl scripts on Unix-like systems such as Linux and Mac OSX. A Hashbang line is the first line of a Perl program and is a path to the Perl binary. It allows invoking the Perl scripts directly, without passing the file to the Perl as an argument. A Hashbang line in Perl looks like:
A Hashbang line is called so because it starts with a Hash(#) and a bang(!). A hashbang line in Perl holds significant importance in a Perl code. Now, let’s get started with the use of this Hashbang line.
Example: Suppose we have a hello world program script of Perl which we will execute on a Linux system with the terminal.
Now, we will save it with the name hello.pl and in the terminal, we will execute it by –
$ perl hello.pl
Here, in the above code, the terminal is running the Perl first and then the Perl is asked to run the code script. If the code script is run without invoking the Perl first then an error arises.
Try running the code as shown below:
Here, the shell we used, tried to interpret the commands in the file. However, it could not find the command print in Linux/Unix. Hence, there is a need to inform shell that it is a Perl script. This is the point where the concept of Hashbang comes into action. Hashbang informs the terminal about the script.
However, before executing this code, there is a need to set the path of the shell to add the current directory to existing directories. This can be done by executing the following command:
$ PATH = $PATH:$(pwd)
This will append the current working directory to the list of directories in the PATH environment variable.
Next, there is a need to add the Hash-bang line #!/usr/bin/perl in the Perl script file hello.pl. This line is always added at the beginning of the code i.e. the first line of the code script is the Hashbang line.
If the above code is run as $ hello.pl, without executing the Perl first, then the output will be:
Above code works fine and no error is produced because of the Hashbang line #!/usr/bin/perl added as the first line of the script.
When the script is executed it is run in the current shell environment. If the script starts with a hash and a bang (hash-bang) #! then the shell will run execute the application that has its path on the hash-bang line (in this case /usr/bin/perl) which is the standard location of the Perl compiler-interpreter. So, the hash-bang line holds the path to the Perl compiler-interpreter.
Now, the error occurs when there is no hash-bang line in the file and we try to execute it without running Perl explicitly. The shell assumes that the script is written in Bash and thus tries to execute it accordingly leading to errors.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!