[29271] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 515 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 14 18:09:56 2007

Date: Thu, 14 Jun 2007 15:09:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 14 Jun 2007     Volume: 11 Number: 515

Today's topics:
    Re: Any cross-platform function to obtain the machine n <joe@inwap.com>
        CGI::Session How can a session be not expired and at th  djbonnici@gmail.com
        encrypt with perl, decrypt with ruby, 3 days and counti <aktxyz@gmail.com>
    Re: Interesting PERL anamoly - confirmation and/or expl <dummy@example.com>
    Re: Interesting PERL anamoly - confirmation and/or expl <rvtol+news@isolution.nl>
        Need help reading two-dimentional array from a file <danchik@gmail.com>
    Re: Need help reading two-dimentional array from a file <mritty@gmail.com>
    Re: Need help reading two-dimentional array from a file <danchik@gmail.com>
    Re: perl and php  krakle@visto.com
    Re: Perl Bug???  krakle@visto.com
    Re: Perl Bug??? <Mike.Thorland@sas.com>
    Re: Perl Bug??? <mritty@gmail.com>
    Re: Perl Bug??? <mritty@gmail.com>
    Re: Perl Bug??? <Mike.Thorland@sas.com>
        replacing a special character <laredotornado@zipmail.com>
    Re: replacing a special character <mritty@gmail.com>
    Re: Runtime disparity - Same program in Perl and Ruby <dummy@example.com>
    Re: Runtime disparity - Same program in Perl and Ruby <kaldrenon@gmail.com>
    Re: urgent:reading 2 files into 2 different $ variables <paduille.4061.mumia.w+nospam@earthlink.net>
    Re: Writing row at a time in Excel using OLE <ashishrai@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 14 Jun 2007 12:05:15 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: Any cross-platform function to obtain the machine name?
Message-Id: <Y8ydnRVpiqD2DOzbnZ2dnUVZ_hGdnZ2d@comcast.com>

Ilias Lazaridis wrote:

> Is the term "host" generally prefered within the perl domain?
> Any rationales to change this to "Host"?

Not Perl; historical reasons.  The file HOSTS.TXT held the names of all the
machines connected to the ARPANET.  Before DNS was invented,
people were expected to use FTP to grab a copy of the master
HOSTS.TXT file.

http://en.wikipedia.org/wiki/History_of_the_internet#NIC.2C_InterNIC.2C_IANA_and_ICANN
http://en.wikipedia.org/wiki/Hosts.txt

	-Joe


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

Date: Thu, 14 Jun 2007 12:56:37 -0700
From:  djbonnici@gmail.com
Subject: CGI::Session How can a session be not expired and at the same time empty?
Message-Id: <1181850997.867747.10470@n15g2000prd.googlegroups.com>

I am here for sure missing something!!!! How can a session be empty
and the same time be not expired? Can you pls give an example.

This is from the doku
"Remember, all expired sessions are empty sessions, but not all empty
sessions are expired sessions."

at the following link
http://search.cpan.org/~markstos/CGI-Session-4.20/lib/CGI/Session/Tutorial.pm

Your help will be greatly appreciated.

David



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

Date: Thu, 14 Jun 2007 20:53:02 -0000
From:  uncle <aktxyz@gmail.com>
Subject: encrypt with perl, decrypt with ruby, 3 days and counting...
Message-Id: <1181854382.457369.262480@i13g2000prf.googlegroups.com>

I have tried hundreds of combos, scanned hundreds of web pages, I
still cannot encrypt with perl and decrypt with ruby.  Hell, I can't
even encrypt with perl/ruby and get the same result.

Have tried blowfish, tripledes, des, rc4, you name.

Would love an expert to help out !

Here is one of my many attempts


#===== PERL

#!/usr/bin/perl
use MIME::Base64;
use Crypt::CBC;
my $key = '12345678';
my $iv = '12345678';
my $text = '12345678';
$cipher = Crypt::CBC->new({
  'literal_key' => 0,
  'key' => $key,
  'iv' => $iv,
  'header' => 'none',
  'padding' => 'standard',
  'prepend_iv' => 0});
$encrypted = $cipher->encrypt($text);
$encoded = encode_base64($encrypted);
print "encrypted=$encrypted\n";
print "encoded=$encoded\n";

#===== RUBY

require 'openssl'
require "base64"
require 'cgi'
require 'uri'
key = "23456789"
token = "12345678"
e = OpenSSL::Cipher::Cipher.new 'DES'
e.encrypt key
s = e.update token
s << e.final
puts s
puts Base64.encode64(s)

~



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

Date: Thu, 14 Jun 2007 18:36:53 GMT
From: "John W. Krahn" <dummy@example.com>
Subject: Re: Interesting PERL anamoly - confirmation and/or explanations welcomed
Message-Id: <9Vfci.20695$kY6.442@edtnps82>

