于 2012-4-27 15:41, Octavian Rasnita 写道:
> From: "Zapp" <zapp.pref...@gmail.com>
>> 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";
>>
>> and in my.pl :
>> #!/usr/bin/perl -w
>> use strict;
>>
>> do './my_env.pl' or die '$!\n";
>> print $abc $ddd; # and nothing happen
>>
>> why? anybody knows? Thanks!
>>
>> -- 
>
>> print $abc $ddd; # and nothing happen
> Are you sure that nothing happends?
> It should have given an error like "requires explicit package name " for the 
> variables which are not defined in your script if you use "use strict".
>
> And if you use:
> print $abc $ddd;
> then the variable $ddd is printed to the file handle $abc.
> If you want to print more variables, you either should concatenate them using 
> "." like:
> print $var1 . $var2 . $var3;
> or print them as a list of vars:
> print $var1, $var2, $var3;
>
> If you define the variable in the current script but you want to assign 
> values for it in another script, you have to use "our" instead of "my", like:
>
> our $ddd;
> do './my_env.pl' or die "$!\n";
> print $ddd;
>
> And in the called script you should also use "our" if you use "use strict" in 
> it also:
> our $ddd="ddd";
>
> But usually there are better ways to call a Perl code from a file in another 
> Perl script...
>
> Octavian
>
Thanks for your reply, I will try ...

-- 
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