I finally understand the tao of slices. After reading through Effective Perl Programming this weekend, I have figured out a problem that was holding me back. While I am sure that the majority of you have had this epiphany long ago, I feel the need to beat my chest and howl about it...

It began, while I was trying to read in, modify and write out shadow file entries:

open(TEMPFILE, ">${TempPath}/temp_shad") || die; open(SHADFILE, "${TempPath}/$passwdfile") || die; while (<SHADFILE>) { chomp; (@entry) = split(/:/,$_); $newentry = join(":", @entry); print TEMPFILE "$newentry\n";

This didn't work, because, as you know (if you are a *nix SA), the shadow file looks like so: foo:asDflkj123./d:6445::::::

So when I wrote it back out,this is what I got:

foo:asDflkj123./d:6445

In comes the concept of slicing <*trumpet sounds*>. Thru the slice, I was able to define those empty fields, while not having to worry about assigning bogus values that I would have to delete. Also, I didn't have to deal w/ trying to assign each field to an individually defined variable and the long lines of code that followed:

($user,$passwd,$mod,$f1,$f2,$f3,$f4,$f5) = split(/:/,$_); $newentry = "$user" . ":" . "$passwd" . ":" . "$mod" . ":" . "$f1" . " +:" . "$f2" . ":" . "$f3" . ":" . "$f4" . ":" . "$f5)\n";
The above code works, but geez, I don't want to have to type all of that each time I need to modify the shadow file.

That code became this:

(@entry[0..8]) = split(/:/,$_); $newentry = join(":", @entry[0..8]);
In short, it made a potentially arduous task, very simple and easier to read.

Now, back to the regularly scheduled program...

Enlightened,

bb

update: fixed # of colons in shadow file entry, thanks davorg


In reply to I am now slice-aware by birdbrane

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.