Without remarking on the program logic or identifier naming convention, here's how that sub should look:
sub Save { my $DirName = 'WinLog'; DirCreator($DirName); foreach (@arr) { my $Log_obj = Win32::EventLog->new($_, ""); my $FileName = "$_.evtx"; $Log_obj->Backup(".\\$DirName.\\$FileName"); given ($^E) { when ('') { print "Log Successfully saved.\n"; } when (/already exists/) { # ... } default { print "Error: $^E\n"; } } $Log_obj->Close; # see Win32::EventLog's docs } }

Here are some helpful hints.

You don't need to escape periods in strings or spaces in regexes. You don't have to preceed function calls with '&'. You don't have to quote variables containing strings if there's nothing else inside the quotes.

When you quoted "$Log_obj" and passed that to close(), you sent a human-readable string that resembles that object, but not the object itself. When you create lexical filehandles, they are closed automatically when the variable goes out of scope. Most importantly, though, $Log_obj is not a filehandle. You needed to do $Log_obj->Close;, which calls the Close method of Win32::EventLog in the context of that object, not the builtin close function.

Declare lexical variables (variables scoped to the inner-most block) with "my". When you use "our", you're declaring a variable in the symbol table, so other modules and packages can access it; if you're just looking to define a module that's scoped to a package, declare it outside of a sub (or other block), and it's accessible to all code in the package.

The Switch module is buggy and deprecated. In Perl 5.10 and later, do use feature 'switch'; to enable the given/when construct, as I illustrated. If you're using an earlier version of Perl, use if/else constructs or something lke for ($value) { if (/foo/) { bar(); next; } }.

By default, $\ will usually be undefined, so you'll want a newline at the end of a print, or you might suffer from buffering.

And don't forget strictures: "use strict; use warnings;".


In reply to Re: Win32::EventLog->Backup(), appending contents of new log to a saved one. by Anonymous Monk
in thread Win32::EventLog->Backup(), appending contents of new log to a saved one. by dannyd

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.