I would just switch the open mode, depending on whether it's a 2005 or pre-2005 logfile. In its most simple case, looking for the "2005" in the pathname

my $openmode = $sqlErrorlog =~ /2005/ ? ">:encoding(utf-16)" : ">"; unless (open LOG, $openmode, $sqlErrorlog) { ...

(or, as a refinement, you could also check if the year is >= 2005 — presuming they switched to unicode with SQL Server 2005, and future versions will adhere to the same naming scheme, i.e. 2007, 2009, ...   though that might be too many presumptions :)

However, that approach is kinda cheating, and not generally recommended programming practice (reason: when you/someone happens to change the pathname some time in the future, it's very unlikely you're going to remember this curious dependency buried deep down in the code...).

So, another way would be to test for the existence of the BOM, and if there, set UTF-16 mode. Actually - as I only recently learned myself - there's already a module for this: File::BOM, which handles the issue even more generally... (see the docs for examples).


In reply to Re^5: How to open SQL 2005 errorlog? by almut
in thread How to open SQL 2005 errorlog? by jc7

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.