J. Linne J. Linne - 2 months ago 11
Perl Question

How to make Perl Generate Output files in a specific directory

Does anyone know how to use perl so output files can be generated automatically? I am running an input on the command line like this:

<<< nextprime(30659918)

30659947

But instead of the output "30659947" being in the command window, I am looking for a quick command that can generate an output file (such as Perl_command1.out in a given directory for example) with the characters "30659947" instead. Does anyone know how to do this? (Why I want to do this is so I do not have the command window racked up when I have larger 300k+ digit inputs)

The perl command I am using is:

perl -Mntheory=:all -E "say next_prime('30659918')

30659947

or the script file: perlscript.pl



#!/usr/bin/env perl
use warnings;
use strict;
use feature 'say';
use ntheory ":all";

say next_prime('30659918')




Answer

You can redirect output from the command line using operators. Another alternative is to redirect standard output to a file in perl script itself.

#!/usr/bin/env perl
use warnings;
use strict;
use feature 'say';
use ntheory ":all";

# open filehandle log.txt
open (my $LOG, '>>', 'log.txt');
# select new filehandle
select $LOG;

say next_prime('30659918');
Comments