Home > Archive > PERL Beginners > March 2008 > Package???
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Sanket Vaidya 2008-03-20, 8:02 am |
|
Hi all,
I am having some questions regarding following codes:
use warnings;
$name = "sanket";
$fred::name = "Fred;
print "In main name = $name\n";
package Fred;
print "Now name = $name";
The output will be as expected:
In main name = sanket
Now name = Fred
Now if I use "strict" then code becomes
use warnings;
use strict;
my $name = "sanket";
$fred::name = "Fred;
print "In main name = $name\n";
package Fred;
print "Now name = $name";
The output is
In main name = sanket
Now name = sanket
why so?
why the out put is not:
In main name = sanket
Now name = Fred
Please help me out.
I am using Active Perl 5.6.2.
| |
| Chas. Owens 2008-03-20, 7:02 pm |
| On Thu, Mar 20, 2008 at 5:26 AM, sanket vaidya <sanket.vaidya@patni.com> wrote:
>
>
> Hi all,
>
> I am having some questions regarding following codes:
>
> use warnings;
>
> $name = "sanket";
> $fred::name = "Fred;
>
> print "In main name = $name\n";
>
> package Fred;
> print "Now name = $name";
>
> The output will be as expected:
> In main name = sanket
> Now name = Fred
>
>
> Now if I use "strict" then code becomes
>
> use warnings;
> use strict;
>
> my $name = "sanket";
> $fred::name = "Fred;
>
> print "In main name = $name\n";
>
> package Fred;
> print "Now name = $name";
>
> The output is
> In main name = sanket
> Now name = sanket
>
> why so?
>
> why the out put is not:
> In main name = sanket
> Now name = Fred
snip
First off, don't do that. Messing around with another package's
variables ties you to that version of the module and is bad juju.
Secondly the issue is that strict* requires you to either declare your
variables or use fully qualified variables ($packagename::var). To
get the output you desire use the our function to declare $name in
Fred (also be certain to use the same case for $Fred::name as package
Fred, and don't type posts into emails, copy and paste the code to
avoid errors like that**):
#!/usr/bin/perl
use warnings;
use strict;
my $name = "foo";
$Fred::name = "bar";
print "$name\n";
package Fred;
our $name;
print "$name\n";
* specifically strict 'vars', see http://perldoc.perl.org/strict.html
** I predict you will see about three people telling you that
$fred::name and $Fred::name are in different packages
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
| |
| Gunnar Hjalmarsson 2008-03-20, 7:02 pm |
| Chas. Owens wrote:
> On Thu, Mar 20, 2008 at 5:26 AM, sanket vaidya <sanket.vaidya@patni.com> wrote:
>
> First off, don't do that. Messing around with another package's
> variables ties you to that version of the module and is bad juju.
Not sure I understand how the OP's question motivates that remark.
> To get the output you desire use the our function to declare $name
> in Fred
Even if that solution 'works', it results in the warning ""our" variable
$name masks earlier declaration in same scope". The reason is that our()
is lexically scoped, not package scoped.
Please consider this example:
C:\home>type test.pl
use warnings;
use strict;
my $name = 'sanket';
$Fred::name = 'Fred';
print "$name\n";
package Fred;
our $name;
print "$name\n";
package main;
print "$name\n";
C:\home>perl test.pl
"our" variable $name masks earlier declaration in same scope at test.pl
line 9.
sanket
Fred
Fred
C:\home>
The third print() statement, even if within package main, outputs the
package variable $Fred::name. In other words, since the our()
declaration in package Fred is not restricted to a block, it's effective
from the point of its location and throughout the rest of the file.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Chas. Owens 2008-03-20, 7:02 pm |
| On Thu, Mar 20, 2008 at 1:13 PM, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
snip
>
> Not sure I understand how the OP's question motivates that remark.
snip
Packages are normally used to implement modules. Modules are discrete
pieces of code that should not depend on the internal workings of
other modules or the scripts that use them. By modifying a variable
in another package the OP is breaking the encapsulation that packages
are there to provide. While it can be done it is an incredibly bad
idea. See below.
>
>
>
> Even if that solution 'works', it results in the warning ""our" variable
> $name masks earlier declaration in same scope". The reason is that our()
> is lexically scoped, not package scoped.
>
> Please consider this example:
>
> C:\home>type test.pl
> use warnings;
> use strict;
>
> my $name = 'sanket';
> $Fred::name = 'Fred';
>
> print "$name\n";
>
> package Fred;
> our $name;
> print "$name\n";
>
> package main;
> print "$name\n";
>
> C:\home>perl test.pl
> "our" variable $name masks earlier declaration in same scope at test.pl
> line 9.
> sanket
> Fred
> Fred
>
> C:\home>
>
> The third print() statement, even if within package main, outputs the
> package variable $Fred::name. In other words, since the our()
> declaration in package Fred is not restricted to a block, it's effective
> from the point of its location and throughout the rest of the file.
snip
Interestingly enough, I don't get that warning with 5.8.8 (but I do
see the behavior). It looks like it was added in 5.10. This is an
issue, but only if you use multiple packages in one file (not common
outside of OO code that shouldn't need our in the first place), you
reopen a previous package, and you forget to limit the scope of the
other packages with braces. Gee, you do something uncommon and
inadvisable and get bitten by it. This is why good style dictates
that packages should go in their own files (with the possible
exception of helper classes in OO code). Perl lets you do many
dangerous and stupid things, but that doesn't mean you should do them.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
| |
| Gunnar Hjalmarsson 2008-03-20, 7:02 pm |
| Chas. Owens wrote:
> On Thu, Mar 20, 2008 at 1:13 PM, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>
> Packages are normally used to implement modules. Modules are discrete
> pieces of code that should not depend on the internal workings of
> other modules or the scripts that use them. By modifying a variable
> in another package the OP is breaking the encapsulation that packages
> are there to provide. While it can be done it is an incredibly bad
> idea.
It's not unusual that module authors give the users the opportunity to
alter one or more package variables. In the light of that, I can't help
thinking that you exaggerate a little. ;-)
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
|
|
|