Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Any ideas on updating Windows INI files with Perl

Expand Messages
  • Charles K. Clarkson
    I wrote this sub while trying to learn how wantarray worked. It uses a hash of hashes to store and write a ini file. It should get you started. If you need
    Message 1 of 1 , Feb 2, 2001
    • 0 Attachment
      I wrote this sub while trying to learn how wantarray
      worked. It uses a hash of hashes to store and write a
      ini file. It should get you started. If you need help
      feel free to ask - documentation is not my forte.

      sub items {

      #
      # To use:
      # %ini = items ($ini_file);
      # $ini_ref = items ($ini_file);
      # items ($out_file, %ini);
      # items ($out_file, $ini_ref);
      #
      # If items is asked to return a hash:
      # %ini = items ($ini_file);
      # A hash of hashes will be returned.
      #
      # If items is asked to return a scalar:
      # $ini_ref = items ($ini_file);
      # A reference to a hash of hashes is returned.
      #
      # If items called in void context:
      # items ($out_file, %ini);
      # The hash of hashes is written to the indicated file.
      # items ($out_file, $ini_ref);
      # The reference is translated and written to the indicated file.
      #
      # The hash of hashes:
      #
      # Our file looks like this:
      # [MAIN]
      # MenuOptions = 1, 4
      # WindowPosition = 0,1,0,2,5,45
      #
      # [OPTIONS]
      # Settings = Resusfile.ini
      # Port = 2
      # Speed = 100%
      #
      # The big hash contains MAIN and OPTIONS which are each hashes
      # To set MenuOptions use:
      # $ini{MAIN}{MenuOptions} = "1, 2";
      # To retrieve MenuOptions use:
      # $option = $ini{MAIN}{MenuOptions};

      if (defined wantarray) { # get items
      my %return;
      my $heading;
      open (IN, $_[0]) || die "Cannot open $_[0]: $!";
      while (<IN>) {
      $heading = $1 if /^\[(.+)\]/;
      $return{$heading}{$1} = $2 if /(\w+)\s=\s(.+)/;
      }
      close IN;
      return wantarray ? %return : \%return;

      } else { # store items
      my $file = shift;
      my %ini;
      # if a refence was sent, convert it to a hash
      %ini = ref($_[0]) eq "REF" ? %{$_[0]} : @_;
      open (OUT, ">$file") || die "Cannot open $file: $!";
      foreach my $heading ( keys %ini ) {
      print OUT "[$heading]\n";
      foreach my $key ( sort keys %{ $ini{$heading} } ) {
      print OUT "$key = $ini{$heading}{$key}\n";
      }
      print OUT "\n";
      }
      close OUT;
      return 1;
      }
      }

      ----- Original Message -----
      From: <daymobrew@...>
      > I am using ActiveState's 5.6 distribution of Perl.
      >
      > I'm writing a Perl program to call InstallShield's PackageForTheWeb.
      > Before I call PFTW, I have to modify the data file it uses. It is a
      > Windows INI file format.
      > I wrote a Windows command line program to update INI files,
      > WriteToINIFile.exe. You specify the INI file, section, key and
      > data on the command line. I'd rather do all the work within Perl so I
      > don't have to remember to include this executable when I use a
      > different machine.
      >
      > I thought about using the Win32::API module and calling
      > WritePrivateProfileString but this module is not included in the
      > standard ActiveState distribution. I would like to avoid it for the
      > same reasons as I'd like to avoid using the Windows command line
      > program.
      >
      > So, are there any standard Perl functions that would make updating an
      > INI file easy?
      > It is not essential I find an alternative to WinToINIFile.exe but I'd
      > like to hear some suggestions.
    Your message has been successfully submitted and would be delivered to recipients shortly.