--- Kyle Babich <[EMAIL PROTECTED]> wrote: > How can I import scalars, arrays, etc. from external perl and text > files? > > This is what I have in index.pl: > > #!/usr/local/bin/perl -wT > use strict; > use CGI qw/ :standard /; > > print header ( 'text/html' ); > > open(TEXT,"<fried.txt") or die ("error: fried.txt failed\n"); > while(<TEXT>) { > print; > } > close(TEXT) or die("error: fried.txt failed\n"); > > print "$name chicken"; > > And this is what I have in fried.txt: > > my $name = "chicken"; > > So why doesn't it work?
--- Kyle Babich <[EMAIL PROTECTED]> wrote: > How can I import scalars, arrays, etc. from external perl and text > files? > > This is what I have in index.pl: > > #!/usr/local/bin/perl -wT > use strict; > use CGI qw/ :standard /; > > print header ( 'text/html' ); > > open(TEXT,"<fried.txt") or die ("error: fried.txt failed\n"); > while(<TEXT>) { > print; > } > close(TEXT) or die("error: fried.txt failed\n"); > > print "$name chicken"; > > And this is what I have in fried.txt: > > my $name = "chicken"; > > So why doesn't it work? When declaring variables with 'my', you wind up lexically scoping them. This is "file" scoped, however, so a lexically scoped variable in another file is not accessible to you. One easy (and kind of simplistic) way of getting around this is to have your variables in the second file in a hash ref. Add the following to a file named "test.dat": { one => 'uno', two => 'dos' } Then, in the same directory, create and run the following program: #!/usr/bin/perl -w use strict; use Data::Dumper; my $hash_ref = do ( 'test.dat' ) or die "Cannot open test.dat: $!"; print Dumper $hash_ref; The above form of 'do' is a special form that "evals" the contents of the file. The last thing returned from the eval is the last results of the last expression evaluated, in this case, a hash reference. Be careful with this technique, though. If someone else can alter the contents of 'test.dat', you could be eval'ing unsafe code. Cheers, Curtis "Ovid" Poe ===== "Ovid" on http://www.perlmonks.org/ Someone asked me how to count to 10 in Perl: push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A __________________________________________________ Do You Yahoo!? Sign up for SBC Yahoo! Dial - First Month Free http://sbc.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]