in reply to Re: Using <<EOL > output.out and EOL
in thread Using <<EOL > output.out and EOL

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

Replies are listed 'Best First'.
Re^3: Using <<EOL > output.out and EOL
by ikegami (Patriarch) on Nov 30, 2009 at 07:52 UTC

    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?

      Hi ikegami,

      You are correct, am using Windows. I normally refer to CMD.EXE as DOS even though I mean it is the MSDOS Console

      Not sure I understand your question on "How come you're not feeding the input to sqlplus plus as initially suggested?". Anyway, I think first is because I want the Perl script to run on both Windows and UNIX so I cannot use bash. I do not want to fork a separate shell as well so I took it off the bash. Because there will be two <<EOL/EOL combination, I found out the inner one need to be enclosed in DOUBLE QUOTE

      On CMD.EXE, if I do:

      open(OUTPUT, " > eol_dos.out") or die $!; print OUTPUT <<EOL; dir EOL

      eol_dos.out contains the literal dir instead of the output from the command dir. If I change <<EOL to <<`EOL` then I get the output from the dir command.

      On *nix, I don't need to do <<`EOL`, i.e. <<EOL will do but am hoping that I do not need to have different versions for Windows and *nix so since <<`EOL` works for both *nix and Windows, I opted for that instead.

      The sqlplus is working on *nix but not on Windows. Don't know what I need to do to get it to work on Windows.

      Any suggestion on the sqlplus thing for Windows will be very much appreciated. I've run out of what else to try. BTW, reason why opting to do it this way is so I do not need to supply username and password to sqlplus as I cannot get SYSDBA connection to work on DBI:DBD. Already asked Pythian to confirm whether that option works on DBI:DBD but no response so far.

      BTW, out of curiosity, do you type in your comments/reply typing <p> and </p> manually as well?

      Thanks again for your response.

        On *nix, I don't need to do <<`EOL`, i.e. <<EOL will do

        That's not true. Taking the same code,

        open(OUTPUT, " > eol_dos.out") or die $!; print OUTPUT <<EOL; dir EOL
        >ver Microsoft Windows XP [Version 5.1.2600] >perl a.pl && type eol_dos.out dir
        open(OUTPUT, " > eol_dos.out") or die $!; print OUTPUT <<EOL; ls -1 EOL
        $ uname Linux $ perl a.pl && cat eol_dos.out ls -1

        The sqlplus is working on *nix but not on Windows

        No. Like I said in my previous post, your sh shell command works in sh but not in cmd.

        I do not want to fork a separate shell as well so I took it off the bash.

        But that's you're doing. You're asking Perl to execute shell command

        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
        so it launches a shell to execute it.

        Any suggestion on the sqlplus thing for Windows will be very much appreciated.

        Yes, stop invoking the shell. Create a pipe to sqlplus and pipe the data to it instead of asking the shell to do that.

        Ideally, that would be

        my @cmd = ( sqlplus => ( '-S', '/as' => 'sysdba', ) ); open(my $to_sqlplus, '|-', @cmd) or die("Can't launch sqlplus: $!\n"); print $to_sqlplus <<'__EOI__'; ... __EOI__ close($to_sqlplus);

        But no-one has gotten around to implementing multi-arg open |-. So you can use

        use IPC::Open2 qw( open2 ); my @cmd = ( sqlplus => ( '-S', '/as' => 'sysdba', ) ); local *TO_SQLPLUS; my $pid = open2('>&STDOUT', *TO_SQLPLUS, @cmd) or die("Can't launch sqlplus: $!\n"); print TO_SQLPLUS <<'__EOI__'; ... foo bar ... __EOI__ close(TO_SQLPLUS); waitpid($pid,0);

        BTW, out of curiosity, do you type in your comments/reply typing <p> and </p> manually as well?

        Yes. Well not </p>. It's optional in HTML.