Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

rather unclear for a title -- i'm sorry

i'm handling my sessions like this:

sub sessionValidate { $sessionFile = "$systemRoot/sessions/$FORM{sessionID}"; if ( (open FILE, "${sessionFile}.db"){ close FILE; dbmopen %stored, $sessionFile, 0655; %session = %stored; dbmclose %stored; return 1; } else {; return 0; } } sub sessionWrite { do { $sessionID = md5_hex($session{user_email},time(),r +and(100000)); $sessionFile = "$systemRoot/sessions/${sessionID}"; if (open FILE, "${sessionFile}.db"){ close FILE; $valid = 0; } else { $valid = 1; } } until $valid = 1; # not necessary -- chances are 1/10 +00000 that we'll have a session id already, but still... dbmopen %stored, $sessionFile, 0666; %stored = %session; dbmclose %stored; return $sessionID; }
this all works fine on my mac osx machine -- where dbmopen files are .db -- but on my debian box, dbmopen creates a .dir and .pag

short of writing for both .db and .dir/.pag can a monk enlighten me?

Replies are listed 'Best First'.
Re: dbmopen - if db file do this else do that
by dws (Chancellor) on May 03, 2002 at 08:52 UTC
    short of writing for both .db and .dir/.pag can a monk enlighten me?

    There's no easy answer to this one. The file extension is going to depend on the versio of DBM that Perl is linked with. If you're only worried about the pair of platforms, set $extension based on testing $^O (OSNAME). And you can avoid the overhead of open() by using -f.

Re: dbmopen - if db file do this else do that
by rinceWind (Monsignor) on May 03, 2002 at 09:58 UTC
    Also, please note that dbmopen is a deprecated feature, and may well disappear in a future Perl release. There are already platforms which do not support it - VMS for example.

    You should look to use tie instead, and the module AnyDBM_File which comes in the core distribution. See perldoc perltie for more on tie.

Re: dbmopen - if db file do this else do that
by perlplexer (Hermit) on May 03, 2002 at 13:03 UTC
    I don't know how this works on MACs but for PC and *N*X you don't need to specify the extension at all; it will be appended automatically.

    I'm guessing your problem is related to the following (rather strange) piece of code:
    if ( (open FILE, "${sessionFile}.db"){ close FILE; dbmopen %stored, $sessionFile, 0655; %session = %stored; dbmclose %stored; return 1; } else {; return 0; }
    Are you trying to make sure that the DBM file exists before opening it? If so, you can do it in a somewhat more portable way:
    sub sessionValidate{ my ($session, $file) = @_; my %stored; return 0 unless dbmopen %stored, $file, 0444; %$session = %stored; dbmclose %stored; }
    This way you let the library deal with naming conventions. Note also that I changed your subroutine to not use global variables. You can call it as follows:
    if ( sessionValidate(\%session, "$systemRoot/sessions/$FORM{sessionI +D}") ){ # ... }else{ # ... }
    --perlplexer
Re: dbmopen - if db file do this else do that
by Stegalex (Chaplain) on May 03, 2002 at 12:45 UTC