borisz has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
is there a way to find out, if a script is started with the -c switch?
perl -c -e 'BEGIN { print "started with -c\n" if somthing }' perl -e 'BEGIN { print "started with -c\n" if somthing }'
Boris

Replies are listed 'Best First'.
Re: find out, if perl is started with the -c switch
by chb (Deacon) on Sep 23, 2005 at 09:34 UTC
    check man perlvar
    perl -c -e 'BEGIN { print "started with -c\n" if $^C }' perl -e 'BEGIN { print "started with -c\n" if $^C }'
      Thanks, thats exactly what I searched for!
      Boris
Re: find out, if perl is started with the -c switch
by Excalibor (Pilgrim) on Sep 23, 2005 at 09:37 UTC

    Try checking for the value of variable $^C...

    $ perl -c -e 'BEGIN { print "$^C\n" }' 1 -e syntax OK $ perl -e 'BEGIN { print "$^C\n" }' 0 $

    See perlvar for more info.

    Good luck,

    --
    our $Perl6 is Fantastic;

Re: find out, if perl is started with the -c switch
by hv (Prior) on Sep 23, 2005 at 12:13 UTC

    If you need to know this because you are grabbing resources in BEGIN blocks and want to avoid doing that when it is just a compile check, it would probably be better to turn the BEGIN block into an INIT block instead (which is executed only after compilation is complete, at the beginning of the runtime phase), thus avoiding the need for the check.

    See perlmod for the gory details about BEGIN, CHECK, INIT and END blocks.

    Hugo

      INIT blocks, hmmm, that reminds me, there was a thread on this sometime back: Which phase am I in?. In that thread, tilly pointed out that that require'ing a module will skip the INIT blocks:

      #! /usr/local/bin/perl -w use lib '.'; require 'foo.pm'; print "running\n"; # foo.pm package foo; INIT { warn "foo init\n" } 1;

      Running the above produces...

      Too late to run INIT block at foo.pm line 2.

      ... which may be something for the OP to keep in mind.

      • another intruder with the mooring in the heart of the Perl

      That is what I have in mind. But the modul in question is loaded with use so $^Cis my rescue.
      Boris