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

Hello, I am writing a perl script to execute another scripts , one is my.ini , which I execute in shell via "source my.ini", but I don't know how to run this command via shell script. I made a simple test.ini in which I just wrote "echo hello", when I write source test.ini then shell terminal shows hello but when I write the command system ("source test.ini"), then it executes but don't show the hello text on output screen . the other two are perl scripts , I want to execute them one by one with this current perl script . Is this command ok for that system "perl my.pl" Thanks NOTE : I started learning perl scripting today
  • Comment on Sourcing another Scripts using perl script

Replies are listed 'Best First'.
Re: Sourcing another Scripts using perl script
by choroba (Cardinal) on Jan 20, 2014 at 23:26 UTC
    system runs /bin/sh, which does not understand source. Use . (dot) instead.

    To check the succes of a system command, compare its return value to zero:

    if (0 == system 'ls') { print "Success\n"; } else { print "Error\n"; }

    To call a Perl script from a Perl script, you can also use do.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Hello! Thanks for your guidance , Actually this ini file will set some environment variables when i sourec it . Executing my ini file will not solve the problem, I used the dot run "./my.ini" it just executes the commands inside the ini file , . I want to use "Source my.ini", Can i do that with perl ?
        By using . instead of source, I meant
        . my.ini

        not ./my.ini.

        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Sourcing other scripts using perl script
by Athanasius (Archbishop) on Jan 21, 2014 at 00:41 UTC

    Hello farha89, and welcome to the Monastery!

    choroba has answered your question; but, since you’re new to Perl, you will find it useful to know the different mechanisms which Perl provides for running external scripts.

    The system command runs an external command, and returns that command’s error status. Any output to STDOUT or STDERR is printed directly to the terminal. Often, this is what you want.

    But sometimes you would rather collect the output of the external command in your Perl script. For this, Perl provides backticks:

    my $output = `test.bat`;

    When you run this, anything sent by test.bat to STDOUT is captured (so, it is not printed to the terminal) and assigned to $output. STDERR still goes directly to the terminal. Note that backticks can also be written as qx(...), so the above is equivalent to:

    my $output = qx( test.bat );

    See perlop#Quote-Like-Operators.

    For more fine-grained control over what is captured, the module Capture::Tiny is useful:

    use Capture::Tiny qw( capture ); my ($stdout, $stderr, @result) = capture { system 'test.bat'; };

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Sourcing another Scripts using perl script
by FloydATC (Deacon) on Jan 21, 2014 at 17:14 UTC

    Hmm! Am I right in assuming you want to source those shell scripts in order to set environment variables or declare shell functions so they will be inherited by your Perl process and any child processes you spawn?

    If so, I don't think it can be done, but I would love for someone to correct me.

    However, if your "my.ini" is just a confusingly named Perl script, it's possible to run it using require "my.ini"; but I would definitely not recommend this.

    Instead, use proper modules. They're not hard to write, and they will save you lots of backtracking later.

    -- FloydATC

    Time flies when you don't know what you're doing