Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl script does not work

Reply
Thread Tools

Perl script does not work

 
 
CSUIDL PROGRAMMEr
Guest
Posts: n/a
 
      04-29-2006
Folk
I am trying hard to run this script on fedora 3

It changes the following pattern in xml files

<!CDATA[slaaam]]> TO

<slaaam>

But it does not work. Any reasons


The script is a as follows

#!/usr/bin/perl
use strict;
use warnings;
my $start_dir = "/home/amjad/chatTrackBot_v1.5/xml_logs";
chdir($start_dir) or die "Can't chdir $start_dir: $!";
my @files = <*.xml>;
my @errors = ();
foreach my $file ( @files ) {
open(OLD, "<$file") or push @errors, "failed to open $file: $!";
open(TEMP, ">temp.txt") or push @errors, "failed to create temp
file: $!";
while (my $line = <OLD>) {
$line =~ s/<!\[CDATA\[([^\]]*)\]\]>/$1/ig;

print TEMP $line;
}
close OLD;
close TEMP;
rename('temp.txt', $file) or push @errors, "Rename failed: $!";
}
if (@errors) {
print "$_\n" for @errors;
}
else {
print "No errors";
}

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      04-29-2006

"CSUIDL PROGRAMMEr" <> wrote in message
news: oups.com...
> Folk
> I am trying hard to run this script on fedora 3
>
> It changes the following pattern in xml files
>
> <!CDATA[slaaam]]> TO
>
> <slaaam>
>
> But it does not work. Any reasons
>



How does it not work? What you want it to do is half the equation. What it's
actually doing is also required for people to have any way of helping you.


>
> The script is a as follows
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $start_dir = "/home/amjad/chatTrackBot_v1.5/xml_logs";
> chdir($start_dir) or die "Can't chdir $start_dir: $!";
> my @files = <*.xml>;
> my @errors = ();
> foreach my $file ( @files ) {
> open(OLD, "<$file") or push @errors, "failed to open $file: $!";
> open(TEMP, ">temp.txt") or push @errors, "failed to create temp
> file: $!";
> while (my $line = <OLD>) {
> $line =~ s/<!\[CDATA\[([^\]]*)\]\]>/$1/ig;


You're reading the file line-by-line here, so the assumption is the CDATA
block is all on one line. I wouldn't think that's a safe assumption to make.
You might want to consider using a real XML parser.

Matt


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-29-2006
CSUIDL PROGRAMMEr wrote:
> It changes the following pattern in xml files
>
> <!CDATA[slaaam]]> TO
>
> <slaaam>
>
> But it does not work. Any reasons


Yes, the regex doesn't match.

> $line =~ s/<!\[CDATA\[([^\]]*)\]\]>/$1/ig;

----------------------^^

Try

$line =~ s/<!CDATA\[([^\]]*)\]\]>/<$1>/ig;
--------------------------------------^--^

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-29-2006
"CSUIDL PROGRAMMEr" <> wrote in
news: oups.com:

> foreach my $file ( @files ) {


FILES: foreach my $file ( @files ) {

> open(OLD, "<$file") or push @errors, "failed to open $file: $!";


It is good that you check for errors, but

* Further down, you still attempt to read from this file. You should
stop processing this file if you cannot open it

* Use lexical filehandles.

* perldoc -q vars

* You don't need the paratheses in the open call as you are using the
lower precedence or rather than ||.

* Use the three argument form of open.

unless ( open my $old, '<', $file ) {
push @errors, "Failed to open '$file': $!";
next FILES;
}

> open(TEMP, ">temp.txt") or push @errors, "failed to create temp
> file: $!";


Ditto.

These are in addition to comments by others.

Sinan

--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-29-2006
"A. Sinan Unur" <> wrote:

> * Use the three argument form of open.
>
> unless ( open my $old, '<', $file ) {
> push @errors, "Failed to open '$file': $!";
> next FILES;
> }


Why?


--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-29-2006
Tim Hammerquist <> wrote:

> John Bokma <> wrote:
>> "A. Sinan Unur" <> wrote:
>> > * Use the three argument form of open.
>> >
>> > unless ( open my $old, '<', $file ) {
>> > push @errors, "Failed to open '$file': $!";
>> > next FILES;
>> > }

>>
>> Why?

>
> Be cause there's no longer MTOWTDI.


There always is

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-29-2006
Tim Hammerquist <> wrote in
news::

> John Bokma <> wrote:
>> Tim Hammerquist <> wrote:
>>> John Bokma <> wrote:
>>>> "A. Sinan Unur" <> wrote:
>>>> > * Use the three argument form of open.
>>>> >
>>>> > unless ( open my $old, '<', $file ) {
>>>> > push @errors, "Failed to open '$file': $!";
>>>> > next FILES;
>>>> > }
>>>>
>>>> Why?
>>>
>>> Be cause there's no longer MTOWTDI.

>>
>> There always is

>
> No. This is Pyth^W the New Perl.
>
> (The following is *not* directed at Mr. Bokma.)


....

> To bring this slightly back on topic, I can't find any place
> where perlfunc says the 1- or 2-arg form of open() is
> deprecated. In fact, it mentions several conveniences offered
> by the 1-/2-arg form.


I did not go into detail, because I thought it wasn't as important an
issue as attempting to read from and write to possibly unopened
filehandles.

While '>temp.txt' is quite safe, with "<$file", $file is interpolated.
That interpolation can have unintended effects.

In the OP's case, there is no guarantee on what characters may appear in
the filename. In that particular case, perldoc -f open specifically
recommends the 3-argument form:

<blockquote>
Use 3-argument form to open a file with arbitrary weird
characters in it,

open(FOO, '<', $file);

otherwise it's necessary to protect any leading and trailing
whitespace:

$file =~ s#^(\s)#./$1#;
open(FOO, "< $file\0");

(this may not work on some bizarre filesystems). One should
conscientiously choose between the *magic* and 3-arguments form
of open():
</blockquote>

It can also be easier to read because it separates the mode from the
file name.

If the OP had limited the possible filenames to be processed by
filtering them in some way, I would not have made the recommendation.

Therefore, I find it better to stick with the three argument form unless
there is a specific reason to prefer the two argument form.

Sinan

--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-29-2006
"A. Sinan Unur" <> wrote:

> While '>temp.txt' is quite safe, with "<$file", $file is interpolated.
> That interpolation can have unintended effects.


My bad, I hadn't seen the OPs "<$file". I read it as: use

open my $fh, '<', $filename ...

I prefer:

open my $fh, $filename ...

Unless:

> In the OP's case, there is no guarantee on what characters may appear in
> the filename. In that particular case, perldoc -f open specifically
> recommends the 3-argument form:


[...]

> If the OP had limited the possible filenames to be processed by
> filtering them in some way, I would not have made the recommendation.
>
> Therefore, I find it better to stick with the three argument form unless
> there is a specific reason to prefer the two argument form.


clear, thanks. Should have had a better peek at the OP's post, apologies.

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      04-30-2006
John Bokma wrote:

>My bad, I hadn't seen the OPs "<$file". I read it as: use
>
>open my $fh, '<', $filename ...
>
>I prefer:
>
>open my $fh, $filename ...


Which is even less safe than

open my $fh, "<$filename"


--
Bart.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ calling perl script - Not able to get the stack arguments pushedrom XPUSH in perl script Yogi Perl Misc 1 09-13-2012 11:30 AM
PLEASE HELP! Perl script does not work! Gandu Perl Misc 9 11-22-2004 07:18 AM
Sample perl code does not work (from Perl cookbook, 2nd ed.) Staale Perl Misc 16 09-30-2004 12:17 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57