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


In reply to Re: Re-ordering items by $id from a flat file by Zaxo
in thread Re-ordering items by $id from a flat file by Migey

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.