Develop a command line Perl program that takes a single positive integer as input from the user and calculates its factorial using recursion. The program should check whether the input is a positive integer and should handle invalid inputs gracefully with user-friendly error messages. The program should output the factorial of the input number and should include appropriate prompts and messages for a smooth user experience. The code should be well-structured, readable, and commented where necessary.
user
```
#!/usr/bin/perl

use strict;
use warnings;

# Function to calculate factorial using recursion
sub factorial {
    my ($n) = @_;
    if ($n <= 1) {
        return 1;
    }
    else {
        return $n * calculateFactorial($n-1);
    }
}

print "Enter a positive integer to calculate its factorial:\n";  # Prompt user to input number

# Get user input as a scalar
my $num = <STDIN>;
chomp $num;

# Check if input is a positive integer
if ($num =~ /^\d+$/) {
    if ($num >= 1) {
        # Calculate factorial and output result
        my $factorial = factorial($num);
        print "The factorial of $num is $factorial\n";
    }
    else {
        # Invalid input: number must be a positive integer
        print "Error: input number is not a positive integer\n";
    }
}
else {
    # Invalid input: must be a positive integer
    print "Error: input must be a positive integer\n";
}
```

This program first defines a function called `factorial` to calculate the factorial of a number using recursion. It checks whether the number is 1 or less (in which case the function returns 1), or greater than 1 (in which case it multiplies the number by the factorial of the next smaller number and recursively calls the function with that number).

The program then prompts the user to input a number as a scalar using `STDIN` and `chomp`, and checks whether the input is a positive integer using a regular expression match. If the input is valid and is a positive integer, the program calls the `factorial` function with the input number and outputs the result with an appropriate message. If the input is not a positive integer, the program prints an error message indicating that it must be a positive integer.
assistant
Develop a command line Perl program that takes a single positive integer as input from the user and calculates its factorial using recursion. The program should check whether the input is a positive integer and should handle invalid inputs gracefully with user-friendly error messages. The program should output the factorial of the input number and should include appropriate prompts and messages for a smooth user experience. The code should be well-structured, readable, and commented where necessary.
user
Here is a possible implementation of the program:

```perl
#!/usr/bin/perl

use strict;
use warnings;

# Prompt the user for an integer input
print "Enter a positive integer: ";

# Read and validate the input
my $n = <STDIN>;
chomp($n);
if (!($n =~ /^\d+$/) || $n <= 0) {
    die "Invalid input. Please enter a positive integer.\n";
}

# Calculate the factorial using recursion
sub factorial {
    my $n = shift;
    if ($n == 0) {
        return 1;
    } else {
        return $n * factorial($n - 1);
    }
}

my $result = factorial($n);

# Output the result
print "The factorial of $n is $result.\n";

```

Here's how the program works:

- The program prompts the user for an integer input using `print`.
- It then reads the input from `STDIN` and validates it using a regular expression that checks if it's a positive integer. If the input is invalid, it prints an error message using `die` and terminates the program. Otherwise, it proceeds to calculate the factorial.
- The `factorial` function is defined using recursion. If the input is 0, it returns 1. Otherwise, it recursively calls itself with the input decremented by 1 and multiplies the result by the input.
- The program calls the `factorial` function with the user input and stores the result in a variable.
- Finally, the program outputs the result using `print`.

Sample output:

```
Enter a positive integer: 5
The factorial of 5 is 120.
```

```
Enter a positive integer: -2
Invalid input. Please enter a positive integer.
```
assistant