newbie01.perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Am in the process of converting UNIX and MSDOS scripts to Perl and some of the UNIX scripts that I have uses a format like the one below:

sqlplus -S "/ as sysdba" <<EOL > output.out alter session set nls_date_format = 'DD-MON-YYYY-HH24-MI-SS' ; set termout off set pagesize 0 set heading off set trimspool on set echo off set feedback off spool ${db}_tmp01.lst select sysdate || ',' || instance_name || ':' || host_name from v\$ +instance ; spool off spool ${db}_tmp02.lst select controlfile_type || ':' || open_mode from v\$database ; spool off spool ${db}_tmp03.lst select max(sequence#) from v\$log_history ; spool off EOL

can someone please advise how is this possible to do on Perl? Am referring to using <<EOL > output.out and EOL.

In case any Oracle DBA is reading this, am not using DBI/DBD because I can't get the / as sysdba connection to work. It is giving me segmentation fault core dump and some of the DBD that comes with the Oracle Install does not support it and our SA does not allow me to install additional modules. Besides, the only way that I will be allowed to get a newer version of the DBI/DBD is to do another install of Perl that comes with a different set of modules instead of replacing the current Perl install that comes with the Oracle install. This means I need to have keep the Perl that comes with the Oracle install and then install another set of Perl install in another directory. Just as a test, I could possibly persuade the SA to do this. Can anyone confirm if this is possible, i.e. have two Perl install on separate directories and then just manipulate @INC to specify the order of searching for the modules?

Replies are listed 'Best First'.
Re: Using <<EOL > output.out and EOL
by zwon (Abbot) on Nov 28, 2009 at 10:50 UTC

    Something like this should work:

    open my $bash, "|-", "bash >/tmp/out.txt" or die $!; print $bash <<EOL; ls df -h uname -a EOL
    Can anyone confirm if this is possible, i.e. have two Perl install on separate directories

    Yes, that's possible and quite common

      Hi,

      I tried out the following and am getting errors for the one on Micro$oft. Any chance of any monks telling me what am I doing wrong.

      The original sugggested code is as below:

      open my $bash, "|-", "bash >/tmp/out.txt" or die $!; print $bash <<EOL; ls df -h uname -a EOL

      I took out the |- and "bash ..." because I want to be able to do this for both Unix and Windows. I assume you cannot do |- on Windows, is that a correct assumption? If anyone can please tell me why am getting the error for the sqlplus under DOS:, it will be very much appreciated. Thanks in advance.

      Codes below, all is working except for sqlplus under DOS.

      DOS: OS Command - works OK: open(OUTPUT, " > eol_dos.out") or die $!; print OUTPUT <<`EOL`; dir EOL exit 0; sqlplus - this gives error : << was unexpected at this time. $ENV{'ORACLE_SID'}="TEST"; $ENV{'ORACLE_HOME'}="C:\oracle\product\10.2.0"; open(OUTPUT, " > eol_sqlplus_dos.out") or die $!; print OUTPUT <<`EOL`; sqlplus -S "/as sysdba" <<"SQLEND" set heading off alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS' ; select 'Connected to the database on ' || sysdate from dual ; SQLEND EOL exit 0 UNIX: OS Command - works OK: open(OUTPUT, " > eol_unix.out") or die $!; print OUTPUT <<`EOL`; ls echo echo "+----------------------------------------------+" echo df echo echo "+----------------------------------------------+" echo uname -a EOL exit 0; sqlplus - works OK: $ENV{'ORACLE_SID'}="test"; $ENV{'ORACLE_HOME'}="/oracle/product/10.2.0"; open(OUTPUT, " > eol_sqlplus_unix_02.out") or die $!; print OUTPUT <<`EOL`; echo "Today is `date`" echo echo "+----------------------------------------------+" echo sqlplus -S "/as sysdba" <<"SQLEND" set heading off alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS' ; select 'Connected to the database on ' || sysdate from dual ; SQLEND echo echo "+----------------------------------------------+" echo EOL exit 0

        Codes below, all is working except for sqlplus under DOS.

        First of all, I doubt you're using DOS. Aren't you using Windows?

        Assuming you're using Windows, the problem is that

        • cmd.exe doesn't provide a here-doc mechanism.
        • cmd.exe doesn't can't take multiple commands separated by newlines.

        How come you're not feeding the input to sqlplus plus as initially suggested?

Re: Using <<EOL > output.out and EOL
by AnomalousMonk (Archbishop) on Nov 28, 2009 at 20:10 UTC

      Hi Anomalous Monk,

      If you don't mind doing a quick lookup please ...

      I tried out the following suggestion from zwon and managed to get something going albeit some errors and cannot worked out where am getting things wrong. Any chance that you may be able to tell what am doing wrong?

      The original sugggested code is as below:

      open my $bash, "|-", "bash >/tmp/out.txt" or die $!; print $bash <<EOL; ls df -h uname -a EOL

      I took out the |- and "bash ..." because I want to be able to do this for both Unix and Windows. I assume you cannot do |- on Windows, is that a correct assumption? If anyone can please tell me why am getting the error for the sqlplus under DOS:, it will be very much appreciated. Thanks in advance.

      Codes below, all is working except for sqlplus under DOS.

      DOS: OS Command - works OK: open(OUTPUT, " > eol_dos.out") or die $!; print OUTPUT <<`EOL`; dir EOL exit 0; sqlplus - this gives error : << was unexpected at this time. $ENV{'ORACLE_SID'}="TEST"; $ENV{'ORACLE_HOME'}="C:\oracle\product\10.2.0"; open(OUTPUT, " > eol_sqlplus_dos.out") or die $!; print OUTPUT <<`EOL`; sqlplus -S "/as sysdba" <<"SQLEND" set heading off alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS' ; select 'Connected to the database on ' || sysdate from dual ; SQLEND EOL exit 0 UNIX: OS Command - works OK: open(OUTPUT, " > eol_unix.out") or die $!; print OUTPUT <<`EOL`; ls echo echo "+----------------------------------------------+" echo df echo echo "+----------------------------------------------+" echo uname -a EOL exit 0; sqlplus - works OK: $ENV{'ORACLE_SID'}="test"; $ENV{'ORACLE_HOME'}="/oracle/product/10.2.0"; open(OUTPUT, " > eol_sqlplus_unix_02.out") or die $!; print OUTPUT <<`EOL`; echo "Today is `date`" echo echo "+----------------------------------------------+" echo sqlplus -S "/as sysdba" <<"SQLEND" set heading off alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS' ; select 'Connected to the database on ' || sysdate from dual ; SQLEND echo echo "+----------------------------------------------+" echo EOL exit 0
        I assume you cannot do |- on Windows, is that a correct assumption?
        No. Consider the code:
        # p_1.pl use warnings; use strict; open my $ph, '|-', 'perl p_2.pl' or die 'opening pipe to p_2: $!'; print $ph <<EOP; hi there bye now EOP close $ph or die 'closing: $!'; print "$0 exiting \n"; exit; # p_2.pl use warnings; use strict; print "in $0: \U$_" while <STDIN>; print "in $0: exiting \n"; exit;
        And output:
        >perl p_1.pl in p_2.pl: HI THERE in p_2.pl: BYE NOW in p_2.pl: exiting p_1.pl exiting
        For the rest, please see ikegami's Re^3: Using <<EOL > output.out and EOL.