On Thu, Mar 20, 2008 at 5:26 AM, sanket vaidya <[EMAIL PROTECTED]> 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.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to