xyz88888@aol.com wrote:
> I encountered a problem with a PERL script I wrote, and I would like
> to find out if this is just a problem with the ActiveState PERL
> interpreter, or a problem in the PERL spec itself. I am using the
> ActivePerl interpreter, build 820 version 5.8.8. I looked for other
> free downloadable PERL interpreters to compare results, but had no
> luck. So I'm hoping to get some feedback from any of you interested
> folks.
> 
> The input: a multimedia production storyboard in ASCII-text format.
> Sections of the file contain dialogue for voice-overs, and I want to
> parse out just this stuff and ignore the rest. In this case, I need
> all lines starting with the string '(rules script) "' copied to the
> output file of my script.
> 
> The process: my PERL script reads in the file line-by-line, does
> string matching against the first part of each line and based on some
> logic, determines if and how that line should be copied to the output
> file.
> 
> The problem: in some instances, the string to be used in the matching
> function needs to be stripped out before copied to the output file. I
> do this using "positional parameters" ($1, $2,...) from the match
> command. Whenever the string contains non-alphanumeric characters
> (with the "\" escape), those characters seem to cause the substitution
> command to fail. After numerous codings, I concluded that based on the
> various outputs.
> 
> The code: For brevity I am only including the combinations of match
> conditionals and the substitution commands I tried that are related to
> this specific problem. I'll copy the whole script down below for those
> who want to see the whole thing, although everything else works fine.
> The match function is written to match the string '(rules script) "'
> at the beginning of a line.
> 
> Variation 1:
> 
>    } elsif (/(^\(rules script\) ")/) {
>       s/$1//;
>       print OUTFILE;
>    }

Put the substitution in the conditionl:

    } elsif ( s/(^\(rules script\) ")// ) {
       print OUTFILE;
    }

[ SNIP ]

> P.S. As promised, here is the whole script.

use warnings;
use strict;

> $x = 0;
> $cont = "f";
> $csm = "f";
> 
> open (INFILE, "lcm01_v1d-TO.txt");
> open (OUTFILE, ">outfile.txt");

You should *ALWAYS* verify that the files opened correctly!

open INFILE,  '<', 'lcm01_v1d-TO.txt' or die "Cannot open 'lcm01_v1d-TO.txt' $!";
open OUTFILE, '>', 'outfile.txt' or die "Cannot open 'outfile.txt' $!";


> while (<INFILE>) {
> ++$x;
>    chomp;
> 
>    if (/^(lcm01_\d{3}\w*)\s?/) {
>       $csm = "f";
>       print OUTFILE "\n$_\nNARRATOR: ";
> 
>    } elsif (s/^(\(rules script\)\s\")//) {
> 
> print STDERR "\n1=$1\n";
>       s/\($1\)\s\"//;
> print STDERR "\n$x:\n$_\n";

You don't need the $x variable to get the current line number:

print STDERR "\n$.:\n$_\n";


>       s/"\s*$/ /;
>       print OUTFILE;
>       print OUTFILE "When you have completed this exercise, click the
> Next button to continue.\n";
> 
>    } elsif ($cont eq "t") {
>       chomp; chomp; chomp;

What do you think that using chomp three times will accomplish (that using it 
only once will not?)


>       if ((/^Notes$/)
>       || (/^Correct Answer:$/)
>       ||  (//))

What did you think that (//) was going to do?

perldoc perlop
[ SNIP ]

         If the PATTERN evaluates to the empty string, the last successfully
         matched regular expression is used instead. In this case, only the "g"
         and "c" flags on the empty pattern is honoured - the other flags are
         taken from the original pattern. If no match has previously succeeded,
         this will (silently) act instead as a genuine empty pattern (which
         will always match).


>       {
>          $cont = "f";
> 
>       } elsif (/"\s*$/) {
>          $cont = "f";
>          s/"\s*$/\n/;
>          print OUTFILE;

You should just put the substitution in the conditionl:

       } elsif ( s/"\s*$/\n/ ) {
          $cont = "f";
          print OUTFILE;


>       } else {
>          print OUTFILE;
>       }
> 
>    } elsif ((! /TRAINER:/)
>      && (/([A-Z]+):\s+"?(.+)/))
>      {
>       $cont = "t";
> 
>       if ($1 eq "CSM") {
>          $csm = "t";
>          $line = "\n$1: $2";
> 
>       } elsif (($line = $2) =~ m/^Click/) {
>          $cont = "f";
> 
>          if ($csm eq "f") {
>             $line = "\n$line";
>          } else {
>             $line = "\nNARRATOR: $line\n";
>          }
> 
>       } elsif ($1 eq "NARRATION") {
>          $line = "\n$1: $2";
> 
>       } elsif ($csm eq "t") {
>          $line = "\n$1: $2";
> 
>       } elsif ($csm eq "f") {
>          $line = "$2";
>       }
> 
>       if ($line =~ m/("\s*)$/) {
>          $line =~ s/$1/\n/;
>          $cont = "f";
>       }
> 
>       print OUTFILE $line;
>    }
> }
> 
> close INFILE;
> close OUTFILE;


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


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

Date: Thu, 14 Jun 2007 20:33:02 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Interesting PERL anamoly - confirmation and/or explanations welcomed
Message-Id: <f4s8n0.1es.1@news.isolution.nl>

xyz88888@aol.com schreef:
> I encountered a problem with a PERL script I wrote, and I would like
> to find out if this is just a problem with the ActiveState PERL
> interpreter, or a problem in the PERL spec itself. 

The third and most likely error is that you made bad assumptions. 


> $x = 0;
> $cont = "f";
> $csm = "f";

Missing:
  use strict;
  use warnings;
and the proper declaration of the variables.


> [...]
> while (<INFILE>) {
> ++$x;

That $x is emulating $., so remove it. 

> [...]
>    } elsif (s/^(\(rules script\)\s\")//) {
> 
> print STDERR "\n1=$1\n";
>       s/\($1\)\s\"//;
> print STDERR "\n$x:\n$_\n";

Yuck. 

   m/^\(rules script\)\s+"(.*)"$/ and print $1, "\n";

(and if you wouldn't have chomped, the "\n" at the end can go) 

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: Thu, 14 Jun 2007 18:39:24 -0000
From:  DanchikPU <danchik@gmail.com>
Subject: Need help reading two-dimentional array from a file
Message-Id: <1181846364.366745.79460@i38g2000prf.googlegroups.com>

Hi,

I am attempting to read in two-dimentional array from a file, but my
data is being treated as a flat array.

The data file looks something like this:

(['abcd','qwerty','L','1234'],
['abce','qwerty','L','1234'],
['abcf','qwerty','L','1234'],
['abcg','qwerty','L','1234']);

In the code, I am doing to standart

         open(DATA, "foo.txt")
         @foo=<DATA>
         close(DATA);

Accessor $foo[0] will return the first line, but $foo[0][0] fails

The data is formatted perfectly, I have tried pasting it directly into
the code and everything works, it's only when I try to read it in that
I fail.

Thanks in advance for your help!

~d



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

Date: Thu, 14 Jun 2007 12:00:53 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Need help reading two-dimentional array from a file
Message-Id: <1181847653.662311.189120@q19g2000prn.googlegroups.com>

On Jun 14, 2:39 pm, DanchikPU <danc...@gmail.com> wrote:
> I am attempting to read in two-dimentional array from a file,
> but my data is being treated as a flat array.
>
> The data file looks something like this:
>
> (['abcd','qwerty','L','1234'],
> ['abce','qwerty','L','1234'],
> ['abcf','qwerty','L','1234'],
> ['abcg','qwerty','L','1234']);
>
> In the code, I am doing to standart
>
>          open(DATA, "foo.txt")
>          @foo=<DATA>
>          close(DATA);
>
> Accessor $foo[0] will return the first line, but $foo[0][0]
> fails
>
> The data is formatted perfectly, I have tried pasting it
> directly into the code and everything works, it's only when I
> try to read it in that I fail.

'[' and ']' and ',' mean absolutely nothing special in text.  They
only mean anything in code.  Perl has no way of knowing that you
wanted the text you read from the file to act as though it was
actually typed into your program.

There are many solutions I can think of off the top of my head:
1) Parse the file looking for your data files and build your 2-
dimensional array.
2) Use eval() to treat the text as Perl code
3) Change how you're creating the file to begin with.

Here's a few examples of each solution:
1)
#!/usr/bin/perl
use strict;
use warnings;
my @foo;
while (<DATA>){
  my @values = /'(.*?)'/g;
  push @foo, \@values;
}

2)
#!/usr/bin/perl
use strict;
use warnings;
my @foo = eval do { local $/; <DATA>} ;

3)
Program that creates the file:
#!/usr/bin/perl
use strict;
use warnings;
use Storable;
my @foo;
#populate @foo
store(\@foo, 'foo.txt');
__END__

Program that reads the file:
#!/usr/bin/perl
use strict;
use warnings;
use Storable;
my @foo = @{ retrieve('foo.txt') };


Please take EXTREME CAUTION with method #2.  If you're not 100% sure
you know exactly what the contents of that file are, you're opening
yourself up to a massive security hole.  That method will execute ANY
perl code found in the file.  Including code that might contain, for
example:
system("rm -rf /");

Paul Lalli



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

Date: Thu, 14 Jun 2007 19:23:07 -0000
From:  DanchikPU <danchik@gmail.com>
Subject: Re: Need help reading two-dimentional array from a file
Message-Id: <1181848987.239975.190510@j4g2000prf.googlegroups.com>

On Jun 14, 3:00 pm, Paul Lalli <mri...@gmail.com> wrote:
> On Jun 14, 2:39 pm, DanchikPU <danc...@gmail.com> wrote:
>
>
>
>
>
> > I am attempting to read in two-dimentional array from a file,
> > but my data is being treated as a flat array.
>
> > The data file looks something like this:
>
> > (['abcd','qwerty','L','1234'],
> > ['abce','qwerty','L','1234'],
> > ['abcf','qwerty','L','1234'],
> > ['abcg','qwerty','L','1234']);
>
> > In the code, I am doing to standart
>
> >          open(DATA, "foo.txt")
> >          @foo=<DATA>
> >          close(DATA);
>
> > Accessor $foo[0] will return the first line, but $foo[0][0]
> > fails
>
> > The data is formatted perfectly, I have tried pasting it
> > directly into the code and everything works, it's only when I
> > try to read it in that I fail.
>
> '[' and ']' and ',' mean absolutely nothing special in text.  They
> only mean anything in code.  Perl has no way of knowing that you
> wanted the text you read from the file to act as though it was
> actually typed into your program.
>
> There are many solutions I can think of off the top of my head:
> 1) Parse the file looking for your data files and build your 2-
> dimensional array.
> 2) Use eval() to treat the text as Perl code
> 3) Change how you're creating the file to begin with.
>
> Here's a few examples of each solution:
> 1)
> #!/usr/bin/perl
> use strict;
> use warnings;
> my @foo;
> while (<DATA>){
>   my @values = /'(.*?)'/g;
>   push @foo, \@values;
>
> }
>
> 2)
> #!/usr/bin/perl
> use strict;
> use warnings;
> my @foo = eval do { local $/; <DATA>} ;
>
> 3)
> Program that creates the file:
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Storable;
> my @foo;
> #populate @foo
> store(\@foo, 'foo.txt');
> __END__
>
> Program that reads the file:
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Storable;
> my @foo = @{ retrieve('foo.txt') };
>
> Please take EXTREME CAUTION with method #2.  If you're not 100% sure
> you know exactly what the contents of that file are, you're opening
> yourself up to a massive security hole.  That method will execute ANY
> perl code found in the file.  Including code that might contain, for
> example:
> system("rm -rf /");
>
> Paul Lalli- Hide quoted text -
>
> - Show quoted text -

