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

Greetings Wise Ones,

I'm trying to reorder menu items listed in a flat db file. I am using a similar sub to accomplish this on a different site that works but is configured a bit differently. This one sort of works but is sending the menu item to the absolute top of the list instead of up one id or down one id. Here is the sub with the problem.
################## sub reordermenu { ################## accesscheck(); @menuitems = Cobra::chomp_database("$datadir/defaults/menu.dat", "0"); open (DATABASE,"<$datadir/defaults/menu.dat"); hold (DATABASE); @menuitems = <DATABASE>; release (DATABASE); close (DATABASE); MENUITEM: for ($ndx=0; $ndx<= $#menuitems; $ndx++) { ($fid,$fmenu_type,$fmenu_show,$fmenu_filename,$fmenu_name,$fme +nu_icon)=split(/\|/,$menuitems[$ndx]); if ($fid eq $info{'m_id'}) { last MENUITEM; } } $swap=false; if ($m_action eq "moveup" && $ndx != 0) { $tondx = $ndx - 1; $swap=true; } elsif ($m_action eq "movedown" && $ndx != $#menuitems) { $tondx = $ndx + 1; $swap=true; } if ($swap) { chomp($menuitems[$ndx]); chomp($menuitems[$tondx]); ($fid,$fmenu_type,$fmenu_show,$fmenu_filename,$fmenu_name,$fme +nu_icon)=split(/\|/,$menuitems[$ndx]); ($tid,$tmenu_type,$tmenu_show,$tmenu_filename,$tmenu_name,$tme +nu_icon)=split(/\|/,$menuitems[$tondx]); $menuitems[$ndx] = "$fid|$tmenu_type|$tmenu_show|$tmenu_file +name|$tmenu_name|$tmenu_icon\n"; $menuitems[$tondx] = "$tid|$fmenu_type|$fmenu_show|$fmenu_file +name|$fmenu_name|$fmenu_icon\n"; } open (DATABASE,">$datadir/defaults/menu.dat"); hold(DATABASE); foreach $rec (@menuitems){ print DATABASE $rec; } release(DATABASE); close (DATABASE); showmenu(); exit; }
Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Re-ordering items by $id from a flat file
by Zaxo (Archbishop) on Jun 22, 2004 at 03:05 UTC

    You appear to be modifying a database file without any file locking. Bad.

    I'm not sure what all your code is meant to do, but I think you could do better with Tie::File or with a simpler way of handling data. You appear to be swapping adjacent pairs of elements according to the values in some magic variables with no apparent origin. As single variables, that seems to mean that you are only swapping one time in all that.

    Lets recognise that an instruction to "moveup" for element n is an instruction to "movedown" for element n-1. That is a more convenient representation for the assumption that the moves will be done You can read a flat file of records into an array, and then replace the elements with a hashref, [Struck, not used in code] or just say,

    use Fcntl ':flock'; use Tie::File; sub reordermenu { # set $info{'m_id'), $m_action somehow # from parameters passed to this. Think # of a better data structure. my $df = tie my @menuitems, 'Tie::File', "$datadir/defaults/menu.dat"; $df->flock(O_EXCL); my @indexes = grep { $info{'m_id'} eq (split /\|/, @menuitems[$_], 2)[0] } 0 .. $#menuitems; for (@indexes) { my $tondx = $m_action eq 'moveup' ? $_ ? $_-1 : $_ : $_ == $#menuitems ? $_+1 : $_; @menuitems[$_, $tondx] = @menuitems[$tondx, $_]; } untie @menuitems; 1; }
    The tied interface writes changes to the file as you make tham. @menuitems is untied at the end to flush writes before losing the lock.

    I've omitted a lot of extra stuff because I don't know what it does. Some looks suspicious, like that exit at the seemingly normal end of the sub. That can't be right.

    After Compline,
    Zaxo

Re: Re-ordering items by $id from a flat file
by hbo (Monk) on Jun 22, 2004 at 04:31 UTC
    I plead "insufficient data" to solve your problem. There's too much left out of the code. Let me comment on the code to indicate why I think so. In what follows, I have adjusted the indentation to match what I'm used to. It helps me to understand the flow.
    ################## sub reordermenu { ################## accesscheck(); @menuitems = Cobra::chomp_database("$datadir/defaults/menu.dat", "0" +); open (DATABASE,"<$datadir/defaults/menu.dat"); hold (DATABASE); @menuitems = <DATABASE>; release (DATABASE); close (DATABASE);
    There's a lot missing here. access_check(), hold() and release() all seem to have obvious meanings, but what the heck does Cobra::chomp_database() do in relation to what follows? It probably doesn't matter, though.
    MENUITEM: for ($ndx=0; $ndx<= $#menuitems; $ndx++) { ($fid,$fmenu_type,$fmenu_show,$fmenu_filename, $fmenu_name,$fmenu_icon)=split(/\|/,$menuitems[$ndx]); if ($fid eq $info{'m_id'}) { last MENUITEM; } }
    The MENUITEM label is superfluous, since there's only the one loop. The if block could be recast in typical Perl postfix style as:
    last if ($fid eq $info{'m_id'});
    Next we have:
    $swap=false; if ($m_action eq "moveup" && $ndx != 0) { $tondx = $ndx - 1; $swap=true; } elsif ($m_action eq "movedown" && $ndx != $#menuitems) { $tondx = $ndx + 1; $swap=true; }
    OK, where does "$m_action" come from? Do we really just swap once in this sub? Are you loading the entire database and saving it for just one swap? Something seems to be missing.
      accesscheck() is a seperate sub that verifies if the user trying to access this function has the proper access level.

      Hold/release are just the new standards for lock/unlock or file_lock/unfile_lock but are not necessary for this sub.

      Cobra is a seperate .pm file for this portal that I don't fully understand yet but was also not necessary for this sub. It can array db items with the one line of code rather than going through the normal open/@/close method.
      $m_action is defined in a previous sub that shows calls all actions and operations for a selected menu being modified.
        OK, but the important question is the last one. Are we doing one swap per call? What is the context of this sub?