[26577] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8712 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Nov 27 14:05:16 2005

Date: Sun, 27 Nov 2005 11:05:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 27 Nov 2005     Volume: 10 Number: 8712

Today's topics:
    Re: [UDP] Overide "source" addr/port ? <stan@blznospam.hmrprintnospam.com>
    Re: Help:  String search in Windows 2000 doesn't find t <millmanbarry@hotmail.com>
    Re: Help:  String search in Windows 2000 doesn't find t <see.sig@rochester.rr.com>
        Is passing by reference preferrable? <markus.dehmann@gmail.com>
    Re: Is passing by reference preferrable? <see.sig@rochester.rr.com>
    Re: Is passing by reference preferrable? xhoster@gmail.com
    Re: Is passing by reference preferrable? <tadmc@augustmail.com>
    Re: Is passing by reference preferrable? <markus.dehmann@gmail.com>
    Re: Is passing by reference preferrable? xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 27 Nov 2005 08:54:28 -0800
From: "Stan R." <stan@blznospam.hmrprintnospam.com>
Subject: Re: [UDP] Overide "source" addr/port ?
Message-Id: <1133110636_35013@spool6-east.superfeed.net>

Big and Blue wrote:
> Stan R. wrote:
>>
>> The problem is, even though my relay works, the source address of the
>> packet's it sends is 192.168.1.2, BUT the logging app is expecting
>> one of the IP's from the remote UDP feeds.
>>
>> Right now I'm using IO::Socket::INET, and obviously I can't set
>> LocalAddr param to an IP not on my system. So I need some way to
>> rewrite or overide the source address of a udp packet before it's
>> sent.
>
>    That needs can only be done at the network level (ie: in the OS).
>
>> I know this is possible because it's how any good NAT works
>
>    And that is done in the OS kernel.
>
>> Is this possible? (I'm assuming it will require something much "lower
>> level" than IO::Socket::INET.)
>
>    It will require somethng in the kernel.  If you're running Linux
> you *might* be able to arrange it using IPtables (although what you
> would do to distinguish which packets have to have their source
> address changed may be fun to work out), but that is far removed fom
> Perl.

Thank you. But are you sure it's not possible to write a raw packet 
yourself? I know I've seen NAT programs written in C before, that do it, 
unfortunately I don't know where to get it or what it was called.

If C could do it, why not Perl? 



 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


------------------------------

Date: Sun, 27 Nov 2005 12:58:41 -0500
From: Barry Millman <millmanbarry@hotmail.com>
To:  tadmc@augustmail.com
Subject: Re: Help:  String search in Windows 2000 doesn't find text in Windows XP: MS Word document
Message-Id: <4389F3D1.80904@hotmail.com>

OK.  Sorry about the bad code.  However, let's reduce this to the 
minimum, removing the search for the text.  All we will do is read 
chunks of data, with this program:

-------------------- start of program --------------------------
open (TEST, "c:\\PERL\\Barry\\Starthere.rtf") || die "File Open Failed: $!";

while (<TEST>)
  {

     print( "Chunk length: ", length($_),"\n");
     $chunks += 1; 	
  }

close (TEST) || die "File Close Failed $!";

     print( $chunks, " Chunks\n");
-------------------- end of program --------------------------

Now, if I run this using Starthere.rtf, I get 1544 Chunks and they have 
all sorts of different lengths.  Some of the first chunks are of length:
103, 218, 250,1,230,63, 255.

However, if I run this using Starthere.doc, I get only ONE chunk, and it 
is of length 6 bytes.

If I examine the MS Word file using a Hex editor, I get the following 
values for bytes 5 through 7 (calling the first byte as zero):
B1   1A   E1

The 1A is the seventh byte of the file.

The PERL program (above) seems to stop at this character.

So forgetting about the search, does this yield any clues?

Thank you,

Barry




Tad McClellan wrote:
> Barry Millman <millmanbarry@hotmail.com> wrote:
> 
> 
>>The format for the HYPERLINK that I am 
>>searching for in the document is:
>>
>>HYPERLINK "mydoc.doc"
> 
> 
>>PROBLEM: 	The program works on the Windows 2000 machine, but does not 
>>find the files on the Win Xp machine.
> 
> 
> 
> I don't think I can help with that part, but the code is too hokey
> to just let it pass...
> 
> 
> 
>>-----------  start actual code segment --------------------
>>	while (/HYPERLINK(\s+.{1,80}?\.doc)/gim)  #  the "g" causes multiple 
>>matches
> 
> 
> 
> The //m does not do anything, so why is it there?
> 
> It changes the meaning of ^ and $, but you don't use those
> anchors in your pattern, so you don't need //m.
> 
>    .{1,80}?
> 
> is the same as
> 
>    .{0,80}
> 
> Do you really want to match ' .doc' ?
> 
> 
> We can't help you analyse why the match is failing because we
> need two things to do that: the pattern and the string that
> the pattern is to be matched against.
> 
> We have only one of those two things...
> 
> 
> 
>>	{
>>		  $fndxx = $1;
>>
>>		  $fndxx =~ s/\"//;   # remove leading quote
>>		  $fndxx =~ s/\s+//;   # remove leading spaces
> 
> 
> 
> Why capture them only to strip them out of the captured string?
> 
> Why not just leave them out of the capture in the first place?
> 
> 
>    while (/HYPERLINK\s+"(.{1,78}\.doc")/gi)
> 
> or, probably better:
> 
>    while (/HYPERLINK\s+"([^"]{1,78}\.doc")/gi)
> 
> 
> 
>>		  $dir="C:\\IGINproducts\\UserDocuments\\"; 	
>>		
> 
> 
> 
> Use single quotes unless you want to make use of one of the two
> extra things that double quotes give you (interpolation
> and backslash escapes).
> 
> Use forward slashes instead of silly slashes unless the path
> is going to be fed to the "command interpreter".
> 
> 
>    $dir='C:/IGINproducts/UserDocuments/';
> 
> 
> 
>>		  print(OUTFILE  $fndxx,",",$date_string,", in: ",basename($file), 
>>"\n")  ;
> 
> 
> 
> Gak!
> 
> Use double quoted strings to concatenate your output string:
> 
>     print(OUTFILE  "$fndxx,$date_string, in: ", basename($file), "\n")  ;
> 
> 
> 
>>If I try this with a test program (the string to test is in the program 
>>itself ) it works fine on the XP machine.
> 
> 
> 
> If you had shown us your complete test program, then we could
> have helped you debug it.
> 
> But you didn't, so we can't.  (hint)
> 
> 
> 
>>I would really appreciate any comments or suggestions about what I am 
>>doing wrong.
> 
> 
> 
> Not posting a short and complete program that we can run that
> illustrates your problem.
> 
> Have you seen the Posting Guidelines that are posted here frequently?
> 
> 


------------------------------

Date: Sun, 27 Nov 2005 18:22:05 GMT
From: Bob Walton <see.sig@rochester.rr.com>
Subject: Re: Help:  String search in Windows 2000 doesn't find text in Windows XP: MS Word document
Message-Id: <hPmif.77342$JQ.72777@twister.nyroc.rr.com>

Barry Millman wrote:

> Hi:
> 
>     I am using Perl 5 (I believe both machines are using ActivePERL 5) 
> on two machines with the same data files.  One machine is Win 2000 the 
> other is Win XP.  The files are MS Word 2000 documents e-mailed 
> (manually) from the Win 2000 machine to the XP machine.
> 
>     The program searches the MS Word Files (both created with MS Word 
> 2000) for the word HYPERLINK.  The format for the HYPERLINK that I am 
> searching for in the document is:
> 
> HYPERLINK "mydoc.doc"
 >
 > (I checked this on the XP machine in Notepad and it is OK.)
 >

Note that MS Word documents are stored in a proprietary binary 
gibberish format.  To assume that a given word in a document will 
actually always be stored in an ASCII string in the .doc file is 
assuming too much.  For example, perhaps it is stored in Unicode? 
  And maybe newer Notepad versions understand enough to present 
Unicode strings?  Try looking at your files with an editor that 
you *know* won't munge the contents.  I suggest VIM.

It is a mystery why a document would get changed while emailing 
it from one system to another.  Or did you perhaps open the 
document with Word after emailing it, and then save it?  You 
don't say.  Is it the same version of Word?  And what email 
system are you using on each of the computers?  Does the same 
thing happen if you zip the file, email the zipped version, and 
unzip it on the other system?

> PROBLEM:     The program works on the Windows 2000 machine, but does not 
> find the files on the Win Xp machine.
> 
>     The code that is not finding the text on the Win XP machine (same as 
> the Win 2000 machine which does find the test)is:
> 
> -----------  start actual code segment --------------------
>     while (/HYPERLINK(\s+.{1,80}?\.doc)/gim)  #  the "g" causes multiple 
> matches

As others have mentioned, the /m modifier does nothing, and the 
 .{1,80}? would be better as .{0,80} .

> 
>     {
>           $fndxx = $1;
> 
>           $fndxx =~ s/\"//;   # remove leading quote

Your comment doesn't match the regex -- it will remove the first 
quote, not a leading quote.

>           $fndxx =~ s/\s+//;   # remove leading spaces

Again, this will remove the first run of whitespace from the 
string, not leading whitespace.

>           $dir="C:\\IGINproducts\\UserDocuments\\";    
>        
>           $fullname = ($dir . $fndxx);
>           $date_string = "Cannot Find";
>           if (-e $fullname) { $date_string = ctime(stat($dir . 
> $fndxx)->mtime); }     #last update date of that file
>           print(OUTFILE  $fndxx,",",$date_string,", in: 
> ",basename($file), "\n")  ;
>           $matches += 1;  # count matches
>        
>     }    #end while HYPERLINK
> -----------  end actual code segment --------------------
> 
> The output for a found HYPERLINK should look like this (it does on the 
> Win 2000 machine):
> 
> mydoc.doc,(date of last update), in: otherdoc.doc
> 
> On Win XP, the program cannot even find the word HYPERLINK (if I modify 
> the code to just search for that).  The directories are valid, I can 
> have the program print a list of all files as it processes them.
> 
> If I try this with a test program (the string to test is in the program 
> itself ) it works fine on the XP machine.
> 
> There are no encryption issues, nor any file or directory problems.

How exactly do you know this?  Using a piece of garbage like 
Notepad won't definitively tell you this.  I would trust Perl 
much further than Notepad.
 ...
> Barry Millman
-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


------------------------------

Date: Sun, 27 Nov 2005 12:16:04 -0500
From: Markus Dehmann <markus.dehmann@gmail.com>
Subject: Is passing by reference preferrable?
Message-Id: <3uu7ulF13a44kU1@individual.net>

Is it preferrable to pass variables by reference, or does the perl 
interpreter optimize things so that it's not necessary?

In other words, is the 2nd variant of the following two much more efficient?

$a = "bla";
$a = modify($a);
print $a;
sub modify { my $b = shift; $b =~ s/l/L/; return $b; }

$a = "bla";
modify(\$a);
print $a;
sub modify { my $bRef = shift; $$bRef =~ s/l/L/; }


Thanks!
Markus


------------------------------

Date: Sun, 27 Nov 2005 17:35:35 GMT
From: Bob Walton <see.sig@rochester.rr.com>
Subject: Re: Is passing by reference preferrable?
Message-Id: <H7mif.77336$JQ.57409@twister.nyroc.rr.com>

Markus Dehmann wrote:

> Is it preferrable to pass variables by reference, or does the perl 
> interpreter optimize things so that it's not necessary?

Preferable?  Preferable for what?  Clarity?  Speed?  Obfuscation?

If you want to know about speed,

    use Benchmark;

and find out.  It might be different on your platform and Perl 
version than on others.

Both schemes have their uses, advantages and disadvantages. 
Arrays and hashes are generally best passed by reference if there 
is more than one argument to the sub, or if the array or hash is 
large.  You can read up on it in:

    perldoc perlsub
 ...
> Markus
-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


------------------------------

Date: 27 Nov 2005 17:52:02 GMT
From: xhoster@gmail.com
Subject: Re: Is passing by reference preferrable?
Message-Id: <20051127125202.069$H6@newsreader.com>

Markus Dehmann <markus.dehmann@gmail.com> wrote:
> Is it preferrable to pass variables by reference, or does the perl
> interpreter optimize things so that it's not necessary?

Whether it is preferrable rarely has anything to do with how the
interpreter optimizes things.  Preferred code is usually determined more by
things like correctness, intuitiveness, maintainibility, etc.

Micro-optimiztion of your code is extremely context and detail dependent,
so it is hard to make general statements.

>
> In other words, is the 2nd variant of the following two much more
> efficient?
>
> $a = "bla";
> $a = modify($a);
> print $a;
> sub modify { my $b = shift; $b =~ s/l/L/; return $b; }
>
> $a = "bla";
> modify(\$a);
> print $a;
> sub modify { my $bRef = shift; $$bRef =~ s/l/L/; }

time perl -le 'foreach(1..1e7) {$a = "bla";$a = modify($a)}; \
         sub modify { my $b = shift; $b =~ s/l/L/; return $b; }'
22.640u 0.000s 0:23.24 97.4%    0+0k 0+0io 351pf+0w

time perl -le 'foreach(1..1e7) {$a = "bla";modify(\$a)}; \
        sub modify { my $bRef = shift; $$bRef =~ s/l/L/; }'
16.430u 0.000s 0:17.26 95.1%    0+0k 0+0io 352pf+0w

I'd say it is slightly more efficient, not much more efficient.  (Of
course, if $a was much larger, this difference could become greater.  But
then again, if $a was much larger, just creating $a in the first place
would probably swamp your call involving $a.)

BTW, did you know that perl automatically passes scalars by alias (which
is kind of, but not exactly, like passing by reference) and that if you
wish to avoid copying the way to do it is by not copying elements of @_ in
the first place?  (both you're "my $b = shift" and "my $bRef = shift" are
examples of such copying.)

time perl -le 'foreach(1..1e7) {$a = "bla";modify($a)}; \
               sub modify { $_[0] =~ s/l/L/; }'
11.520u 0.010s 0:11.60 99.3%    0+0k 0+0io 349pf+0w

But again, micro-optimization of obviously toy code is rarely of much
value.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Sun, 27 Nov 2005 12:02:24 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Is passing by reference preferrable?
Message-Id: <slrndojt5g.nrb.tadmc@magna.augustmail.com>

Markus Dehmann <markus.dehmann@gmail.com> wrote:

> In other words, is the 2nd variant of the following two much more efficient?


   perldoc Benchmark


> $a = "bla";
> $a = modify($a);
> print $a;
> sub modify { my $b = shift; $b =~ s/l/L/; return $b; }
> 
> $a = "bla";
> modify(\$a);
> print $a;
> sub modify { my $bRef = shift; $$bRef =~ s/l/L/; }


There is also a 3rd alternative to consider:

$a = "bla";
modify($a);
print $a;
sub modify { $_[0] =~ s/l/L/; }


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Sun, 27 Nov 2005 13:18:48 -0500
From: Markus Dehmann <markus.dehmann@gmail.com>
Subject: Re: Is passing by reference preferrable?
Message-Id: <3uubk9F12r8bpU1@individual.net>

xhoster@gmail.com wrote:
> Markus Dehmann <markus.dehmann@gmail.com> wrote:
> 
>>Is it preferrable to pass variables by reference, or does the perl
>>interpreter optimize things so that it's not necessary?
> 
> 
> Whether it is preferrable rarely has anything to do with how the
> interpreter optimizes things.  Preferred code is usually determined more by
> things like correctness, intuitiveness, maintainibility, etc.
> 
> Micro-optimiztion of your code is extremely context and detail dependent,
> so it is hard to make general statements.
> 
> 
>>In other words, is the 2nd variant of the following two much more
>>efficient?
>>
>>$a = "bla";
>>$a = modify($a);
>>print $a;
>>sub modify { my $b = shift; $b =~ s/l/L/; return $b; }
>>
>>$a = "bla";
>>modify(\$a);
>>print $a;
>>sub modify { my $bRef = shift; $$bRef =~ s/l/L/; }
> 
> 
> time perl -le 'foreach(1..1e7) {$a = "bla";$a = modify($a)}; \
>          sub modify { my $b = shift; $b =~ s/l/L/; return $b; }'
> 22.640u 0.000s 0:23.24 97.4%    0+0k 0+0io 351pf+0w
> 
> time perl -le 'foreach(1..1e7) {$a = "bla";modify(\$a)}; \
>         sub modify { my $bRef = shift; $$bRef =~ s/l/L/; }'
> 16.430u 0.000s 0:17.26 95.1%    0+0k 0+0io 352pf+0w
> 
> I'd say it is slightly more efficient, not much more efficient.  (Of
> course, if $a was much larger, this difference could become greater.  But
> then again, if $a was much larger, just creating $a in the first place
> would probably swamp your call involving $a.)
> 
> BTW, did you know that perl automatically passes scalars by alias (which
> is kind of, but not exactly, like passing by reference) and that if you
> wish to avoid copying the way to do it is by not copying elements of @_ in
> the first place?  (both you're "my $b = shift" and "my $bRef = shift" are
> examples of such copying.)
> 
> time perl -le 'foreach(1..1e7) {$a = "bla";modify($a)}; \
>                sub modify { $_[0] =~ s/l/L/; }'
> 11.520u 0.010s 0:11.60 99.3%    0+0k 0+0io 349pf+0w

