于 2012-4-27 20:43, Shawn H Corey 写道:
> On 12-04-27 03:06 AM, Zapp wrote:
>> when I use bash, I can write a file ( a.sh ) like :
>> abc='abc'
>> ddd='aaa'
>> ...
>> then I can load it in other file:
>> source a.sh
>> echo $abc $ddd # it always work!
>>
>> but in perl , how can I do like that ?
>>
>> I write a file ( my_env.pl ) like:
>> #!/usr/bin/perl -w
>> my $abc='abc';
>> my $ddd="ddd";
> `my` produces lexical variables which means they are scoped to the file.
> These variable will not be accessible outside the file. Tey something like:
>
> use vars qw( $abc $ddd );
> $abc = 'abc';
> $ddd = 'ddd';
>
>> and in my.pl :
>> #!/usr/bin/perl -w
>> use strict;
>>
> use vars qw( $abc $ddd );
>
>> do './my_env.pl' or die '$!\n";
>> print $abc $ddd; # and nothing happen
> print $abc, $ddd, "\n";
>
>> why? anybody knows? Thanks!
>>
> If you want to create a configuration file:
>
> $ cat MyConfig.pm
> #!
>
> use strict;
> use warnings;
>
> use base qw( Exporter );
> our @EXPORT = qw( %MyConfig );
> our @EXPORT_OK = qw( );
> our %EXPORT_TAGS = (
>   all => [ @EXPORT, @EXPORT_OK ],
> );
>
> our %MyConfig = (
>   abc => 'abc',
>   ddd => 'ddd',
> );
>
> 1;
> __END__
> $ cat myscript.pl
> #!/usr/bin/env perl
>
> use 5.014;
> use strict;
> use warnings;
>
> use Data::Dumper;
>
> # Make Data::Dumper pretty
> $Data::Dumper::Sortkeys = 1;
> $Data::Dumper::Indent   = 1;
>
> # Set maximum depth for Data::Dumper, zero means unlimited
> local $Data::Dumper::Maxdepth = 0;
>
> our %MyConfig;
> use MyConfig;
>
> print Dumper \%MyConfig;
>
> __END__
> $
>
>
Thanks! It's work!

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to