Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Is strict too strict here?

by saintmike (Vicar)
on Jul 11, 2008 at 23:40 UTC ( [id://697117]=perlquestion: print w/replies, xml ) Need Help??

saintmike has asked for the wisdom of the Perl Monks concerning the following question:

Just stumbled across this interesting 'strict' complaint: Let's say I want a module that has a function eval_this() which takes a snippet of code and then eval's it in the main package:
package Foo; use strict; sub eval_this { my $code = shift; $code = "package main; \n$code"; eval $code; die ("Error: $@") if $@; } package main; our $somevar = "somevalue"; Foo::eval_this('print $somevar;');
Now, as you can see, $somevar has been declared properly as a global in main before the function was called, but yet 'use strict' throws a fatal runtime error:
Variable "$somevar" is not imported at (eval 1) line 2.
Error: Global symbol "$somevar" requires explicit package name at (eval 1) line 2.
Is there a way around this? 'no strict "vars"' would obviously shut strict up, but I would still like strict's variable name checking. Verified with perl-5.10.0.

Replies are listed 'Best First'.
Re: Is strict too strict here?
by ikegami (Patriarch) on Jul 11, 2008 at 23:52 UTC
    our is lexically scoped, so it's not in view of the eval and thus the code that references $somevar. Fix:
    Foo::eval_this('our $somevar; print $somevar;');
      Fix: Foo::eval_this('our $somevar; print $somevar;');
      Yeah, that would work, but that's unfortunately not an option, since I don't know beforehand which variables are used in the eval'd string (well, I guess I could walk main's symbol table and figure out all the declared globals, then add "our $this; our $that" in front of the string, but that's rather ugly.

      Any other ideas?

        You don't know which variable are used in the evaled string when writing the evaled string?

        Based on your proposed solution, you'd be happy with "no strict;".

Re: Is strict too strict here?
by jettero (Monsignor) on Jul 11, 2008 at 23:45 UTC
    No, that's correct. our is a scoped name-space global and your string is evaluated in a different scope. You can either put another our in the eval string or name the package in it (ie, print $main::somevar, in which case you don't even need the our).

    -Paul

Re: Is strict too strict here?
by tilly (Archbishop) on Jul 12, 2008 at 00:22 UTC
Re: Is strict too strict here?
by Joost (Canon) on Jul 12, 2008 at 00:27 UTC
    'no strict "vars"' would obviously shut strict up, but I would still like strict's variable name checking.
    Since strict vars relies on lexical analysis, you have a perverse need to use eval() and you already mentioned that you don't know which variables will be used (how is this in any way realistic again? This seems like a prime example of an XY Problem) you have no choice but to disable strict vars. Luckily you only need it at the eval point:
    sub eval_this { my $code = shift; $code = "package main; \n$code"; no strict 'refs'; eval $code; die ("Error: $@") if $@; }

    update: you could do:

    eval_this('our $var; print $var');
    Which doesn't really improve on the situation at all, but would shut up strict.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://697117]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found