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

In keeping with the spirit of self-security, I'm curious about the viability of an idea where CGI::Carp can be used to choose whether to output perl errors to the browser based on the existence of a cookie or an IP address. This part is irrelevant--the problem is that I can't get perl to import conditionally. Here, the idea is that the typical perl cgi script might start with:

BEGIN { use CGI; $query = new CGI; $user = $query->cookie('acct'); if ($user eq "foobar") { use CGI::Carp qw(fatalsToBrowser); # ALWAYS IMPORTS } }

The problem is that perl sees the fatalsToBrowser line and imports the function, regardless of the if statement. I can't seem to conditionally choose to import the function.

ideas?

Replies are listed 'Best First'.
Re: "Safe" ways for Carp to report errors to browser?
by dave_the_m (Monsignor) on Jan 09, 2005 at 19:34 UTC
    use CGI::Carp qw(fatalsToBrowser)
    replace that with
    require CGI::Carp; import CGI::Carp qw(fatalsToBrowser);

    Dave.

      Of course! "Doh!" on me...

      FWIW, the reason I overlooked this might be a common one that others (doing a search) might also overlook, so I think it deserves a tad clarification --please correct me if I'm wrong on this:

      "require" waits until runtime to bring the code in, unlike "use", which compiles the function into place before running. Hence, using "use" as I did causes the fatalsToBrowser() function to be called irrespective of the conditional in the if statement. The solution you showed, using the "require"/"import" combination, assures that the code doesn't get imported and used until runtime, allowing the conditional will be tested beforehand.

      The reason this eluded me in the first place is because this is all taking place within a BEGIN block, which is executed at compile-time, not runtime. Here, it would have seemed irrelevant whether to use use or require, till I realized that all I'm doing is defining the function, set_message(). This function isn't getting called yet--it's just being defined. Later, if there are syntax errors that perl's compile phase will catch, then set_message() is called, which will then do the test, which then determines whether to imports fatalsToBrowser().

      If my explanation is accurate, it could be useful for people who are looking to learn the subtle nuances between use and require.

        Well, I don't quite follow your explanation (I'm not sure where set_message() appears - it's not in your sample code), so instead I'll just describe what happens.

        When perl gets to the end of compiling a BEGIN block, it temporarily stops compiling, executes the code in the block, then continues compiling. Similarly, when Perl sees a 'use' while compiling, it temporarily stops the current compile, reads in the file referenced by the 'use', compiles it, executes it, calls the import function(), then continues off compiling at the statement following the 'use'.

        In your original code, the Perl interpreter starts compiling the code in your BEGIN block, sees the 'use', reads in and compiles the file, calls its import method, then continues compiling the rest of the code in the BEGIN block. (At this point, the import has already affected the current package's symbol table for any subsequent code). Finally the end of the BEGIN block is reached, compilation pauses and the BEGIN block is executed.

        At this point your if statement is executed, which by now has been optimised away to a noop, since it has an empty body (since 'use' is invisible at runtime).

        Dave.