Paul,
Thank you! Method 1 worked like a charm.

~d



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

Date: Thu, 14 Jun 2007 13:36:12 -0700
From:  krakle@visto.com
Subject: Re: perl and php
Message-Id: <1181853372.153980.34380@j4g2000prf.googlegroups.com>

On Jun 12, 6:19 am, peter <p...@juno.com> wrote:

> At one time, I did a bit of perl but now I see php alot.  I was
> wondering what you guys thinks of the pros/cons of  perl and php.
>

I am a Web Developer. It takes a lot more code in Perl than PHP to
accomplish simple tasks. Some may say (in this group) PHP is geared
towards beginners. They are wrong. PHP was created with the internet
in mind where as Perl wasn't. In my opinion there's only one way to
use Perl on the internet effectively and that is with mod_perl and a
framework like Maypole or Catalyst. But that comes with a whole
'nother set of problems and Pros and Cons.

In short... If you're developing for the internet and your only
choices are PHP and Perl, use what you know best! If you must choose
for other reasons... I would use PHP simply because it makes more
sense to use the right tool for the right job. Afterall, it was made
for that purpose. But really if you're looking for a JOB you may want
to take a look at ASP+C#.



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

Date: Thu, 14 Jun 2007 13:21:43 -0700
From:  krakle@visto.com
Subject: Re: Perl Bug???
Message-Id: <1181852503.121301.323350@j4g2000prf.googlegroups.com>

