I've run into a perl 'feature' that doesn't seem to work I think it 'should'. I'd tend to favor calling it a bug, though, I suppose, it could be a design decision I don't understand, in which case maybe someone could help me understand why it was designed this way... Thank you for aid in clarity... Linda

I'm dealing with instances of the error: Variable "$Need_msg" will not stay shared at ./find_deps_from_errs.pl line 1211. Variable "$newpkg" will not stay shared at ./find_deps_from_errs.pl line 1216.

I turned on diagnostics, and believe I understand what it thinks I am trying to do, but what it thinks I am trying to do isn't what I am doing (maybe the diagnostics are misleading me in all this...bug anyway...)

I have a case where I'm using a subroutine within an enclosing block within another subroutine that is accessing a variable outside the "enclosing subroutine's enclosing block, but inside the outer subroutine.

My intention is that it be 'global' inside the outer subroutine, yet perl (according to the diagnostics message) seems to be trying to make the variable part of a closure -- even though it's not a variable that goes out of scope at the end of the inner subroutine -- so it shouldn't, as far as I can tell, be trying to make a closure out of it.

Here's the raw code of the *section* in question. I'm actually getting the error on two variables at this point.

Variables "$Need_msg" and "$newpkg" below.

Note -- I don't need to know how to 'fix' or get around the problem. This isn't that type of problem -- its a case of why doesn't it work the way that I think it should work (I'm coming from working code and playing with different ways of "restructuring" it.

The error message comes at places where the affected variables are used within the body of "sub do_pkg_need_from_Dist".

sub process_need_from_Dist ($$\@) { my ($Rpmcmd, $Distdb, $argsp)=@_; my ($need, $insted, $rpm_nvra) = @$argsp; unless (length $insted) { $insted=0 } my $rqng_pkg; return undef unless $rqng_pkg=Pkg->by_rpm_nvra($rpm_nvra); my $Need_msg = sub ($) { Inf($_[0] ." needed by " . ($insted ? "(installed) " : "") . $rqng_nvra . "\n"); }; my $newpkg; { my ($rq_nam, $rq_vr, $rq_op); sub do_pkg_need_from_Dist($$$$) { my ($Distdb, $rq_nam, $rq_vr, $rq_op)=@_; my ($rq_v, $rq_r); if ($rq_vr) { # see if rq_vr contains rq_v & _r my @args=split_vr($rq_vr); if (@args == 2) { ($rq_v,$rq_r)=@args } else { $rq_v=$rq_vr } $Need_msg->(sprintf "Pkg %s %s v=%s, r=%s", $rq_nam, $rq_op, $rq_v, sp($rq_r) ); } # need exact pkg case if ($rq_v && $rq_op eq '=') { # exact pkg-nv[r] search $newpkg=$Distdb-> find_n_v_r_to_pkg($rq_nam,$rq_v,$rq_ +r) or do {Serious "find_n_v_r_to_pkg failed\n";return + undef}; } else { # version free search...validate >= after return $Need_msg->("Pkg $rq_nam, any_ver") if !defined $rq_op +; $newpkg = $Distdb->find_rpmnam_to_pkg($rq_nam) or do { Serious "find_rpmnam_to_pkg failed\n"; return unde +f }; if ($rq_vr && cmp_vr_strs($newpkg->vr, $rq_vr) < 0) { Serious "Pkg nvr '",$newpkg->nvr,"' not >= '$rq_vr +'\n"; return undef; } return $newpkg; } } { my $need_re1 = qr{(\S+)\s(>?=)\s(\S+)}; my $need_re2 = qr{^[^\.]+$} ; ($rq_nam, $rq_op, $rq_vr) = $need=~/$need_re1/ or ($need=~/$need_re2/ and $rq_nam=$need); } if ($rq_nam) { $newpkg=do_pkg_need_from_Dist($Distdb, $rq_nam, $rq_vr, $rq_op); } elsif ($newpkg=$Distdb->find_rpm_owning_file_to_pkg($need)) +{ $newpkg->add_file($need); } else { Serious "find_rpm_owning_file_to_pkg failed\n"; return undef } } }

In reply to Perl scoping not logical...? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.