9.3. Subroutines

Example 9-4. examples/subroutines/optional_params.p6

#!/usr/bin/perl6
use v6;

# search the text within a file return 1 if found, 0 if not
sub search ($text, $file) {
	my $fh = open $file, :r orelse die;
	for $fh.readline -> $line {
		if index($line, $text) > -1 {
			return 1;
		}
	}
	return 0;
}

# optional parameter
sub search($text, $file, $all?) {
	my $fh = open $file, :r orelse die;
	my $cnt = 0;
	for $fh.readline -> $line {
		if index($line, $text) > -1 {
			return 1 if not $all;
			$cnt++;
		}
	}
	return $cnt;
}

If you are interested in on-site trainings by the author, please contact me directly.

Online courses: