Re: open > file && s/ / /;



Brian Volk wrote:
> Hi all,

Hello,

> Can someone pls take a look at the script below and explain what I'm doing
> wrong.. The script runs w/out errors but the substitution is not working..
> Hopefully the note below will be enough info..
>
> I was able to print the file names that I wanted but once I tried to open
> the file and s / / /; the wheels fell off.. :~)
>
> I think I can open a file this way....? Pls explain what I am doing
> wrong....
> Thank you!
> ---------------------------------
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> # dir w/ text files containing text and links
> my $dir = "J:/flash_host/ecomm/descriptions/product/small";
>
> # a text file that has the file names which contain broken links
> my $bad_file = "c:/brian/spartan/bad_links.txt";
>
> open BAD, "< $bad_file" or die "Can't read $bad_file: $!\n";
>
> # name each record in $bad_file => $file
> while (my $file = <BAD>) {
> chomp $file;
>
> if (-e "$dir/$file") {
>
> # open the text file w/ a bad link and sub "http://..."; w/ a local
> file
> open BADFILE, "> $dir/file" or die "Can't open $dir/$file for replace:
> $!\n" &&
> s & http://.* & descriptions/product/MSDS/$file &;
> close BF
> }
>
> }
> -----------------------------------

Perl provides some shortcuts to allow you to do what you want:

#!/usr/local/bin/perl

use strict;
use warnings;

# dir w/ text files containing text and links
my $dir = 'J:/flash_host/ecomm/descriptions/product/small';

# a text file that has the file names which contain broken links
my $bad_file = 'c:/brian/spartan/bad_links.txt';

open BAD, '<', $bad_file or die "Can't read $bad_file: $!\n";

# store the files to "edit" in @ARGV
@ARGV = map { chomp; "$dir/$_" } <BAD>;

# set the in-place edit variable
# cannot be '' on Windows
$^I = '.bak';

# modify the files and save the originals with .bak extention
while ( <> ) {
s & http://.* & descriptions/product/MSDS/$file &;
print;
}

__END__



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: When were @- and @+ added
    ... >>)) use warnings; ... >>)) If I don't 'use warnings;' in that script, ... >> I can't reproduce that with any version of Perl that supports @-. ... Binary build 815 provided by ActiveState ...
    (comp.lang.perl.misc)
  • Re: Can not add a scalar variable of path into @INC?
    ... invoking another Perl script from here. ... use warnings; ... unshift @INC, $mylib; ...
    (comp.lang.perl.misc)
  • Re: strange behavior of -s $filename
    ... readdir) that have a pattern and are not empty and i am using the ... Many, many people don't write Perl code with warnings turned on and, as ... your Perl script, so there's no real reason not to use them. ...
    (comp.lang.perl.misc)
  • Re: Why doesnt this UserAgent script work?
    ... >> It works just find, with strict and warnings, from the command ... >probably meant $response, though. ... should always test the script I am going to post. ... >It's the web server which complains - not Perl. ...
    (comp.lang.perl.misc)
  • Re: my first perl script!
    ... Matthias Wille wrote: ... > could take a short look at my script an tell me if it is good perl ... then either warnings or strictures would have found this bug. ...
    (comp.lang.perl.misc)