I'm not far above an initiate myself. But yes, declaring $a with my within the eval limits its scope only to that eval block -- as declaring anything with my makes it a lexical variable limited to whatever { } block it is within (not just eval; the same goes for a sub or 'for' or any block). If you want a global variable -- why do you want a global variable? If you just want something that's visible to both your eval and your sub, you could declare 'my $a' outside both blocks -- (creating a lexical variable at the package level). It would do what you need it to do, and make it effectively 'global' to your script here.
Does this sound confusing? Sorry. I am still figuring out scope myself. I read this excellent article on scope the other day (discovered in a link here). It is kind of old, but timeless (in recent versions of Perl, 'our' declarations are recommended over 'use vars' for package variables).
In any case, I would recommend the pragmata "use warnings 'all'" and "use strict" to help you catch issues like this. It's particular helpful when you're learning, but recommendable always.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|