On Jun 13, 2:11 pm, Paul Lalli <mri...@gmail.com> wrote:
> Whenever you find yourself wanting to type something like that, you
> should really count to 10 and ask yourself what's more likely - that
> there's a new and undiscovered bug in the language that's been used
> worldwide for 20 years that you just happened to find, or that you
> either did something wrong or don't understand the results you're
> seeing.

There's bugs in every Perl release. Your message implies that the
language hasn't changed or evolved since 1987. Which is silly. A
better point is... it's less than likely that someone who isn't Perl
savvy enough to 'use strict' will find a bug with the language.






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

Date: Thu, 14 Jun 2007 16:22:34 -0400
From: "Mike Thorland" <Mike.Thorland@sas.com>
Subject: Re: Perl Bug???
Message-Id: <f4s82b$hil$1@foggy.unx.sas.com>

Another follow up question. Why does manipulation of $_ in "while loops" 
contained in routine Foo() affect the localized implicit $_ in the "for 
loop" for @array_test , but "for loops" in routine Foo() do not?

For example if you use a for loop the assignment doesn't affect the original 
$_ for loop variable:
sub Foo() {
   @array2 = ("one");
   for (@array2) { $_ = "new value"; }
}

@array_test = ("original value");
for (@array_test) {
   Foo();
}
print "array_test: @array_test\n";

