On 2/10/10 Wed Feb 10, 2010 3:07 PM, "newbie01 perl"
<[email protected]> scribbled:
> Hi all,
>
> We are in the process of changing UNIX and MS-DOS scripts into Perl, mostly
> UNIX scripts and there are a lot of codes that uses the <<EOF and EOF pair.
> Am just wanting to know if there are better alternative to what I've been
> doing so far.
>
> Example of these are as below:
>
> Example 01:
>
> UNIX script version:
> num_records=`sqlplus -S "/as sysdba" << EOF | sed 's# *##g'
> set heading off
> set feedback off
> set pages 0
> set echo off
> set trimspool on
> select count(1) from all_tables where owner = '${owner}';
> exit
> EOF`
>
> Changed to Perl - this work alright, is there a better way of doing this?
>
> $owner = 'SYSMAN';
> $num_records=`sqlplus -S "/as sysdba" << EOF | sed 's# *##g'
> set heading off
> set feedback off
> set pages 0
> set echo off
> set trimspool on
> select count(1) from all_tables where owner = '${owner}';
> exit
> EOF`;
> print $num_records;
>
> exit 0;
Rather than shelling out and creating an external process to run sqlplus,
you should consider using a Perl database module to allow your program to
connect to the database directly. Checkout the DBI module and a DBD module
for your particular database. This will require a significant rewriting
effort, however.
>
>
> Example 02:
>
> UNIX script version:
> sqlplus -S "/as sysdba" << EOF
...
> EOF
>
> Changed to Perl - this work alright, is there a better way of doing this?
>
> `sqlplus -S "/as sysdba" << EOF
Use the system function if you do not want to capture the output of the
sqlplus program.
>
> Example 03:
>
> UNIX script version:
>
> cat /dev/null > eol_unix.out
> cat >> eol_unix.out << EOF
> `( ls ;
> echo ;
> echo "+----------------------------------------------+" ;
> echo ;
> df ;
> echo ;
> echo "+----------------------------------------------+" ;
> echo ;
> uname -a )`
> EOF
>
> Changed to Perl - this work alright, is there a better way of doing this?
>
> open(OUTPUT, " > eol_unix.out") or die $!;
Better to use the three-parameter version of open and lexical file handle
variables:
open( my $output, '>', 'eol_unix.out' ) or die("Can't open eol_unix.out:
$!");
> print OUTPUT <<`EOL`;
print $output ...
> ls
> echo
> echo "+----------------------------------------------+"
> echo
> df
> echo
> echo "+----------------------------------------------+"
> echo
> uname -a
> EOL
>
>
> Any feedback will be very much appreciated. Thanks in advance
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/