This idiom is described in the perlrun documentation. The -x switch scans the entire file and ignores everything that appears before the first line starting with #! , and also contains the word perl . This means that your system will run the script using the Perl interpreter, whether you call the script using perl or using the shell command ( sh/bash/ksh/ etc.). I.e
$ perl this_script
and
$ sh this_script
both will run the script using perl .
To solve the second question, this idiom has nothing to do with combining the shell script and the Perl script in the same file. There are several ways to approach this problem, but perhaps the most readable way is to write a script in the shell, but using the shell's heredoc notation to invoke Perl code.
#!/bin/bash
source share