LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2010, 11:10 PM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
source vaibale in perl


Hello

I can define all variables in tcl programming in a file for instance var.cfg
and source the same file in my tcl script such as

source var.cfg

Is this possible in perl too?
 
Old 04-28-2010, 01:17 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
you can use

require "filename.pl"

or

require "filename.txt"

filename.txt (or .pl) doesn't need #!/usr/bin/perl at the start, but the file must end with

1;

on it's own.
 
Old 04-28-2010, 03:33 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by smoker View Post
but the file must end with

1;

on it's own.
that is not strictly correct. the last statement must return a true value.


here is a (hastily written) example of how to save a hash to file and read it in
on the next invocation. The sort of thing that's easy in tcl and lisp.
if you try it the 'now' should change on each go.

(ignore the hard-coded file names, bad style)
Code:
use strict;
use warnings;
use Data::Dumper;

our %thing;

require "./1" if -f "./1";
%thing = (
        fish => 'lips',
        create => scalar localtime(),
        ) unless %thing;

$thing{now} = scalar localtime;

open OUT, ">1" or die "ouch! $@\n";
print OUT  Data::Dumper->Dump ( [\%thing], [qw(*thing)]) ;
print Data::Dumper->Dump ( [\%thing], [qw(*thing)]) ;
 
Old 04-28-2010, 07:02 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bharatbsharma View Post
Hello

I can define all variables in tcl programming in a file for instance var.cfg
and source the same file in my tcl script such as

source var.cfg

Is this possible in perl too?

Well, YES and NO. The YES part is that you can get values of variable from an external file provided the variables are global/package ones.

The NO part is that you can't get value of lexical variable through 'require', but you can through 'eval'.

A better idiom is to import an anonymous hash:


Code:
sergei@amdam2:~/junk> cat -n hash_importer.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4
     5  my $x = 10;
     6  my $hash_ref = require './hash_to_be_imported.prl';
     7
     8  warn "\$x=$x";
     9
    10  foreach my $key(keys %{$hash_ref})
    11    {
    12    warn "key/value: $key/$hash_ref->{$key}"
    13    }
sergei@amdam2:~/junk> cat -n hash_to_be_imported.prl
     1  {
     2  foo => 1,
     3  bar => 2
     4  }
sergei@amdam2:~/junk> ./hash_importer.pl
$x=10 at ./hash_importer.pl line 8.
key/value: bar/2 at ./hash_importer.pl line 12.
key/value: foo/1 at ./hash_importer.pl line 12.
sergei@amdam2:~/junk>
If you want to delve deeper into the issue and understand how Perl uses lexical scoping to isolate things, replace contents of 'hash_to_be_imported.prl' file with these:

Code:
sergei@amdam2:~/junk> cat -n hash_to_be_imported_1.prl
     1  my $x = 0;
     2
     3  warn "IMPORTED FILE: \$x=$x";
     4
     5  {
     6  foo => $x++,
     7  bar => $x++
     8  }
     9
sergei@amdam2:~/junk>
and look carefully at the behavior/value of $x in both files.


The point of my example is complete separation of the data to be imported from the importer - this allows a lot of reuse of the data to be imported. Another point is that data to be imported can not screw up the importer provided the importer is written using only lexical variables.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl: need a few lines to change source directory within a bioinformatics program pwd_pwd_omg_pwd Programming 4 11-18-2008 05:57 PM
Build libperl.so from perl source? TotalDefiance Linux - Software 0 02-08-2006 11:57 AM
Perl: Source shell script to for environment variables? stefanlasiewski Programming 3 02-07-2006 06:27 PM
Installing Gtk-Perl from Source Error AceTech747 Linux - Software 0 01-27-2004 09:18 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration