Hello, I'm not too experienced with Perl and am never motivated to learn until I actually need something done, so sorry this isn't all eloquent and concise. :P

I have a list of URLs in a text file. I often add incomplete URLs or directory paths to the list in haste (for instance, missing http:// or the entire domain name itself), so I wrote up a perl script to add this automatically. I also want it all kept in lowercase to make it easier to grep, and I tend to copy lists of URLs that are not all lowercase. I originally used this:

#!/usr/bin/perl #This is c.pl open (file, "petpages"); open NEW, ">", "petpages.new" or die $!; while ($lines = <file>) { $lines =~ tr/A-Z/a-z/; print NEW $lines; } close (file); exec("rm petpages ; mv petpages.new petpages ; perl c2.pl");

Which referred to this file:

#!/usr/bin/perl #This is c2.pl open (file, "petpages"); open NEW, ">", "petpages.new" or die $!; while ($lines = <file>) { print NEW "http://" . $lines; } close (file); exec("rm petpages ; mv petpages.new petpages");

I thought just today to put it all into one file this way:

#!/usr/bin/perl open (file, "petpages"); open NEW, ">", "petpages.new" or die $!; while ($lines = <file>) { $lines =~ tr/A-Z/a-z/; print NEW $lines; } close (file); exec("rm petpages ; mv petpages.new petpages"); open (file, "petpages"); open NEW, ">", "petpages.new" or die $!; while ($lines = <file>) { print NEW "http://" . $lines; } close (file); exec("rm petpages ; mv petpages.new petpages");

This completed case conversion but failed to prepend http:// to each line as the pair of files did successfully.

With warning flag on, it threw me this:

Unquoted string "file" may clash with future reserved word at c.pl lin +e 3. Unquoted string "file" may clash with future reserved word at c.pl lin +e 9. Unquoted string "file" may clash with future reserved word at c.pl lin +e 11. Unquoted string "file" may clash with future reserved word at c.pl lin +e 16. Statement unlikely to be reached at c.pl line 11. (Maybe you meant system() when you said exec()?)

I followed the meaning of this somewhat, and changed the handles (note file vs. file2):

#!/usr/bin/perl open (file, "petpages"); open NEW, ">", "petpages.new" or die $!; while ($lines = <file>) { $lines =~ tr/A-Z/a-z/; print NEW $lines; } close (file); exec("rm petpages ; mv petpages.new petpages"); open (file2, "petpages"); open NEW, ">", "petpages.new" or die $!; while ($lines = <file2>) { print NEW "http://" . $lines; } close (file2); exec("rm petpages ; mv petpages.new petpages");

However, this also fails to prepend http://, and returns:

Unquoted string "file" may clash with future reserved word at c.pl lin +e 3. Unquoted string "file" may clash with future reserved word at c.pl lin +e 9. Statement unlikely to be reached at c.pl line 11. (Maybe you meant system() when you said exec()?)

I'm pretty much lost at this point. Any suggestions? Also, is exec() outdated?

Thanks,

Jack


In reply to File manipulation only works when I split it into two files. by Vonunov

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.