tschutter
tschutter

Reputation: 29

What is $^C in Perl?

I am trying to understand this code in Perl that I have inherited:

sub myfunc($)
{
    my ($path) = @_;
    unless ($^C)
    {
        # manipulate path
    }
    return $path;
}

But I do not understand the $^C test. What does it do?

Upvotes: 0

Views: 226

Answers (1)

brian d foy
brian d foy

Reputation: 132783

Perl's -c switch compiles the source and tells you if the syntax is valid. However, Perl doesn't have a strict separation between compile-time and run-time. You can run some code during the compilation (BEGIN blocks), and you can compile code when you are running (eval()).

This means that you might think that you are safe while syntax checking, and that you would be wrong. The second example actually runs some code:

$ perl -c -le 'print "Hello!"'
-e syntax OK

$ perl -c -le 'BEGIN{print "Hello!"}'
Hello!
-e syntax OK

This is more important in IDEs. Many of these will continually run perl -c on your source so it can highlight problems. However, this also means that your IDE is now potentially a trojan horse for executing that code you just copied and pasted from Stackoverflow.

Thus, the $^C guard (see perlvar):

$ perl -c -le 'BEGIN{ print "Hello!" unless $^C }'
-e syntax OK

I'm guessing that the developer who put that there either had a bet to use all the special variables in one program, or was reacting to unwanted behavior from their tools.

Upvotes: 6

Related Questions