Output:
array_test: original value

Yet if you change the for loop to a while loop the assignment does affect $_
sub Foo() {
   @array2 = ("one");
   while (@array2) { $_ = shift @array2; $_ = "new value"; }

@array_test = ("original value");
for (@array_test) {
   Foo();
}
print "array_test: @array_test\n";


Output:
array_test: new value

"Paul Lalli" <mritty@gmail.com> wrote in message 
news:1181761903.532008.212050@i38g2000prf.googlegroups.com...
> On Jun 13, 2:34 pm, "Mike Thorland" <Mike.Thorl...@sas.com> wrote:
>> Subject: Perl Bug???
>
> Whenever you find yourself wanting to type something like that, you
> should really count to 10 and ask yourself what's more likely - that
> there's a new and undiscovered bug in the language that's been used
> worldwide for 20 years that you just happened to find, or that you
> either did something wrong or don't understand the results you're
> seeing.
>
>> This was tested with Perl 5.8.5 and 5.6.1
>>
>> Test Program:
>>
>> sub Foo() {
>>    print "array_test: @array_test\n";
>>
>>    $shellcmd = "/bin/echo hi'";
>>    open SHELLCMD, "$shellcmd |" or
>>       die "Could not open shellcmd $!\n";
>>    while (<SHELLCMD>) { } # do nothing - works with backticks not with 
>> shell
>> loop
>>    close SHELLCMD;
>>
>>    print "array_test: @array_test\n";
>>
>> }
>>
>> @array_test = ("one");
>> for (@array_test) {
>>    Foo();
>>
>> }
>>
>> Output:
>>
>> array_test: one
>> array_test:
>>
>> Why does array_test become unitialized?
>
> It doesn't.  The only element of @array_test, however does.  If you'd
> enabled warnings, you would see Perl tell you that you're printing an
> undefined value, rather than printing no values.  You could also
> examine the contents of @array_test more precisely with the
> Data::Dumper module.
>
> perldoc perlsyn
> describes how for each loop works.  When you don't give a loop
> iterator variable, Perl localizes $_ and uses that.  In your foreach
> loop, $_ is an ALIAS to the element of the list you're iterating over,
> meaning that changes to $_ will change the list itself.
>
> In your subroutine, you are using $_ implicitly once more, this time
> to iterate over the "/bin/echo hi |" shell command's output. In each
> iteration of your while loop, $_ gets assigned to the current line of
> output.  When the output is exhausted, the readline operator (ie,
> <SHELLCMD>) returns undef, and so $_ gets the value undef.  But
> remember that you've already aliased $_ to be the current element of
> @array_test.  So since you just changed $_, you've also changed the
> current element of @array_test.
>
>> Yet if I add a loop variable for
>> @array_test ...
>>
>> for $bar (@array_test) {
>>    Foo();
>>
>> }
>>
>> It works:
>>
>> Output:
>>
>> array_test: one
>> array_test: one
>
> Right, because this example doesn't use the localized $_, so the
> function call doesn't get a chance to change your array elements.
>
> Best practice - don't rely on the implicit nature of $_ for either for
> loops nor while loops.  If you must use $_, then localize it
> beforehand.  If you'd put the statement "local $_;" before you began
> your while loop, you'd be working with a temporary value of $_ that
> was not aliased to the foreach's list, and this "bug" would not have
> appeared.
>
> Paul Lalli
>
> P.S.  To help you see what's happening, please take a look at this
> modified program, based off your example:
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
>
> my @array_test;
> sub Foo() {
>   print "array_test: ", Dumper(\@array_test);
>
>   my $shellcmd = "/bin/echo hi'";
>   open SHELLCMD, "$shellcmd |" or
>      die "Could not open shellcmd $!\n";
>   while (<SHELLCMD>) {
>      print "$. - ", Dumper(\@array_test);
>   }
>   close SHELLCMD;
>
>   print "array_test: ", Dumper(\@array_test);
>
> }
>
> @array_test = ("one");
> for (@array_test) {
>   Foo();
> }
>
> 




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

Date: Thu, 14 Jun 2007 13:45:00 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Perl Bug???
Message-Id: <1181853900.813003.242990@e9g2000prf.googlegroups.com>

On Jun 14, 4:21 pm, kra...@visto.com wrote:
> On Jun 13, 2:11 pm, Paul Lalli <mri...@gmail.com> wrote:
>
> > Whenever you find yourself wanting to type something like that, you
> > should really count to 10 and ask yourself what's more likely - that
> > there's a new and undiscovered bug in the language that's been used
> > worldwide for 20 years that you just happened to find, or that you
> > either did something wrong or don't understand the results you're
> > seeing.
>
> There's bugs in every Perl release. Your message implies that the
> language hasn't changed or evolved since 1987.

I implied nothing of the sort.  Read my message again.

> A
> better point is... it's less than likely that someone who isn't
> Perl savvy enough to 'use strict' will find a bug with the
> language.

That is exactly what my message said.  Read my message again.

Paul Lalli




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

Date: Thu, 14 Jun 2007 13:47:24 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Perl Bug???
Message-Id: <1181854044.752590.205600@g37g2000prf.googlegroups.com>

On Jun 14, 4:22 pm, "Mike Thorland" <Mike.Thorl...@sas.com> wrote:
> Another follow up question. Why does manipulation of $_ in
> "while loops" contained in routine Foo() affect the localized
> implicit $_ in the "for loop" for @array_test , but "for loops"
> in routine Foo() do not?

Once again, I refer you to `perldoc perlsyn`:
     Foreach Loops

     The "foreach" loop iterates over a normal list value and
     sets the variable VAR to be each element of the list in
     turn.  If the variable is preceded with the keyword "my",
     then it is lexically scoped, and is therefore visible only
     within the loop.  Otherwise, the variable is implicitly
     local to the loop and regains its former value upon exiting
     the loop.  If the variable was previously declared with
     "my", it uses that variable instead of the global one, but
     it's still localized to the loop.  This implicit
     localisation occurs only in a "foreach" loop.

A foreach loop localizes $_.  A while loop does not.  That is, the for
loop stores off the current value of $_ before the loop begins, and
restores this value once the loop ends.

Paul Lalli



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

Date: Thu, 14 Jun 2007 17:07:01 -0400
From: "Mike Thorland" <Mike.Thorland@sas.com>
Subject: Re: Perl Bug???
Message-Id: <f4salm$iv5$1@foggy.unx.sas.com>

I see so the even though the for loop creates a "localized" $_ variable, it 
is still global and can be manipulated. After the for loop exits the 
original $_ variable is restored

sub Foo() {
   $_ = "new value";
}

@array_test = ("original value");
$_ = "Global value before entering for loop";
print "BEFORE \$_ = $_\n";
for (@array_test) {
   Foo();
}
print "AFTER \$_ = $_\n";
print "array_test: @array_test\n";


Output:
BEFORE $_ = Global value before entering for loop
AFTER $_ = Global value before entering for loop
array_test: new value

Thanks again!!

"Paul Lalli" <mritty@gmail.com> wrote in message 
news:1181854044.752590.205600@g37g2000prf.googlegroups.com...
> On Jun 14, 4:22 pm, "Mike Thorland" <Mike.Thorl...@sas.com> wrote:
>> Another follow up question. Why does manipulation of $_ in
>> "while loops" contained in routine Foo() affect the localized
>> implicit $_ in the "for loop" for @array_test , but "for loops"
>> in routine Foo() do not?
>
> Once again, I refer you to `perldoc perlsyn`:
>     Foreach Loops
>
>     The "foreach" loop iterates over a normal list value and
>     sets the variable VAR to be each element of the list in
>     turn.  If the variable is preceded with the keyword "my",
>     then it is lexically scoped, and is therefore visible only
>     within the loop.  Otherwise, the variable is implicitly
>     local to the loop and regains its former value upon exiting
>     the loop.  If the variable was previously declared with
>     "my", it uses that variable instead of the global one, but
>     it's still localized to the loop.  This implicit
>     localisation occurs only in a "foreach" loop.
>
> A foreach loop localizes $_.  A while loop does not.  That is, the for
> loop stores off the current value of $_ before the loop begins, and
> restores this value once the loop ends.
>
> Paul Lalli
> 




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

Date: Thu, 14 Jun 2007 19:35:46 -0000
From:  "laredotornado@zipmail.com" <laredotornado@zipmail.com>
Subject: replacing a special character
Message-Id: <1181849746.638026.202890@x35g2000prf.googlegroups.com>

Hello,

I'm running Fedora Linux Core 5.  I have some weird characters in my
files that are causing HTML validation to fail.  In vim, they appear
like

Anderson<92>s

where "<92>" is this odd character.  It should really be an
apostraphe.  My question to the group is how do I run a perl search
and replace command along the lines of

perl -pi -e 's/old_expr/new_expr/g' myfile.html

to swap out the bizarre "<92>" character with an apostraphe?

Thanks, - Dave



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

Date: Thu, 14 Jun 2007 13:00:13 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: replacing a special character
Message-Id: <1181851213.621482.289410@i38g2000prf.googlegroups.com>

On Jun 14, 3:35 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.com> wrote:
> I'm running Fedora Linux Core 5.  I have some weird characters in my
> files that are causing HTML validation to fail.  In vim, they appear
> like
>
> Anderson<92>s
>
> where "<92>" is this odd character.  It should really be an
> apostraphe.  My question to the group is how do I run a perl search
> and replace command along the lines of
>
> perl -pi -e 's/old_expr/new_expr/g' myfile.html
>
> to swap out the bizarre "<92>" character with an apostraphe?

Sounds like you have a "smart quote" in there.  Hex 92 is Decimal 146,
which is the ' character in whatever character set I'm using....

I would try:
perl -pi -e"s/\x22/'/g" myfile.html

Paul Lalli



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

Date: Thu, 14 Jun 2007 19:11:18 GMT
From: "John W. Krahn" <dummy@example.com>
Subject: Re: Runtime disparity - Same program in Perl and Ruby
Message-Id: <qpgci.25243$nx3.6968@edtnps89>

Kaldrenon wrote:
> Hi all (this is going to comp.lang.ruby and comp.lang.perl.misc),
> 
> The other day I wrote a basic program in Perl,

Did you write it in basic or in Perl?   :-)

