The scoping of 'use Carp' and 'use strict' is the same. The problem was that your code didn't use the effects of 'use Carp'. The following compiles:
use strict; use warnings; use Carp qw(croak); package Test; sub croak_test { Carp::croak "this is a test"; } Test::croak_test;
You were calling Test::croak, which doesn't exist, so the parser sees no reason to accept an unparenthesized string constant after it.

So use Carp qw(croak) must be declared after the 'package' only so that will create the function you're actually trying to call. It could go earlier, if you wish: you could insert

{ package Test; use Carp qw(croak); }
at any point before the 'package', and it would create the correct subroutine and allow you to call it without parens. Heck, you could just put sub Test::croak; at any point in the file before the call to croak and it would be happy.

(Well, if you don't define the function, it'll complain if you ever try to call Test::croak, but at least it'll compile.)


In reply to Re: Use of Carp outside of package won't compile... by sfink
in thread Use of Carp outside of package won't compile... by memnoch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.