Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: chooseDirectory set Default

by olus (Curate)
on Jul 11, 2008 at 13:56 UTC ( [id://696962]=note: print w/replies, xml ) Need Help??


in reply to chooseDirectory set Default

If you want the user to have a directory set by default the next time he runs your program, then you'll have to 'save state'. Try using a config file, where you write the directory the user chose, and have your program reading that file at the begining and look for that info. The first time the program is executed that information will not be there, but there will be something the next time.

update: below is some code you can adapt to include in your program. Error checking on dealing with the file is not in the script, as I don't know TK (and how to, visually, alert the user in case of errors) and I would have the script dieing. Run the script and look at config.txt's contents, and then modify the value of the variable $new_default, run it again and look for differences.

use strict; use warnings; my $file = "config.txt"; my $default_dir = ""; my @config; my $content; if (-f $file) { open CONF, "<$file"; $content = <CONF>; close CONF; if ($content =~ /DEFAULT_DIR/) { @config = split /:/,$content; $default_dir = $config[1]; } } my $new_default = "default_path"; if($new_default ne $default_dir) { open CONF, ">$file"; print CONF "DEFAULT_DIR:".$new_default; close CONF; }

After all this, do go and take a look at open. It is important that you know how to deal with files.

Replies are listed 'Best First'.
Re^2: chooseDirectory set Default
by lil_v (Sexton) on Jul 11, 2008 at 15:19 UTC
    I've never used a config file before. Do you have any examples on how it could be done?

      For something as simple as the program your making (at least you made it appear simple), you can simply write the data in a fashion such as:

      initdir:/home/user initbuttonstate:active

      et cetera.

      Of course, then open the config and gather the information. Depending on the complexity of the config file, you can just split each line on a colon, and use the second variable of the array.

      <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re^2: chooseDirectory set Default
by lil_v (Sexton) on Jul 11, 2008 at 20:28 UTC
    thx

      Sure mate. But this time you gotta promise you will read open's docs :).

      First point: when you read the file, and since you are adding more lines, you have to get all those lines. Also, you are appending a new line to the end of each conf variable, so there is the need to chomp that new line.

      if (-f $file) { open CONF, "<$file"; my @contents = <CONF>; close CONF; foreach $content (@contents) { my @config = split /-/,$content; if ($content =~ /DEFAULT_IN/) { $default_in = $config[1]; chomp($default_in); } elsif ($content =~ /DEFAULT_OUT/) { $default_out = $config[1]; chomp($default_out); } } }

      Second point: Writing the info back to the file. I don't know what those browse methods are, but if you need to write in two separate steps, then you'll have to write all the info both times.

      print CONF "DEFAULT_IN-$def_path\nDEFAULT_OUT-$def_path_out\n";

      As a side note, you should look for a way of being able to write only if any of the paths actually changed.

      if( ($def_path ne $default_in) || ($def_path_out ne $default_out) ) { # at least one of the values changed, write both }

      update lil_v updated his node, so now this doesn't make much sense.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://696962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found