> and the following day I
> rewrote it in Ruby. I'm curious about the differences in runtime of
> the two versions, though.
> 
> Let me start by describing the program (I'll append full code for both
> to the end): it reads in a list of alphanumeric codes from file
> (format is [\w\d\S]+_\d{3},

The character class \d is a subset of \w and they are both a subset of \S so 
your expression could be simplified to:

\S+_\d{3}


> but they're separated by a comma in the
> file), then creates a hash with those codes as keys and empty arrays
> as values. After the hash is built, the program traverses through a
> given directory and its subdirectories (using File::Find in Perl and
> Find.find in Ruby) and checks each file against the hash of codes
> (with a few regexps and conditions to prevent lots of unnecessary
> looping), adding it to the array for a code if the code is found in
> the filename. Finally, it writes the contents of the hash to a .csv
> file in the format CODE,PATH for each match.
> 
> Now, if it were the case that Ruby or Perl were simply -slower- than
> the other, I wouldn't be bothering you folks. But here's where it gets
> a little unusual: the number of elements in the code list has a
> noticeable impact on the run time of the Ruby version, but far less on
> the Perl version. I ran each one a few times with code lists of
> various sizes, and they both print start/stop timestamps at the end,
> so I collected the data:
> 
> Entries | Seconds
> Ruby
> 4	   | 153
> 64	  | 133
> 256	 | 222
> 512	 | 327
> 1024    | 562
> 1500    | 683
> Perl
> 4	 | 291
> 64	| 258
> 256    | 253
> 512    | 248
> 1024  |	353
> 1500  |	363
> 
> Ruby runs faster for low numbers of entries, as you can see, but once
> you get up to 1500, Ruby's time has more than tripled while Perl's
> time has gone up about a fifth.
> 
> I've looked over the code for both versions several times, and I don't
> see any significant differences. The only important feature the Ruby
> version lacks is the sort() before writing the file.
> 
> I'd really appreciate any insight into why Ruby's runtime grows so
> readily and Perl's does not.

