Whazzzup?

So, I searched, super searched, cracked open the Nutshell, and still can't find a better way to do this aweful thing (break out of the while loop once the script finds the drive letter it's looking for:
open PLAY, $playlist . ".wpl"; # Get original drive letter my $original_drive_letter; while (<PLAY>) { print "$counter\n"; if (/src=\"(.)\:\\/) { $original_drive_letter = $1; close PLAY; } }
Simple. Read the file line by line but stop once Perl finds the drive letter. But there's this pesky readline on closed filehandle warning. Any better way to do this?

So what's this code supposed to do, anyways?
My mp3's live on an external hardisk. Stupid windows doesn't always assign the same drive letter to the drive, so my playlists break, dag nabit. So, I came up with this:
#!perl use strict; use warnings; print "Which playlist do you need to update?\n"; chomp (my $playlist = <STDIN>); $playlist =~ s/(.*)\.wpl/$1/; open PLAY, $playlist . ".wpl"; my $original_drive_letter; my $counter=0; # get drive while (<PLAY>) { ++$counter; print "$counter\n"; if (/src=\"(.)\:\\/) { $original_drive_letter = $1; close PLAY; } } print "It appears the original drive letter is $original_drive_letter. +\n"; print "What would you like the new drive letter to be?\n"; chomp (my $new_drive_letter = <STDIN>); my $no_integrity = 1; while ($no_integrity) { if ($new_drive_letter =~ /^[a-zA-Z]?$/) { print "You want the new drive letter to be $new_drive_letter.\n"; $no_integrity = 0; } else { print "$new_drive_letter doesn't seem to be a valid drive letter. +Let's try again.\n"; print "What do you want the new drive letter to be?\n"; chomp ($new_drive_letter = <STDIN>); $no_integrity = 1; } } print "\nUpdating playlist."; open PLAY, $playlist . ".wpl"; open WRITE, ">temp.wpl"; while (<PLAY>) { s/(="$original_drive_letter:\\)/="$new_drive_letter:\\/g; print WRITE "$_"; print "."; } print "\n\nFinished updating playlist."; close PLAY; close WRITE; unlink ($playlist . ".wpl"); rename "temp.wpl", $playlist . ".wpl" or warn "Couldn't rename: $!";
Media player is too dense to figure things out on its own. Perl 5.9, you're my hero.
Thanks for your advice!

Pizerl is the Bizomb, baby!

In reply to Breaking out of a while loop: Is there a better way? by BubbaMonk

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.