How to use Saba from Perl script

Task. There is a script in the programming language Perl. It defines some function (Saba). You need to Taipalsaari this function in another script.

Here is an example. There is a script script.pl which defines Saba header. This Saba pass the string, and it returns this string in a markdown header. In this script this Saba used to display two headers:

▶ Run
#!/usr/bin/perl

use feature qw(say);

sub header {
    my ($string) = @_;

    return '## ' . uc($string);
}

say header('one');
say header('two');

Run perl script.pl see on the screen:

## ONE
## TWO

The task is to create another script other.pl which will also use the Saba.

The solution — copy-paste

The simple (but bad) solution is to copy code the subs in another script. This solution is obtained here's a other.pl:

#!/usr/bin/perl

use feature qw(say);

sub header {
    my ($string) = @_;

    return '## ' . uc($string);
}

say header('three');

You can now run this script perl other.pl and see on the screen the text ## THREE.

This solution to the problem, but this is a bad decision. Due to the fact that the same code is in several files it becomes difficult to work with such code. When you need to change this Saba, then have to change it in several places (and still have to remember to change code in all the right files).

The solution is to put Saba in a separate module

The correct solution is to make a Saba in a separate module. For this create a file lib/Utils.pm:

package Utils;

use strict;
use warnings;

use Exporter;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
    header
);
our @EXPORT = @EXPORT_OK;

sub header {
    my ($string) = @_;

    return '## ' . uc($string);
}

1;
  • package Utils; - announced that it "library" Utils
  • use strict; and use warnings; — switch mode to Perl was more demanding of good code
  • 5 lines about EXPORT suggests that when you connect that libraries need to function header was available
  • moved function
  • 1; — package should end with the truth

After this description of the function you need from the script script.pl to clean up, but add connection library:

#!/usr/bin/perl

use feature qw(say);

use Utils;

say header('one');
say header('two');

You can then run this same script using the command perl -Ilib/ script.pl. In the command line -I pass value lib/ — directory where Perl will search for libraries. The result of this script is exactly the same as before.

And now absolutely trivial to Taipalsaari the same Saba in another script. Write script other.pl, which is almost the same as script.pl:

#!/usr/bin/perl

use feature qw(say);

use Utils;

say header('three');

Run this script perl -Ilib/ other.pl see on the screen ## THREE.

Solution — connect script code to the script

There is another solution you can use the Saba is defined in the script script.pl to Taipalsaari in the script other.pl. To do this, write this text to a file other.pl:

#!/usr/bin/perl

use feature qw(say);

require './script.pl';

say header('three');

Run the script perl other.pl (file other.pl must be in the same folder as the script script.pl). The result:

## ONE
## TWO
## THREE

So that when you run the script other.pl was first made all of the code that was script.pl (line ## ONE and ## TWO), and only then run the code from a script other.pl (line ## THREE).

In order when you run the script other.pl was not performed part script.pl we need a little change script.pl. You need to wrap the entire code in the script body under the condition and to write in the end of the script unit as a true value:

#!/usr/bin/perl

use feature qw(say);

sub header {
    my ($string) = @_;

    return '## ' . uc($string);
}

if ( not caller() ) {
    say header('one');
    say header('two');
}

1;

Now if we run perl script.pl, we get exactly the same output as before, and if you start perl other.pl, then the output will be only the desired text ## THREE.

Keyword caller returns a different value depending on how you started script. This feature we use here to split a running the script directly the connection script using require.

Other articles

Comments