#!perl

use strict;
use warnings;

use ScriptX 'Run';

sub run {
    my ($self, $stash) = @_;

    my $name;
    while (1) {
        print "Please enter your name: ";
        chomp($name = <STDIN>);
        if ($name =~ /\S/) {
            last;
        } else {
            print "Wrong response. ";
        }
    }

    my $gender;
    while (1) {
        print "Please enter your gender (M/F): ";
        chomp($gender = <STDIN>);
        if ($gender =~ /^[mf]$/i) {
            last;
        } else {
            print "Wrong response. ";
        }
    }

    print "Hello, ", ($gender =~ /m/i ? "Mr." : "Mrs."), " $name!\n";
}

ScriptX->run;

# ABSTRACT: Run a code
# PODNAME: scriptx-eg-run-sub

__END__

=pod

=encoding UTF-8

=head1 NAME

scriptx-eg-run-sub - Run a code

=head1 VERSION

This document describes version 0.000002 of scriptx-eg-run-sub (from Perl distribution ScriptX), released on 2020-10-01.

=head1 SYNOPSIS

 % script-eg-run-sub
 Please enter your name: Budi
 Please enter your gender (M/F): x
 Wrong response. Please enter your gender (M/F): m
 Hello, Mr. Budi!

=head1 DESCRIPTION

This script loads the L<ScriptX::Run> plugin to run something. The plugin
accepts a coderef (C<code>) or a string/array for external command (passed to
L<IPC::System::Options>'s C<system()>). Alternatively, it can search for
C<run()> in the C<main> package. In this script, we provide the third option.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/ScriptX>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-ScriptX>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=ScriptX>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
