-1

So basically what I mean is: is there a way for me to disable the function print's output when invoking a perl script at the command line? If not available in the base implementation of perl then maybe CPAN?

Things that I am aware of:

  • perl has a debugger
  • redirecting stdout to some file and then wiping it (useless in my case since I need STDOUT for something else)
  • having all lines that start with a print commented (doesn't scale, is ugly etc...)
  • redirecting in UNIX to dev/null

I am not referring to any warnings, exceptions, errors etc... Just the standard print function or any of it's very close siblings.

Similar to how you'd use C's #ifdef but at file scope and not having to write so much for so little.

I'm willing to install a cpan module in case it offers this kind of functionality.

I am limited to Perl 5.14.

user3046061
  • 353
  • 4
  • 14
  • 1
    http://perlmaven.com/stdout-stderr-and-redirection – Gabs00 Dec 08 '13 at 15:01
  • Redirection is the only sane solution: Unixes have `/dev/null`, Windows has `NUL` or `\Device\Null`. It is [not possible to override `print`](https://metacpan.org/pod/release/RJBS/perl-5.18.1/lib/CORE.pod). You can however define your own output function that may or may not print, depending on the context. – amon Dec 08 '13 at 15:14
  • It's not clear what you are trying to achieve. You mention `#ifdef` and you mention `>/dev/null`, but they are completely different. (e.g. `#ifdef` is used to create different binaries, but Perl doesn't create binaries.) Please state more clearly what you are trying to achieve and why the solutions you have found are not adequate. – ikegami Dec 08 '13 at 15:27
  • I know they're different but in this case they offer something similar. The code I'll be using has to work on both Windows and Linux. If push comes to shove I'll use /dev/null and NUL but I am welcoming any alternative, be it: CPAN module that offers what I'm describing in the OOP or some kind of workaround which @amon has cleared up that is not possible. In case someone posts a customized sub that uses print and offers this functionality I'll accept it. – user3046061 Dec 08 '13 at 15:31

1 Answers1

0

Assuming you are writing this script yourself, or can edit it, something like below will work.

use Getopt::Long;
our $VERBOSE = 0;
GetOptions ('verbose+' => \$VERBOSE);
# ...
xprint(1, "Yadda yadda yadda");
# ...
sub xprint {
    my ($pri, $msg) = @_;
    print("$msg\n") if $pri >= $VERBOSE;
}

EDIT:

Or without priority levels:

use Getopt::Long;
our $VERBOSE = '';
GetOptions ('verbose' => \$VERBOSE);
# ...
xprint("Yadda yadda yadda");
# ...
sub xprint {
    # can also be replaced with printf(@_) for more cowbell.
    print("$_[0]\n") if $VERBOSE;
}

EDIT:

As an aside, for #ifdef functionality, replace the whole getopt section with a simple constant:

use constant VERBOSE => 1;

And remove the $ from VERBOSE after the print statement:

print("$_[0]\n") if VERBOSE;
codnodder
  • 1,674
  • 9
  • 10
  • For the time being I won't be using any priority levels. Please change the name of the sub, I think log is taken already by something. – user3046061 Dec 08 '13 at 15:55
  • Thanks, I'll be extending the sub to match the kind of parameters I'm passing to it, so customizing it for arrays, hashes and other stuff. – user3046061 Dec 08 '13 at 16:24