Did you compare the output of the Perl and Ruby versions to see if there were 
any differences?


> Code of both versions follows.
> 
> Thanks,
> Andrew Fallows
> 
> use File::Find;
> use strict;
> use warnings;
> my $code;
> my $type;
> my %filecodes = ();
> my $start_time = "Started: " . localtime();
> $| = 1; #Enables flush on print.
> $\ = "\n"; #Automatic newlines on print
> open(ITEM_LIST, "(path)") or die "Error";

You should include the $! (or $^E) variable in the error message so you know 
why it failed.


> # This loop builds a hash whose keys are the codes/types from file
> # and whose values are references to empty arrays
> while(my $item = <ITEM_LIST>)
> {
> 	$item =~ s/,/_/;
> 	$item =~ s/\n//g;

That is usually done with chomp:

         chomp $item;

> 	print $item;
> 	my @files = ();
> 	$filecodes{$item} = \@files;

You don't need to create an array, just assign an anonymous array:

  	$filecodes{$item} = [];


> }
> print "Hash built";
> 
> # Uses File::Find to iterate over the entire subdirectory
> find(\&file_seek, "(path)");
> 
> # The searching portion: gets each location from File::Find, then
> compares it
> # to all the targets. If there is a match, prints a message and adds
> that file
> # to the related array.
> sub file_seek
> {
> 	my $file = $_;
> 	# Kicks out if the file in question is not of the necessary format
> 	if(!(-f $file) || !($file =~ /^[\d\w\S]+_\d{3}/)){ return; }

Using $_ instead of the copy in $file:

         return if !-f || !/^\S+_\d{3}/;


> 	foreach my $target (keys(%filecodes))
> 	{
> 		# If the file name contains the code sought
> 		if($file =~ /$target/)

Because $target may contain some regular expression meta-characters you should 
quotemeta it:

  		if ( $file =~ /\Q$target/ )

Or use the index function:

  		if ( 0 <= index $file, $target )


> 		{
> 			print "found $file in $File::Find::dir";
> 
> 			# Jumps out if the list for this code already contains this file.
> 			for (0..@{$filecodes{$target}})

You have an off-by-one error:

  			for (0..$#{$filecodes{$target}})

> 			{
> 				if(defined(${$filecodes{$target}}[$_])
> 				&& $File::Find::name eq ${$filecodes{$target}}[$_]) {return; }

${$filecodes{$target}}[$_] can be written more simply as $filecodes{$target}[$_].


But you don't really need to use an array index:

  			for ( @{$filecodes{$target}} )
                         {
                             return if defined() && $File::Find::name eq $_;

(Or you could use a Hash of Hashes.)


> 			}
> 			push(@{$filecodes{$target}}, $File::Find::name);
> 		}
> 	}
> }
> 
> # After the whole directory has been searched, prints each key and all
> # values found for it.
> open(RESULTS, "> (path)") or die "Error 2";

You should include the $! (or $^E) variable in the error message so you know 
why it failed.


> foreach my$target ( sort(keys( %filecodes )))
> {
> 	my @results = @{$filecodes{$target}};

Do you really need to make a copy of the array?


> 	if(@results == 0) { push(@results, "NO FILES FOUND") }

If the array is empty you can just assign to it:

         @results = 'NO FILES FOUND' unless @results;


> 	print $target;
> 	foreach (@results)
> 	{
> 		print RESULTS "$target,$_";
> 		print "\t$_";
> 	}
> }
> close RESULTS;
> print  $start_time;
> print "Ended: " . localtime();



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


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

Date: Thu, 14 Jun 2007 20:15:16 -0000
From:  Kaldrenon <kaldrenon@gmail.com>
Subject: Re: Runtime disparity - Same program in Perl and Ruby
Message-Id: <1181852116.227265.75870@n15g2000prd.googlegroups.com>

Thanks for the reply, John. There are a number of good tips in your
reply for making my code more "Perl"-y. I don't think many (if any)
will actually change the way the program runs, though, will they? A
lot of the things I did work, but are styled more like Java, the
language I use most. For example, I know I can just use $_ in sub
file_seek, but I prefer to give my vars names that make more sense at
a glance. But I'll keep all of your advice in mind.

Thanks again,
Andrew



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

Date: Thu, 14 Jun 2007 22:03:16 GMT
From: "Mumia W." <paduille.4061.mumia.w+nospam@earthlink.net>
Subject: Re: urgent:reading 2 files into 2 different $ variables
Message-Id: <EWici.156$iz5.76@newsread4.news.pas.earthlink.net>

On 06/04/2007 12:00 PM, geot wrote:
> hi Bob
> 
> thank you vey much for the information, pls do  excuse me for not
> giving more details.
> i will try to explain more clearly. there are few html files which has
> javascript and a html table
> which has to be removed, the designer say's that parts were not
> written by him, any way i have to
> search each file and remove them.
> [...]

Try to keep better control of your webserver :-)

This should help remove some of the rouge elements:

     use strict;
     use warnings;
     use File::Slurp;
     use File::Find;

     my $jscript = read_file('data/jscript.txt');
     my $table = read_file('data/table.txt');

     my @htfiles;
     my $wanted = sub {
         no warnings 'once';
         if (/(\.html?|\.cfm)$/i) {
             push @htfiles, $File::Find::name;
         }
     };
     find($wanted, 'data');

     foreach my $file (@htfiles) {
         my $changed = 0;
         my $html = read_file($file);
         $html =~ s/\Q$jscript\E// && $changed++;
         $html =~ s/\Q$table\E// && $changed++;
         if ($changed) {
             write_file("$file.2", $html);
             print "Wrote changed file: $file.2\n";
         }
     }



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

Date: Thu, 14 Jun 2007 11:32:59 -0700
From:  Ash <ashishrai@gmail.com>
Subject: Re: Writing row at a time in Excel using OLE
Message-Id: <1181845979.191313.65100@i38g2000prf.googlegroups.com>

On Jun 14, 2:05 pm, "anton.vanderst...@chello.nl"
<anton.vanderst...@chello.nl> wrote:
> On Jun 14, 7:49 pm, Ash <ashish...@gmail.com> wrote:
>
> > Hi!
>
> > I need to write a row at a time or a whole sheet at a time, anything
> > faster than one cell at a time in Excel sheet using Win32::OLE. Can
> > anyone help me?
>
> > Spreadsheet::WriteExcel module have functions for writing row at a
> > time but I need to modify existing Excel file and the module does not
> > allow that.
>
> > Thank you in advance!
>
> Hey,
>
> You must look for the package Spreadsheet::WriteExcel
> With this package you can create an Excel document realy fast.
>
> With the Perl listing below I  create an Excel file first and then I
> write data in it based on an sql statement.
> In this example I did use an connection to an Access database.
>
> Have fun.
>
> #!

Thanks!But I need to modify existing Excel file and not create a new
one.



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

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 V11 Issue 515
**************************************


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