Vonunov has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File manipulation only works when I split it into two files.
by lostjimmy (Chaplain) on Dec 03, 2008 at 01:03 UTC | |
by Anonymous Monk on Dec 03, 2008 at 01:09 UTC | |
by oko1 (Deacon) on Dec 03, 2008 at 02:31 UTC | |
|
Re: File manipulation only works when I split it into two files.
by jethro (Monsignor) on Dec 03, 2008 at 01:16 UTC |