Synax
- A Perl script typically starts with:
- #!/usr/bin/perl: This shebang line tells the system where to find the Perl interpreter.
- use strict;: Helps catch common coding errors by enforcing strict variable declaration.
- use warnings;: Warns about potentially problematic code, improving debugging.
Excecution
1.Execute with the Perl Interpreter
perl script.pl
2. Using the Shebang Line
Steps:
- Make sure the first line of your script specifies the Perl interpreter:
#!/usr/bin/perl
- Make the script executable by setting the execute permission:
chmod u+x script.pl
- Run the script by typing:
./script.pl
3. Directly from the command line
- You can execute short Perl code directly from the command line without creating a file.
- syntax:
- Output:
Hello, World!
Commenting in perl
- '#' is used to comment a single line.
- Using 'begin' and 'cut' for commentinng multiple lines.
Double Quote and Single Quote
- Single quote:
- Perl treats the contents of single-quoted strings as-is.
- Escape sequences like \n (newline) and \t (tab) are not interpreted within single quotes.
- Variable substitution is not allowed.
- Example:
- Output of above code:
Hello, $name\n
2. Double quotes:
- Strings in double quotes allow variable substitution and escape sequences. This means that any variable inside the string will be replaced with its value, and escape sequences like \n and \t will be interpreted.
- Example:
- Output of above code:
Hello, Alice (with a newline after)
- Example including both:
- Output of above code:
value of a is 10
value of a is $a\n