in reply to Re^2: When is "use module" not the same as require+import?
in thread When is "use module" not the same as require+import?

It seems that you can get around the scoping issue by not leaving the BEGIN block:
BEGIN { my @fields = qw/field1 field2/; use fields @fields; } my $self = fields::new; $self->{field1} = 'value1';
works just fine for me. It may fairly be argued that this is not much different from putting a require and import inside the BEGIN, though.

UPDATE: ikegami correctly points out that I must not have tested what I thought I tested, because this code doesn't work. Sorry. I think Bloodnok's solution (adapted to use fields) works, though.

Replies are listed 'Best First'.
Re^4: When is "use module" not the same as require+import?
by ikegami (Patriarch) on Sep 19, 2008 at 19:20 UTC
    BEGIN { my @fields = qw/field1 field2/; use fields @fields; }
    is equivalent to
    BEGIN { my @fields = qw/field1 field2/; BEGIN { require fields; fields->import(@fields); } }
    which is compiled and executed as follows:
    1. my @fields = qw/field1 field2/; is compiled
    2. require fields; is compiled
    3. fields->import(@fields); is compiled
    4. require fields; is executed
    5. fields->import(@fields); is executed
    6. my @fields = qw/field1 field2/; is executed

    As you can see, an empty array is passed to the module. I believe you were mistaken when you claimed it worked.