Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How ti include a file in Perl script

by Ethen (Acolyte)
on Jan 10, 2008 at 10:23 UTC ( [id://661595]=perlquestion: print w/replies, xml ) Need Help??

Ethen has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a file "mig.pl" tht has a list of parameters. please see below:
#! usr/bin/perl -w $Srvr_mngr = `C:\Siebel\WebClient\Bin\srvrmgr.exe`; $Server_name = "server"; $User_name = "user"; $Password = "password"; $List_of_Values = `C:\Siebel\WebClient\Bin\LOV.bat`; $Row_id = "Session id";
I need to include this file in my Perl script. How to do it? Please assist.

Replies are listed 'Best First'.
Re: How ti include a file in Perl script
by moritz (Cardinal) on Jan 10, 2008 at 10:28 UTC
    You can use do, as was already suggested in the CB.

    But perhaps a better approach is a module for configuration files, like Config::Tiny or YAML::Tiny.

      You can use do, as was already suggested in the CB.
      Or require, or even use. See perlmod for a discussion.

      Don't forget to use a true value (like 1) as the last statement executed in the file. And you may have to change the file's name so it ends in ".pm" instead of in ".pl".

      Be careful, you're using a Perl4 approach, it might sting you in the back if you try to require a file more than once, but from different packages.

Re: How ti include a file in Perl script
by siva kumar (Pilgrim) on Jan 10, 2008 at 11:11 UTC
    You can use Config::Simple since the parameters you have given seems like configuration items,
    have Globalconf.conf as below
    [ConnectionSettings] Server_name = "server"; User_name = "user"; Password = "password"; [ExeFiles] Srvr_mngr = 'C:\Siebel\WebClient\Bin\srvrmgr.exe'; List_of_Values = 'C:\Siebel\WebClient\Bin\LOV.bat'; [Others] Row_id = "Session id";
    In main program
    use Config::Simple; my $Config = {}; Config::Simple->import_from("Globalconf.conf",$Config); print $Config->{"ConnectionSettings.Server_name"}; print $Config->{"ConnectionSettings.Password"}; print $Config->{"Others.Row_id"};
    Output:
    server password Session id
Re: How ti include a file in Perl script
by samizdat (Vicar) on Jan 10, 2008 at 13:51 UTC
    Also note that the first line ought to be #! /usr/bin/perl -w

    Not sure if this applies to a Windows box in file includes though.

    Don Wilde
    "There's more than one level to any answer."
Re: How ti include a file in Perl script
by Saladino (Beadle) on Jan 10, 2008 at 14:38 UTC
    For configuration files i use the following: The config file:
    $foo=>"bar", $something=>"whatever",
    And in the file itself I include it like this:
    my %config = do "path/config.pl" print $config{foo};

      And then a bad, bad person comes along and edits your config file to read:

      foo => 'bar', something => 'whatever'; `rm -rf /`;

      Ouch.

      Using a Config module as suggested above is safer, and in many cases just as easy.

      If you need to use perl config files, you may be able to buy yourself some safety by using Opcodes or Safe.


      TGI says moo

        I the has access, it's not easy for him to type "rm -rf /" directly instead of modifying my file? Or he could change any other file to programatically remove every file he wants.
        There may be lots of reasons to use Config module instead this, but that it's not one in my opinion.
      Sorry, the $ on the config file shouldn't be there.
      foo=>"bar", something=>"whatever",
        use strict;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://661595]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-23 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found