It's interesting that if you don't copy and just use $_[0], as you 
suggest, then passing by reference is not an advantage anymore.  It even 
seems passing by reference is slower than passing by value now:

time perl -le 'foreach(1..1e7) {$a = "bla";modify($a)}; \
  sub modify {$_[0] =~ s/l/L/; }'
10.494u 0.018s 0:10.54 99.6%    0+0k 0+0io 27pf+0w
time perl -le 'foreach(1..1e7) {$a = "bla";modify(\$a)}; \
  sub modify { ${$_[0]} =~ s/l/L/; }'
12.976u 0.014s 0:13.05 99.4%    0+0k 0+0io 0pf+0w

But then, time is rather unreliable of course, and the Benchmark module 
should be used here...

Markus


------------------------------

Date: 27 Nov 2005 18:30:51 GMT
From: xhoster@gmail.com
Subject: Re: Is passing by reference preferrable?
Message-Id: <20051127133051.260$Hn@newsreader.com>

Markus Dehmann <markus.dehmann@gmail.com> wrote:
> xhoster@gmail.com wrote:
>
> time perl -le 'foreach(1..1e7) {$a = "bla";modify($a)}; \
>   sub modify {$_[0] =~ s/l/L/; }'
> 10.494u 0.018s 0:10.54 99.6%    0+0k 0+0io 27pf+0w
> time perl -le 'foreach(1..1e7) {$a = "bla";modify(\$a)}; \
>   sub modify { ${$_[0]} =~ s/l/L/; }'
> 12.976u 0.014s 0:13.05 99.4%    0+0k 0+0io 0pf+0w
>
> But then, time is rather unreliable of course, and the Benchmark module
> should be used here...


How so?  I find "time" to be at least as reliable as Benchmark.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8712
***************************************


home help back first fref pref prev next nref lref last post