my variable is recognized in following sub

S

sln

Lexical variables were introduced in Perl 5.

In Perl 4 and before, package variables where all there was.




You are correct!

However the primary benefit of using lexical variables is for the programmer,
rather than for the interpreter.




Here is another mantra that will serve you well:

Always prefer lexical variables (my) over package variables (our),
except when you can't.




Yes you can.

-------------------
#!/usr/bin/perl
use warnings;
use strict;

$main::var = 'some value';
print "$main::var\n";
-------------------

Works fine...




Then you don't *want* them to be package variables.

Since you _can_ use lexical variables, then following the mantra,
that is what you should use.




You are not looking in the right place then.

I dashed off this crude test:

-------------------
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;

my @files;
find( \&is_module, @INC );
my $module_cnt = @files;
print "$module_cnt module files found\n";

my $strict_cnt;
foreach my $file ( @files ) {
$strict_cnt++ if qx/grep -l 'use strict;' $file/;
}

print "$strict_cnt module files found that use strict\n";

my $percent = sprintf '%.0f', ($strict_cnt / $module_cnt) * 100;
print "$percent% of modules have strict enabled\n";

sub is_module {
push @files, $File::Find::name if /\.pm$/;
}
-------------------


About 80% of the modules on my system appear to have strict enabled.




Please post Real Perl Code rather than attempt to paraphrase Perl code.

Case matters.




Putting the "use strict" (not "use Strict", case matters) "down here"
will not change anything. You'll get the same error regardless of
where in the file the "use strict" statement is.

To use your package variable with strict enabled you must either declare it:

our $IwannaBeApackageGlobal = "do use strict";

or use the explicit package name:

$FunStuff::IwannaBeApackageGlobal = "do use strict";

Thans Tad!


sln
 
U

Uri Guttman

s> I understand now. I tried all package global declaration methods
s> and tested them.. Just popped them on top of a pre-existing
s> package I already have. All worked, but the fully qualified name
s> would take a non-existent package name as well. It might be that if
s> the module isin't found, it loads it into the main:: namespace
s> without warning. Dunno.

you don't get what the package command does. it does VERY little. all it
does is set the default package space for any names that aren't fully
qualified (until end of scope or the next package command). it does
nothing else. perl's packages are just namespaces without any special
semantic or metadata.

s> I tested en-mass, here (printed to STDERR because the module produces output
s> I redirect from the command line) is what I got:

s> Package FunStuff;
s> use vars qw($IwannaBeApackageGlobal2);

s> our $IwannaBeApackageGlobal = "use strict then"; # works
s> $IwannaBeApackageGlobal2 = "use strict then = 2"; # works
s> $hhh::IwannaBeApackageGlobal3 = "use strict then = 3"; # works, but have to use qulified name all the time

no you don't. fully qualified names are only needed if you are in one
package and want to access a symbol in a different package.

uri
 
S

sln

[snip]

To use your package variable with strict enabled you must either declare it:

our $IwannaBeApackageGlobal = "do use strict";

or use the explicit package name:

Just to be clear, I'm talking about using the variable within the package
when strict is enabled.

Within the package where this is declared, all is fine if I use the package name.
$FunStuff::IwannaBeApackageGlobal = "do use strict";

Here is cut & paste code sample:
---------------------------------------
package FunStuff;

use strict;

our $IwannaBeApackageGlobal = "use strict then";
$FunStuff::IwannaBeApackageGlobal3 = "use strict then = 3";

print $IwannaBeApackageGlobal,"\n"; # works with strict
print $FunStuff::IwannaBeApackageGlobal3,"\n"; # works with strict
print $IwannaBeApackageGlobal3,"\n"; # does not work with strict

----------------------------------------------------------------------------------
Error: Variable "$IwannaBeApackageGlobal3" is not imported at [...] line 10.


Why do I get this error when printing $FunStuff::IwannaBeApackageGlobal3 as just
$IwannaBeApackageGlobal3. Without strict, this works fine.

sln
 
S

sln

[snip]

print $FunStuff::IwannaBeApackageGlobal3,"\n"; #[snip]
[snip]

Doesen't work, it doesn't work.

Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,
Thanks Tad,

sln
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top