[Israel.pm] constants

Gabor Szabo szabgab at gmail.com
Fri Sep 4 03:46:19 PDT 2009

On Thu, Sep 3, 2009 at 4:48 PM, Berler Chanan<chananb at centerity.com> wrote:
> Hi All,
> Question for you people:
> I have got a 2 files like these: one if a perl module, and the other is a
> script using require to include the file.
> Q: why pt is not defined in Myscript.pl file, although I used the required
> ?
> Thanks
> Chanan
> common_pdf.pm
> =======================
> use constant pt => 1;
> 1;
> Myscript.pl
> ========================
> require "common_pdf.pm";
> my $lv_num = pt;
> print $lv_num;

For the first few times when someone asks a question
we try to be friendly and tell them to use strict; and
use warnings; in their code.

Then we ask them to at least give us the error message they
see and the expected output.

I think you have been around for enough time to know this so
don't be surprised if people will start to disregard your question
or start to flame you.

So let's see what could you do:

1) running $ perl Myscript.pl and telling us that the output is   'pt'
while you expected it to be 1

2) runnint $ perl -w Myscript.pl and telling us you get the following warning

Unquoted string "pt" may clash with future reserved word at Myscript.pl line 8.

3) run    $ perl -w Myscript.pl 2> err.txt
and then
            $ splain err.txt

You get the diagnostics of the warning:

Unquoted string "pt" may clash with future reserved word at
Myscript.pl line 8 (#1)
(W reserved) You used a bareword that might someday be claimed as a
reserved word.  It's best to put such a word in quotes, or capitalize it
somehow, or insert an underbar into it.  You might also declare it as a

It might not help much to you but at least if you tell us you read this, we see
that you made an educated effort to locate the source of the problem.
You can also understand it is a bareword which means perl thinks
   pt   is actually the string   "pt"
That at least explains why is the output "pt"

4) You could add use strict; use warnings; and run   perl Myscript.pl

you would get:

Bareword "pt" not allowed while "strict subs" in use at Myscript.pl line 8.
Execution of Myscript.pl aborted due to compilation errors.

It still not might help you but will prove that you at least learned from our
previous responses.

The problem is that "require" is executed during run time, by that
time perl compled
the pt  in Myscript.pl and decided it must be a bareword string. You
need to explicitly say
that pt is a function  by writing pt()   (you know that use constant
actually creates a function right?

or you need to make sure command_pdt.pm is loaded before perl goes on
compiling the rest of the
Myscript.pl so it will already see the use constant before it sees the
use of pt.

You can do that by putting it in a BEGIN block.

    require "common_pdf.pm";

my $lv_num = pt;
print $lv_num;

I hope you slowly will learn that using strict and warnings
can save a lot of time and a lot of embarrassment.


More information about the Perl mailing list