Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

"Variable will not stay shared" warning

by George_Sherston (Vicar)
on Aug 07, 2002 at 11:29 UTC ( #188283=perlquestion: print w/replies, xml ) Need Help??

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

Sibling monks.. My script works - does what I expect. BUT wherever I pass a hash element to the PDF::Create string function, I get a warning which I don't understand:
warning: Variable "%Params" will not stay shared
I get this warning when I have
$page->string($fn, 12, 150, $height, $Params{InvoiceID});
n.b. that the hash %Params does have a valid element InvoiceID.

When I replace that line with
$page->string($fn, 12, 150, $height, 1000);
... the warning goes away. And as I say, the script seems to work o.k. either way. Should I worry? And what does it mean?

George Sherston

Replies are listed 'Best First'.
Re: "Variable will not stay shared" warning
by jmcnamara (Monsignor) on Aug 07, 2002 at 11:46 UTC

    Without seeing the surrounding code it is difficult to say. However, this section from perldiag might point you in the right direction:
    Variable ``%s'' will not stay shared (W closure) An inner (nested) named subroutine is referencing a le +xical variable defined in an outer subroutine. When the inner subroutine is called, it will probably see the valu +e of the outer subroutine's variable as it was before and during the *f +irst* call to the outer subroutine; in this case, after the first call t +o the outer subroutine is complete, the inner and outer subroutines will + no longer share a common value for the variable. In other words, the variable will no longer be shared. Furthermore, if the outer subroutine is anonymous and references a lexical variable outside itself, then the outer and inner subrouti +nes will never share the given variable. This problem can usually be solved by making the inner subroutine anonymous, using the sub {} syntax. When inner anonymous subs that reference variables in outer subroutines are called or referenced, + they are automatically rebound to the current values of such variables.

    --
    John.

      Yeha. That was just what I needed. Now I get it. What was happening was that, for some reason that now eludes me, I had written one subroutine inside another. %Params got made in the outer sub. Like a good boy, I was passing arguments from the outer to the inner, even though the inner was within the scope of the outer. BUT, although I passed \%Params so that in the inner sub I did my $Params = shift; I was getting values out of it by $Params{MyKey}. If I'd put the inner sub outside the outer sub, I would have got an error, because %Params wd have been undefined; but in fact, it just assumed I was being lazy and not passing the argument. But still gave me a warning to help me see the error of my ways. Now when I do $Params->{MyKey} the error goes away. A spoonful of sugar helps the medicine go down, as they say. Thanks.

      George Sherston
Re: "Variable will not stay shared" warning
by perrin (Chancellor) on Aug 07, 2002 at 14:08 UTC
    Wild guess: you are using Apache::Registry and your code looks like this:
    my %Params; .... sub foo { .... $page->string($fn, 12, 150, $height, $Params{InvoiceID}); .... }
    In general, this warning means you've created a closure and $Params{InvoiceID} will not change for the rest of the life of your program. That could be bad.
Re: "Variable will not stay shared" warning
by PodMaster (Abbot) on Aug 08, 2002 at 05:05 UTC
    If the warnings you get are not clear enough for you,

    use diagnostics;

    #!/usr/bin/perl -w use diagnostics; use strict; sub outermost { my $a; sub middle { sub { $a } } }
    Use strict warnings and diagnostics or die

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: "Variable will not stay shared" warning
by theorbtwo (Prior) on Aug 07, 2002 at 12:03 UTC

    I'd tend to file this under "Not My Problem". OTOH, remember about it, because if somthing related goes wrong this might be causing it. Also, you can probably shut the warning up by saying $Params{InvoiceID}+0 or "$Params{InvoiceID}", thus making it no longer a variable.


    Confession: It does an Immortal Body good.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2019-07-19 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?