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

Im curious if anyone can explain why
require Carp; Carp::croak "This will produce a compile time error.";
wont compile '(Do you need to predeclare Carp::croak?)' but
use Carp; Carp::croak "This will compile fine.";
is fine and so is
use warnings; require Carp; Carp::croak "And so will this.";
I know that warnings does the use Carp; so the two are the same really, but why should it make a difference if Carp is used or required when the usage is via a fully qualified function call? I mean using a different module as the example works fine, so whats with Carp?

require Data::Dumper; print Data::Dumper::Dumper([qw(This is good too)]);
Thanks in advance...

Yves / DeMerphq
--
When to use Prototypes?
Advanced Sorting - GRT - Guttman Rosler Transform

Replies are listed 'Best First'.
Re: 'require Carp;' has differnet meanings under 'use warnings;'?
by demerphq (Chancellor) on Mar 12, 2002 at 15:06 UTC
    So thanks to arturo and tye I got to the bottom of this.

    The reason for the difference is that 'require' is a runtime directive whereas 'use' is a compile time directive. Now since I left off the parens on the croak the croak needs to be defined for perl not to get confused. So the way to solve this problem is either use the parens (best choice I would say) or to wrap the require statements in a BEGIN block.

    *sigh* So many subtleties, so little time...

    BTW: tye provided a little quote in the CB that I think is useful so Ill repeat

      tye: don't get confused into thinking that there is one "compile time" and one "run time". Each line (or less) of code can have different compile/run times
    Thanks all.

    Yves / DeMerphq
    --
    When to use Prototypes?
    Advanced Sorting - GRT - Guttman Rosler Transform

      So the way to solve this problem is either use the parens (best choice I would say) or to wrap the require statements in a BEGIN block.

      Is there a reason for not wanting to use Carp, instead of require it? If it's the importing, you could simply supply an empty list:

      use Carp ();

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: 'require Carp;' has differnet meanings under 'use warnings;'?
by VSarkiss (Monsignor) on Mar 12, 2002 at 15:44 UTC

    Just to add fuel to the fire: your first example will work fine if you use parentheses, like so:

    require Carp; Carp::croak("This works fine also");
    If Perl has compiled a sub, you can drop the parentheses when calling it. Otherwise you need to leave them in. But let's not get into prototypes....