Trouble overwriting a global variable in a block

J

juergen

Hello!

I want to overwrite a global variable (main::) from a block. Here is my
code:
-----
$MS_IMG_PATH = "foobar";
if ( exists($ENV{'PRODUCT'}) )
{
$MS_IMG_PATH = 'barfoo';
if ( $ENV{'PRODUCT'} eq "7b4/105/1" )
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
----

The program puts out:
barfoo
/dcim/100olymp
/dcim/100olymp
foobar

But it should be:
barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp

I really can't imagine what is going on.
Thank you in advance!

Bye, juergen
 
R

robic0

Hello!

I want to overwrite a global variable (main::) from a block. Here is my
code:
-----
$MS_IMG_PATH = "foobar";
if ( exists($ENV{'PRODUCT'}) )
{
$MS_IMG_PATH = 'barfoo';
if ( $ENV{'PRODUCT'} eq "7b4/105/1" )
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
----

The program puts out:
barfoo
/dcim/100olymp
/dcim/100olymp
foobar

But it should be:
barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp

I really can't imagine what is going on.
Thank you in advance!

Bye, juergen

I didn't get what you did. If every conditional is true:

use strict;
use warnings;

my $MS_IMG_PATH = "foobar";
if (1)
{
$MS_IMG_PATH = 'barfoo';
if (1)
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";

__END__

barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp
 
S

sanjeeb

As per the code you sent variable $MS_IMG_PATH is global ,So what ever
you will change in that it will reflect in rest of the code.
if you really want to make it in a scope then use "my" to make the
scope of that variable in that block.

Regards
sanjeeb
 
R

robic0

As per the code you sent variable $MS_IMG_PATH is global ,So what ever
you will change in that it will reflect in rest of the code.
if you really want to make it in a scope then use "my" to make the
scope of that variable in that block.

Regards
sanjeeb

Didn't know that, is it a main:: variable?

use strict;
use warnings;

$MS_IMG_PATH = "foobar";
if (1)
{
$MS_IMG_PATH = 'barfoo';
if (1)
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";

__END__

Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 4.
Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 7.
Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 10.
Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 11.
Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 12.
Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 14.
Global symbol "$MS_IMG_PATH" requires explicit package name at rr.pl line 16.
Execution of rr.pl aborted due to compilation errors.
 
R

robic0

I didn't get what you did. If every conditional is true:

use strict;
use warnings;

my $MS_IMG_PATH = "foobar";
if (1)
{
$MS_IMG_PATH = 'barfoo';
if (1)
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";

__END__

barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp


Hmmm...
But, when strict is commented out, it gets:

#use strict;
#use warnings;

$MS_IMG_PATH = "foobar";
if (1)
{
$MS_IMG_PATH = 'barfoo';
if (1)
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";

__END__

barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp
 
R

robic0

To declare globals you can use 'our' which would make it visible across packages.
Read about my, local and our scoping declarations.
 
S

sanjeeb

use strict;
$ENV{'PRODUCT'}="7b4/105/1";# Suppose given
my $MS_IMG_PATH = "foobar";
if ( exists($ENV{'PRODUCT'}) )
{
my $MS_IMG_PATH = 'barfoo';
if ( $ENV{'PRODUCT'} eq "7b4/105/1" )
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";


}


print "$MS_IMG_PATH\n";





output

barfoo
/dcim/100olymp
/dcim/100olymp
foobar
 
R

robic0

use strict;
$ENV{'PRODUCT'}="7b4/105/1";# Suppose given
my $MS_IMG_PATH = "foobar"; this ^^^^^^^^^
if ( exists($ENV{'PRODUCT'}) )
{
my $MS_IMG_PATH = 'barfoo';
^^^^^^^^^^^^
is not the same as this
if ( $ENV{'PRODUCT'} eq "7b4/105/1" )
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";


}


print "$MS_IMG_PATH\n";





output

barfoo
/dcim/100olymp
/dcim/100olymp
foobar

Scoping. Either way it has nothing to do with main::
 
R

robic0

^^^^^^^^^^^^
is not the same as this

Scoping. Either way it has nothing to do with main::

He/you may be talking about package globals. These are adressed
based on scoping blocks, unless a superset shared name is used
from whoever imported your package. That can only be main::, the
one that hosts your Perl program. That is why when he mentioned 'main::'
flags came up and I mentioned my,our,local. By 'global' I guess he means
package global. By default your .pl is an imported module, into main::
 
J

juergen

Hello!
I didn't get what you did. If every conditional is true:
[...]
barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp
You are right. The code snippet works, when run standalone. I intended
to run this by the linux hotplug system on hotplug events. Somehow the
script gets executed several times on the event.
And the set of environment variables differs on each call! - That
scrambles the logic of the rest of the program and it comes to this
very confused behaviour.
So the problem is hidden in the hotplug system (and perhaps my rule - I
don't know yet how to tighten it ;-)).

I'm sorry that I did not notice that before ...
You all have helped me a lot! Thank you very much!

Bye, juergen
 
B

Brian McCauley

I want to overwrite a global variable (main::) from a block.

Something you should want to do very rarely but there should be no
problem.
Here is my code:
-----
$MS_IMG_PATH = "foobar";
if ( exists($ENV{'PRODUCT'}) )
{
$MS_IMG_PATH = 'barfoo';
if ( $ENV{'PRODUCT'} eq "7b4/105/1" )
{
print "$MS_IMG_PATH\n";
$MS_IMG_PATH = '/dcim/100olymp';
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
}
print "$MS_IMG_PATH\n";
----

The program puts out:
barfoo
/dcim/100olymp
/dcim/100olymp
foobar

Not do me it doesn't. Are you absolutely sure you've cut and pasted
your test code exactly and in its entirity?
But it should be:
barfoo
/dcim/100olymp
/dcim/100olymp
/dcim/100olymp

Does for me.
I really can't imagine what is going on.

Nor can I. If the code you show us is really the exact test case you
have executed then something very odd indeed is happening.

Can you tell us what build of perl you are using?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top