in reply to Re^5: Using import to generate subroutines
in thread Using import to generate subroutines

This is wrong. Strict subs and vars are compile time but strict refs are handled at runtime. Its used by the dereferencing opcodes so while you don't see an opcode for strict refs itself, it is still used. In fact, it can only be effective at runtime.

Also, every time you say use ... you run the module's import() method. It happens at compile time but it certainly isn't a once-only thing.

In regard to revdiablo's concern, the right thing to do is to put it inside the loop. It doesn't exert any additional overhead and definately should be used without having extra, confusing scopes.

Replies are listed 'Best First'.
Re^7: Using import to generate subroutines
by fergal (Chaplain) on Nov 25, 2004 at 00:17 UTC
    Of course they're checked at run time, as you say, it can only be effective at run time. I was simply pointing out that no strict 'refs' is not interpreted or executed at run time. The interpreter does not even know that there was such a line in the program, all it knows is that certain operations have had their strict refs bit set to 1 and others haven't. This was probably done using strict but it could also have been doneusing some of the B modules, there's no way to know.

    The point being that, for example this code

    print $$f; { no strict 'refs'; print $$f; print $$g; } print $$f;
    is not compiled down to a series of ops like
    print $$f; switch on the strict refs flag; print $$f; print $$g; switch off the strict refs flag; print $$f;

    Every dereference op has a flag on it to say whether it should be strict or not so it is in fact compiled down to something like

    print ( dereference $f allowing strings ); print ( dereference $f not allowing strings ); print ( dereference $g not allowing strings ); print ( dereference $f allowing strings );

    The no strict 'refs' does not perform any action at all at run time and so it's position relative to loops is irrelevant.

    Also, every time you say use ... you run the module's import() method. It happens at compile time but it certainly isn't a once-only thing.
    I'm not sure what your point is, I think you have misintepreted
    any use statement only gets run once no matter where it is

    All I meant by this was

    use MyModule; # this will be run once at compile time print "hello\n"; use MyModule; # this will be run once at compile time for (1..10) { use MyModule; # this will be run once at compile time print $_; }

    Each of the 3 use statements are run once only. If they were require statements then the 3rd one would be run 10 times because when you're dealing with require it does matter where it is.

      Ok, you're absolutely correct. I disagreed by accident. I though you had said something else.