Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What could potentially be wrong in this script?

Reply
Thread Tools

What could potentially be wrong in this script?

 
 
Chris
Guest
Posts: n/a
 
      03-28-2007
I am writing a Perl script to check that dependency files exist that
are included in definition files that we have. However, I am getting
a completely unexplainable problem. For one specific file only, the
algorithm I have doesn't seem to work, or at least the string that is
getting saved from the regex is somehow not working properly with
certain Perl calls. The format of the strings is the same in every
file and in this one in particular, I have even modified the line,
moved it around, etc, etc. I am basically going through each file,
opening it, and then based on the type of file, saving a regex to
use. In one case, "^#tagdef\\s*(.*)" and in the other "^#include\
\s*(.*)" (they are saved off in variables. As I read each line of the
file, I am searching for the currently switched on regex, and then
using $1 to see what I have in the (), which is the file I want to
search for. This file is saved in a list of files that I gather (and
this file is in the list, I've printed and see it is there). I do a
foreach() on this file list, and then use index(fileInlist, $1) != -1
to indicate I have found the file. However, for only one specific
file, this doesn't work. What is even stranger is, if I print "$1\n",
the file prints just fine. But, if I do something like print "$1 after
\n", the whole output is messed up. If I print "before $1\n", nothing
prints at all. If I print "before $1 after\n", only after prints.

Here is a cut-paste of my script:

#!/usr/bin/perl -w

use lib "./scripts/perl/FILES-1.0";

use strict;
use Files::FileModules;
use Cwd;

my $boxPath = $ARGV[0];
my $myPath = "path";

my $startingDir = getcwd;

my @filePostfix = ("\.def\$", "\.cfg\$");

chdir("$boxPath/$myPath");

my @myFiles = FindAllFiles(@filePostfix, "./");

my @myFileCopy = @myFiles;

my $DEFILE = 0;
my $CFGFILE = 1;

foreach my $files (@myFiles)
{
my $searchTag = " ";

if(index($files, 'imgGame') != -1)
{
next;
}

open FILEHANDLE, "< $files" or die "Can't open $files\n";

print "\nIn File $files\n";

if($files =~ /\.def$/)
{
$searchTag = "^#include\\s*(.*)";
}
elsif($files =~ /\.cfg$/)
{
$searchTag = "^#tagdef\\s*(.*)";
}

while(<FILEHANDLE>)
{
if($_ =~ /$searchTag/)
{
my $findThis = $1;
my $foundFile = 0;
print "\nSearching for $findThis\n";
foreach my $thing (@myFileCopy)
{
if(index($thing, 'imgGame') != -1)
{
next;
}

if(index($thing, $findThis) != -1)
{
print "\nFound dependency: $findThis\n";
$foundFile = 1;
last;
}
}
die "Did not find: $findThis in $files\n" if !$foundFile;
}
}

close FILEHANDLE;
}


The index call isn't working on this bizarre string. However, if I do
things like length() on it, it shows the correct length, but other
calls, even the die call at the end, can't print it out. Only if it
is printed by itself with nothing else does it even print out.

 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      03-29-2007
On Mar 29, 1:36 am, "Chris" <foureightye...@yahoo.com> wrote:

[snip]

> if I print "$1\n",
> the file prints just fine. But, if I do something like print "$1 after
> \n", the whole output is messed up. If I print "before $1\n", nothing
> prints at all. If I print "before $1 after\n", only after prints.


not really sure, but could be a rogue "\r" in $1,
try dumping out the content in hex: print unpack('H*', $1);

--
Klaus

 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      03-29-2007
On Mar 28, 7:33 pm, "Klaus" <klau...@gmail.com> wrote:
> On Mar 29, 1:36 am, "Chris" <foureightye...@yahoo.com> wrote:
>
> [snip]
>
> > if I print "$1\n",
> > the file prints just fine. But, if I do something like print "$1 after
> > \n", the whole output is messed up. If I print "before $1\n", nothing
> > prints at all. If I print "before $1 after\n", only after prints.

>
> not really sure, but could be a rogue "\r" in $1,
> try dumping out the content in hex: print unpack('H*', $1);
>
> --
> Klaus


Hi, Klaus:
Thanks for suggestion and for making me aware of unpack! There
is a rogue carriage return (0xd) in the string that isn't appearing in
the other strings, even though the file characteristics are the same
as far as the naked eye can see between all my files, this is the only
one with that strange character at the end. chomp() doesn't seem to
get rid of it either. Is there something I can do to deal with this
situation? I've tried retyping the string by hand in the file, but it
doesn't seem to be going away for some reason. In fact, I can delete
the line and retype it in any other file and it works. For some
reason, this file isn't happy...

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-29-2007
On 2007-03-29 13:15, Chris <> wrote:
> Thanks for suggestion and for making me aware of unpack! There
> is a rogue carriage return (0xd) in the string that isn't appearing in
> the other strings, even though the file characteristics are the same
> as far as the naked eye can see between all my files, this is the only
> one with that strange character at the end. chomp() doesn't seem to
> get rid of it either.


Chomp only removes $/, which is usually "\n", not "\r".

> Is there something I can do to deal with this situation?


You probably want to ignore any whitespace at the end of the line, so
you could change your pattern from:

$searchTag = "^#include\\s*(.*)";

to

$searchTag = "^#include\\s*(.*?)\\s*$";

This would not only get rid of the \r, but also of any spaces or tabs at
the end of the line (which normally aren't visible to the naked eye
either).


> I've tried retyping the string by hand in the file, but it
> doesn't seem to be going away for some reason.


Your editor is probably detecting the MS-DOS line endings and acting
accordingly. It should have a way to change this. For example, in vim,
you can switch to unix-style line endings with

:set fileformat=unix

and then just save the file. There are also utilities like dos2unix
which do this, or you can write a simple one-line perl script ...

hp


--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-30-2007
Chris <> wrote:
> On Mar 28, 7:33 pm, "Klaus" <klau...@gmail.com> wrote:
>> On Mar 29, 1:36 am, "Chris" <foureightye...@yahoo.com> wrote:
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.

>>
>> not really sure, but could be a rogue "\r" in $1,



> There
> is a rogue carriage return (0xd) in the string


> Is there something I can do to deal with this
> situation?



Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
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
A potentially dangerous querystring ... [ValidateRequest] Boris ASP .Net 5 04-17-2004 05:22 PM
A potentially dangerous Request.Form value was detected from the client amit ASP .Net 1 02-26-2004 09:47 PM
Why Getting 'A Potentially Dangerous Request...' Error? Anil Kripalani ASP .Net 2 02-25-2004 06:39 PM
A potentially dangerous Request.Form Alex Munk ASP .Net 2 12-17-2003 09:11 AM
Potentially Massive Internet Attack Starts Today =?iso-8859-1?Q?Frisbee=AE_MCNGP?= MCSE 14 08-26-2003 02:12 AM



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