I'm using open to have microsoft's osql.exe execute some sql for me and save it down to file. This works fine except when the file it saves down to is open. ( like in excel for instance. ) At that point, I get an error printed out to console:
Cannot open output file - c:\test\data\extract.csv No such file or directory

Now I know it's the wrong error getting printed to console, I just want to catch the fact that something bad happenned. how would guys do it?
sub SqlScriptResultsToFile { my %args = ( db_srv => '', db_name => '', db_user => '', db_pass => '', sql => '', filepath => '', separator => ',', columnwidth => 20000, @_ ); my @errors; # # arg validation ... # my $osql = "|c:\\Progra~1\\MSSQL7\\Binn\\osql.exe -n -U$args{db_us +er} -P$args{db_pass} -S$args{db_srv} -d$args{db_name} -o$args{filepat +h}"; $osql .= " -s$args{separator}" if ( $args{separator} ne ' +' ); $osql .= " -w$args{columnwidth}" if ( $args{columnwidth} ne '' +); if ( ! open(PROGRAM,$osql) ) { push @errors, "invalid filepath [$args{$filepath}]"; return ( success => 0, errors => \@errors ); } print PROGRAM $args{sql}; close PROGRAM; return ( success => 1, errors => \@errors ); }

In reply to How would you catch this error? by LanceDeeply

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.