I read all of the replies to the node
What can we assume in a BEGIN block ? and although I think I understand them, it does not help me understand the following:
Why, when compiling this code, using strict, does the compiler complain about the symbols not being defined?
#!/usr/bin/perl
use strict;
print keys %aa,", $bb, @cc\n";
BEGIN {our %aa=(1,11); our $bb=22; our @cc=(3,33); }
I thought that everything in the BEGIN block is parsed first, hence the symbol names (aa, bb and cc) are defined, before the rest of the code is parsed. Obviously I am wrong. Yet it also seems to
work if strict is turned off.
Output with use strict
Global symbol "%aa" requires explicit package name at test.pl line 3.
Global symbol "$bb" requires explicit package name at test.pl line 3.
Global symbol "@cc" requires explicit package name at test.pl line 3.
BEGIN not safe after errors--compilation aborted at test.pl line 8.
Output with
# use strict (no strict)
1, 22, 3 33
I am confused...
Sandy
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.