Perl: Difference between revisions

imported>Nix
add see also link
imported>Nix
example using nix-shell in script
Line 1: Line 1:
==Running a Perl script==
== Running a Perl script ==


Common problems and solutions:
=== Replacing #! with nix-shell ===
 
Perl scripts normally start something like this:
 
<syntaxHighlight lang=shell>
  #!/usr/bin/env perl
</syntaxHighlight>
 
In Nix, we often make isolated environments using [https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html nix-shell]. You can do this in the <code>#!</code> (shabang) section directly in the script too. Here is an example from the manual &mdash; a Perl script that specifies that it requires Perl and the HTML::TokeParser::Simple and LWP packages:
 
<syntaxHighlight lang=perl>
#! /usr/bin/env nix-shell
#! nix-shell -i perl -p perl perlPackages.HTMLTokeParserSimple perlPackages.LWP
 
use HTML::TokeParser::Simple;
 
# Fetch nixos.org and print all hrefs.
my $p = HTML::TokeParser::Simple->new(url => 'http://nixos.org/');
 
while (my $token = $p->get_tag("a")) {
    my $href = $token->get_attr("href");
    print "$href\n" if $href;
}
</syntaxHighlight>
 
=== Invoking nix-shell on command-line ===


<code>Can't locate DB_File.pm in @INC (you may need to install the DB_File module)</code>: run it with <code>nix-shell -p perl -p perlPackages.DBFile --run ./myscript.pl</code>
<code>Can't locate DB_File.pm in @INC (you may need to install the DB_File module)</code>: run it with <code>nix-shell -p perl -p perlPackages.DBFile --run ./myscript.pl</code>