perl module?

2008-05-02 Thread Levente Kovacs
Ok, I've reached the level, when I want to do perl modules. I've seen
tutorials with the `h2xs' approach, but the problem is that I don't know what
it EXACTLY does.

I'd like to write a code shared among several simple scripts, as a NON-OO
module, used in single program packege. I'd be happy with some #include style
in C, but I don't know how to do that really.

Thanks for your help.

-- 
Levente Kovacs [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl module?

2008-05-02 Thread Gunnar Hjalmarsson

Levente Kovacs wrote:

Ok, I've reached the level, when I want to do perl modules. I've seen
tutorials with the `h2xs' approach, but the problem is that I don't know what
it EXACTLY does.


Exactly? Well, it creates a skeleton. See for instance the 
Step-by-step: Making the module section in perldoc perlnewmod.



I'd like to write a code shared among several simple scripts, as a NON-OO
module, used in single program packege. I'd be happy with some #include style
in C, but I don't know how to do that really.


I'm not sure what it is that you don't know how to do...

Anyway, start with the FAQ entry

perldoc -q create a module

where you find pointers to the relevant docs. Come back here if you have 
a more specific question. It would be pointless to repeat large portions 
of the perldoc here, wouldn't it?


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl module?

2008-05-02 Thread J. Peng
On Fri, May 2, 2008 at 2:54 PM, Levente Kovacs [EMAIL PROTECTED] wrote:
 Ok, I've reached the level, when I want to do perl modules. I've seen
  tutorials with the `h2xs' approach, but the problem is that I don't know what
  it EXACTLY does.

Try this simple one:
http://search.cpan.org/~rjbs/Module-Starter-1.470/lib/Module/Starter.pm


  I'd like to write a code shared among several simple scripts, as a NON-OO
  module, used in single program packege. I'd be happy with some #include style
  in C, but I don't know how to do that really.


I have replied this similiar question on this list, just copy it here.

You can 'source' or 'include' another perl file from the current one.
The ways are like:


#
# the first way
#
The first,you can just require a file,because this file didn't be
declared as a package,so it doesn't has its own namespace,so all
variables in this file can be imported into main package's space.

$ cat mydata.pl
use strict;
our (@key1,@key2);

$key1[64]=0xc120718a1ccce7f8;
$key2[64]=0xeadf28cb82020921;
$key1[128]=0xaf503224b6cff0639cf0dc310a4b1277;
$key2[128]=0x3e1fcbd4e91ca24bb276914de3764cdf;

1;

$ cat usedata.pl
require 'mydata.pl';
print $key1[64];


#
# the second way
#
The second way,you can declare the config file as a package,and export
the needed varibles.When main script use this package,it import those
variables automatically.

$ cat mydata2.pm
package mydata2;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our (@key1,@key2);
our @EXPORT = qw(@key1 @key2);


$key1[64]=0xc120718a1ccce7f8;
$key2[64]=0xeadf28cb82020921;
$key1[128]=0xaf503224b6cff0639cf0dc310a4b1277;
$key2[128]=0x3e1fcbd4e91ca24bb276914de3764cdf;


1;

$ cat usedata2.pl
use mydata2;
print $key1[64];


#
# the third way
#
Both the first way and the second way are not good.Because your config
file is large,the former ways have imported all those large content
into your main script.If your main script is run under cgi/modperl
which is generally multi-process,your memory could be eated quickly.So
the best way is to create an object then multi-process can share the
object if this object was not changed later,since object is only
located in its own namespace.

$ cat mydata3.pm
package mydata3;
use strict;

sub new {
   my $class = shift;
   my (@key1,@key2);


   $key1[64]=0xc120718a1ccce7f8;
   $key2[64]=0xeadf28cb82020921;
   $key1[128]=0xaf503224b6cff0639cf0dc310a4b1277;
   $key2[128]=0x3e1fcbd4e91ca24bb276914de3764cdf;


   bless {key1=[EMAIL PROTECTED],key2=[EMAIL PROTECTED],$class;
}

1;

$ cat usedata3.pl
use mydata3;
my $d = mydata3-new;
print $d-{key1}-[64];

-- 
J. Peng - [EMAIL PROTECTED]
Professional Chinese Squid supports
http://SquidCN.spaces.live.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: I want a perl script for?

2008-05-02 Thread Vinayak dutt
On 5/1/08, Rob Dixon [EMAIL PROTECTED] wrote:

 Vinayak dutt wrote:
 
  As am new to Perl and their is a requirment which says -
 
  1. There exists a .zip which contains folders and file.
 
  2. Search for 'makefile' in the respective folders and store them in a
 text
  file.
 
  3. And search for .c files in them and list them in separate text file.
 
  4. Then the files that exists other than .c files should be removed from
 the
  dir.
 
  Can anybody give me an idea as to how to solve the above req.


 I think that, since you are new to Perl, it would be better to extract the
 contents of the zip archive before you try to process its contents with
 Perl.

 What you have described isn't trivial, and to do it within the context of
 a
 single archive file would be a struggle without a few months of Perl
 experience.


 Rob



Hi Rob,

I understand it would be better if i extract the contents of the zip file.
I have also extracted the zip file now and doing accordingly.
But what is that i have not described/explained correctly in my previous
mail??

Now after extracting the zip files i have lot of folders that contain
makefile in them.
And  am also now able to LIST ALL THE FILENAME'S THAT HAVE MAKEFILE and
THEIR PATH in a separate text file called listof_allmake.txt.

From these makefile i have to list all .c files which i have finished,
i.e, i HAVE LISTED ALL THE .C FILENAMES and THEIR FILEPATH in a separate
text file called listof_cfiles.txt.

Next what i have to do is to take individual filepath of makefile and list
respective .c files in them and call all makefiles recursively in all the
other folders and have to store them separately.

Last step is to remove all the .c files which i have listed separately.

Please get back to me if i am not clear with anything here.


Thanks for your time again
Regards,
Vinayak


Comparing files with regular expressions

2008-05-02 Thread rubinsta
Hello,

I'm a Perl uber-novice and I'm trying to compare two files in order to
exclude items listed on one file from the complete list on the other
file.  What I have so far prints out a third file listing everything
that matches the exclude file from the complete file (which I'm hoping
will be a duplicate of the exclude file) just so I can make sure that
the comparison script is working.  The files are lists of numbers
separated by newlines.  The exclude file has 333 numbers and the
complete file has 9000 numbers.

Here's what I have so far:

#!/usr/bin/perl
use strict;
use warnings;

open(ALL, all.txt) or die $!;
open(EX, exclude.txt) or die $!;
open(OUT,'exTest.txt') or die $!;

my @ex_lines = EX;
my @all_lines = ALL;

foreach $all (@all_lines){
   foreach $ex (@ex_lines){
   if ($ex =~ /(^$all)/){
print OUT $1;
   }
   }
}
close(ALL);
close(EX);
close(OUT);

I realize the nested foreach loops are ugly but I don't know enough to
navigate the filehandles, which as I understand, can only be assigned
to variables in their entirety as an array.  Any thoughts on how this
might be done?

Thanks!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Comparing files with regular expressions

2008-05-02 Thread Chas. Owens
On Thu, May 1, 2008 at 4:09 PM, rubinsta [EMAIL PROTECTED] wrote:
 Hello,

  I'm a Perl uber-novice and I'm trying to compare two files in order to
  exclude items listed on one file from the complete list on the other
  file.  What I have so far prints out a third file listing everything
  that matches the exclude file from the complete file (which I'm hoping
  will be a duplicate of the exclude file) just so I can make sure that
  the comparison script is working.  The files are lists of numbers
  separated by newlines.  The exclude file has 333 numbers and the
  complete file has 9000 numbers.

  Here's what I have so far:

  #!/usr/bin/perl
  use strict;
  use warnings;

  open(ALL, all.txt) or die $!;
  open(EX, exclude.txt) or die $!;
  open(OUT,'exTest.txt') or die $!;
snip

Use the three argument version of open and lexical filehandles:

open my $ex, , exclude.txt
or die could not open exclude.txt: $!;

snip

  my @ex_lines = EX;
  my @all_lines = ALL;
snip

Using filehandles in list context is a bad idea.  It may work now when
the files are small, but data almost always grows.  Unless you are
certain that the file will remain small you should not do this.  Use a
while loop instead.

snip

  foreach $all (@all_lines){
foreach $ex (@ex_lines){
if ($ex =~ /(^$all)/){

This is testing to see if there are any lines in the exclude file that
start with what was in the complete file.  That is if the complete
file was

1
2

and the exclude file was

10
20

then all lines would be excluded.  Is this really what you want?
Also, given that you have not surrounded $all with \Q and \E (like
/^\Q$all\E/) and metacharacters in $all (like *, ., ?, etc.) will be
treated as metacharacters instead of normal characters.  Unless the
lines in complete are know to be regexes this could be bad.  And by
bad I mean everything from mismatches to the dreaded (?{system qq(rm
-rf $ENV{HOME})}).

If you don't have regexes in the complete file but do want to check
for its entires as prefixes in the exclude file, you are better off
using a prefix tree (aka a trie*).  It is an O(m log n)** algorithm,
as opposed to the O(n*m) algorithm you are using now.  There is at
least one Perl implementation: Tree::Trie***.

If you don't have regexes in the complete file and do not want to
check for entries as prefixes in the exclude file you are better off
using a hash set* to test for existence (roughly an O(m+n)
solution).  Luckily in Perl a hash set is easy to build, you just use
a hash variable with the keys being your data and the values all being
either undef or 1 depending on your style (I tend to use 1 for
simplicity's sake, but I think undef might be smaller).  Using a hash
also gives you the freedom to use something like DB_FILE** if the
files get very large (thus saving memory without having to add much
code.

snip
 print OUT $1;
}
}
  }
  close(ALL);
  close(EX);
  close(OUT);
snip

These calls to close at the end of the script are unnecessary.  Only
call close explicitly if you need to close a file before the
filehandle goes out of scope.

Another simple tip is to treat STDIN/files on the command line as your
complete file and STDOUT as your output file.  This form of Perl
script is called a filter and is very easy to write and use.  What
follows is my implementation of the hash set version:

#!/usr/bin/perl

use strict;
use warnings;

#this is a hack to make the script runnable
#without external data files, in a normal
#script you would open a real exclude file
#here
my $exclude = 1\n2\n3\n;
open my $ex, , \$exclude
or die could not open the scalar \$exculde as a file: $!;

my %exists;
$exists{$_} = 1 while $ex;

#this is also a hack, in a normal script
#you would say
#while (my $line = ) {
#to get a loop over STDIN or files specified
#on the commandline
while (my $line = DATA) {
print $line unless $exists{$line};
}

__DATA__
1
2
10
20


* http://en.wikipedia.org/wiki/Trie
** This is big O notation, basically it measure the order of
magnitude of number of steps needed to complete the algorithm.  So, if
you had 1,000 lines in exclude and 10,000 lines in complete it would
take roughly 10,000,000 steps to complete the algorithm you are using
now and only 13,287 with the trie.
*** http://search.cpan.org/~avif/Tree-Trie-1.5/Trie.pm
 http://en.wikipedia.org/wiki/Big_O_notation
* basically a hash with no values used for testing of existance of values
** http://perldoc.perl.org/DB_File.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl module?

2008-05-02 Thread Levente Kovacs
Hi,


On Fri, 2 May 2008 16:32:32 +0800
J. Peng [EMAIL PROTECTED] wrote:

 #
 # the third way
 #


Thanks for your answer, approach #3 works good. That is what I wanted.

Cheers,
-- 
Levente Kovacs [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




IDE for Perl in Linux

2008-05-02 Thread Rodrigo Tavares
Hello,

Today I write my perls scripts with a simple editor.
I found this link http://www.enginsite.com/Perl.htm, but it run only in Windows.

This link http://www.solutionsoft.com/perl.htm, contain the for linux, but have 
to buy.

Anybody knows a simple and good IDE Perl for Linux ? 

By,

Faria







  Abra sua conta no Yahoo! Mail, o único sem limite de espaço para 
armazenamento!
http://br.mail.yahoo.com/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread J. Peng
On Fri, May 2, 2008 at 11:04 PM, Rodrigo Tavares
[EMAIL PROTECTED] wrote:
 Hello,

  Today I write my perls scripts with a simple editor.
  I found this link http://www.enginsite.com/Perl.htm, but it run only in 
 Windows.

  This link http://www.solutionsoft.com/perl.htm, contain the for linux, but 
 have to buy.

  Anybody knows a simple and good IDE Perl for Linux ?

I believe, many Perl guys (including me) use VI/VIM under unix for
their Perl editor.


-- 
J. Peng - [EMAIL PROTECTED]
Professional Chinese Squid supports
http://SquidCN.spaces.live.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File and perl

2008-05-02 Thread Kenneth Wolcott
Now *THAT* is helpful! Descriptive yet succinct w/ references. Methodical.
Awesome.

--Ken Wolcott

On Thu, May 1, 2008 at 6:13 PM, Chas. Owens [EMAIL PROTECTED] wrote:

 On Thu, May 1, 2008 at 8:45 PM, Richard Lee [EMAIL PROTECTED] wrote:
 snip
   ls -ltr | tail -100 | cut -d' ' -f13
 snip

 Let's pick this apart, shall we?

 ls -tr gets all of the (non-hidden) files in the current directory
 reverse sorted by time (the l is unnecessary and is why you need the
 cut later) and the tail takes the last one hundred (or fewer) of them.

 Well, Perl can get all of the (non-hidden) files in the current
 directory very easily with glob*:

 my @files = *;

 The next step is to sort them on mtime (largest mtime values last)
 using stat** to get the mtime and sort*** to sort the list:

 my @files = sort { (stat $a)[9] = (stat $b)[9] } *;

 All of those calls to stat to get the mtime during the sort can be
 expensive, so we might want to do a Schwartzian Transform to speed
 it up:

 my @files =
map { $_-[1] }
sort { $a-[0] = $b-[0] }
map { [(stat)[9], $_] } *;

 To get the last hundred files we can use a list slice* using the
 range operator**:

 my @files = (
map { $_-[1] }
sort { $a-[0] = $b-[0] }
map { [(stat)[9], $_] } *
 )[-100 .. -1];

 But this leaves use with undefs if we have fewer than one hundred
 files in the current directory, so we need a grep*** to weed them
 out:

 my @files = grep defined, (
map { $_-[1] }
sort { $a-[0] = $b-[0] }
map { [( stat)[9], $_ ] } *
 )[-100 .. -1];

 * http://perldoc.perl.org/functions/glob.html
 ** http://perldoc.perl.org/functions/stat.html
 *** http://perldoc.perl.org/functions/sort.html
  http://en.wikipedia.org/wiki/Schwartzian_transform
 * http://perldoc.perl.org/perldata.html#Slices
 ** http://perldoc.perl.org/perlop.html#Range-Operators
 *** http://perldoc.perl.org/functions/grep.html

 --
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the ability to read.

 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/





Re: IDE for Perl in Linux

2008-05-02 Thread Chas. Owens
On Fri, May 2, 2008 at 11:04 AM, Rodrigo Tavares
[EMAIL PROTECTED] wrote:
 Hello,

  Today I write my perls scripts with a simple editor.
  I found this link http://www.enginsite.com/Perl.htm, but it run only in 
 Windows.

  This link http://www.solutionsoft.com/perl.htm, contain the for linux, but 
 have to buy.

  Anybody knows a simple and good IDE Perl for Linux ?
snip

I use VIM, but then I am crusty old UNIX die-hard (see the webcam
image on my website for beardly proof); however, I really liked
ActiveState's Komodo.  There is a free version* and an expensive
version** (around $300).  I would suggest trying out the expensive
version to see if you like the features it has that the free one
doesn't***.

* http://activestate.com/Products/komodo_ide/komodo_edit.mhtml
** http://activestate.com/Products/komodo_ide/index.mhtml
*** http://www.activestate.com/Products/komodo_ide/edit_comparison.mhtml

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl module?

2008-05-02 Thread Gunnar Hjalmarsson

Levente Kovacs wrote:

J. Peng wrote:

On Fri, May 2, 2008 at 2:54 PM, Levente Kovacs [EMAIL PROTECTED] wrote:

 I'd like to write a code shared among several simple scripts, as a NON-OO
 module, ...


snip


#
# the third way
#
Both the first way and the second way are not good.Because your config
file is large,the former ways have imported all those large content
into your main script.If your main script is run under cgi/modperl
which is generally multi-process,your memory could be eated quickly.So
the best way is to create an object then multi-process can share the
object if this object was not changed later,since object is only
located in its own namespace.


Thanks for your answer, approach #3 works good. That is what I wanted.


A slightly surprising response, considering that you in the original 
post said that you would like to write a non-OO module.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl module?

2008-05-02 Thread Levente Kovacs
On Fri, 02 May 2008 18:33:07 +0200
Gunnar Hjalmarsson [EMAIL PROTECTED] wrote:

 Levente Kovacs wrote:
  J. Peng wrote:
  On Fri, May 2, 2008 at 2:54 PM, Levente Kovacs
  [EMAIL PROTECTED] wrote:
   I'd like to write a code shared among several simple scripts, as
  a NON-OO module, ...
 
 snip
 
  #
  # the third way
  #
  Both the first way and the second way are not good.Because your
  config file is large,the former ways have imported all those large
  content into your main script.If your main script is run under
  cgi/modperl which is generally multi-process,your memory could be
  eated quickly.So the best way is to create an object then
  multi-process can share the object if this object was not changed
  later,since object is only located in its own namespace.
  
  Thanks for your answer, approach #3 works good. That is what I
  wanted.
 
 A slightly surprising response, considering that you in the original 
 post said that you would like to write a non-OO module.

Yes, but without it I could not figure out how to achieve it. I think it is
very complicated comparing to C, C++. The more language I learn, the more I
think C(++) is the most flexible language.

Thanks,
Levente

-- 
Levente Kovacs [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread Levente Kovacs
On Fri, 2 May 2008 08:04:04 -0700 (PDT)
Rodrigo Tavares [EMAIL PROTECTED] wrote:

 Anybody knows a simple and good IDE Perl for Linux ? 

Use gvim.

-- 
Levente Kovacs [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl module?

2008-05-02 Thread J. Peng
On Sat, May 3, 2008 at 1:26 AM, Levente Kovacs [EMAIL PROTECTED] wrote:
 The more language I learn, the more I
  think C(++) is the most flexible language.


This is most probably you know C(++) better than others.
For me I think Perl is flexible enough.

-- 
J. Peng - [EMAIL PROTECTED]
Professional Chinese Squid supports
http://SquidCN.spaces.live.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Perl Expect help

2008-05-02 Thread Ravi Malghan
Hi: I am trying to build a simple perl/expect program which will telnet, run a 
command and provide me the result of the command in a string or array to 
process within the script. I have gotten so far as the script telnets, runs the 
command the prints the result in stdout. I can't seem to figure how to get the 
result of the command in a string so I can continue processing and do other 
things within the script.

my $exp = Expect-spawn($command, @params)
or die Cannot spawn $command: $!\n;
$exp-expect($timeout,
   [qr/login:/ = sub {my $exp = shift;
   $exp-send($username\n);
   exp_continue;
   } ],
   [qr/Password: $/ = sub {my $exp = shift;
   $exp-send($password\n);
   exp_continue;
   } ],
   [qr/READY$/ = sub {my $exp = shift;
   $exp-send(select Running from Service WHERE Name = 
'CheckForRemedyTickets';\n);
   exp_continue;
   } ],

  );



TIA
Ravi



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread Dr.Ruud
Rodrigo Tavares schreef:

 Anybody knows a simple and good IDE Perl for Linux ?

http://e-p-i-c.sourceforge.net/ 

-- 
Affijn, Ruud

Gewoon is een tijger.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




another help on input record separator clarity please

2008-05-02 Thread Richard Lee

Please help me with this another misunderstanding of my perl.

My plan is to make input record separator to \n\n so that file records 
are separated by blank space.. and then

collect information and push them into array by joining.

But when I run this, it says uninitilized values...  I am doing 
something wrong.. aren't i



cat ././f_this.pl
#!/usr/bin/perl

use strict;
use warnings;

open FH, , /tmp/fgg, or die cannot $!\n;

my @yahoo;
my $count;

while (FH) {
   local $/ = \n\n;
   ++$count;

   my $fgh =~ /fgh\s+(\S+)/;
   my $ijk =~ /ijk\s+(\S+)/;
   my $lmk =~ /lmk\s+(\S+)/;
 
   push @yahoo, join('_', $fgh, $ijk, $lmk);

}

[EMAIL PROTECTED] tmp]# cat fgg
abc
def
fgh 111
ijk 333
lmn 2

abc
def
fgh 222
ijk 121
lmk 23
[EMAIL PROTECTED] tmp]#

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: another help on input record separator clarity please

2008-05-02 Thread Chas. Owens
On Fri, May 2, 2008 at 5:55 PM, Richard Lee [EMAIL PROTECTED] wrote:
snip
  while (FH) {
local $/ = \n\n;
snip
  }
snip

You want $/ to have an effect on FH, but it is localized to inside
of the loop.  You need to say

{
local $/ = \n\n;
while (FH) {
}
}

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: another help on input record separator clarity please

2008-05-02 Thread Richard Lee

Chas. Owens wrote:

On Fri, May 2, 2008 at 5:55 PM, Richard Lee [EMAIL PROTECTED] wrote:
snip
  

 while (FH) {
   local $/ = \n\n;


snip
  

 }


snip

You want $/ to have an effect on FH, but it is localized to inside
of the loop.  You need to say

{
local $/ = \n\n;
while (FH) {
}
}

  

thanks..

works for me,


#!/usr/bin/perl

use strict;
use warnings;

open FH, , /tmp/fgg, or die cannot $!\n;

my @yahoo;
my $count;

{
  local $/ = \n\n;
  while (FH) {
   ++$count;

   #my $fgh =~ /fgh\s+(\S+)/;
   my ($f,$i,$l);
   if (/fgh\s+(\d+)/smx) {
  $f = $1;
   }
   if (/ijk\s+(\S+)/smx) {
  $i = $1;
   }
   if (/lmn\s+(\S+)/smx) {
  $l = $1;
   }

   push @yahoo, join('_', $f, $i, $l);
  }
}

for (@yahoo)  {
   print $_\n;
}
[EMAIL PROTECTED] tmp]# cat fgg
abc
def
fgh 111
ijk 333
lmn 2

abc
def
fgh 222
ijk 121
lmn 23
[EMAIL PROTECTED] tmp]# ./f_this.pl
111_333_2
222_121_23

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Comparing files with regular expressions

2008-05-02 Thread rubinsta
Many thanks, Chas.  These are all very helpful (and educational!)
suggestions.  I adapted your example like so (specifying the all.txt
on the command-line):

#!/usr/bin/perl
use strict;
use warnings;

open my $ex, , exclude.txt or die $!;
open my $out, , exTest.txt or die $!;

my %exists;
$exists{$_} = 1 while $ex;

## I changed the unless to if so I could easily
## compare the output of the script to the
## original exclude.txt file

while (my $line = ){
print $out $line if $exists{$line};
}

The problem is the exlude.txt and exTest.txt do not match.  Everything
in the exTest.txt file is also in the exclude.txt file but there are a
number of lines that appear in the all.txt and the exclude.txt that do
not end up in exTest.txt.  The numbers are EANs and are thus all
exactly the same format, e.g. 9780657007423.  Any thoughts as to why
some of the matches are getting missed?

Just out of beginner curiosity, why did you suggest I use the 3
argument filehandle instead of:
open(EX, exclude1.txt) or die $!

Thanks again for all your help!


On May 2, 7:41 am, [EMAIL PROTECTED] (Chas. Owens) wrote:
 On Thu, May 1, 2008 at 4:09 PM, rubinsta [EMAIL PROTECTED] wrote:
  Hello,

   I'm a Perl uber-novice and I'm trying to compare two files in order to
   exclude items listed on one file from the complete list on the other
   file.  What I have so far prints out a third file listing everything
   that matches the exclude file from the complete file (which I'm hoping
   will be a duplicate of the exclude file) just so I can make sure that
   the comparison script is working.  The files are lists of numbers
   separated by newlines.  The exclude file has 333 numbers and the
   complete file has 9000 numbers.

   Here's what I have so far:

   #!/usr/bin/perl
   use strict;
   use warnings;

   open(ALL, all.txt) or die $!;
   open(EX, exclude.txt) or die $!;
   open(OUT,'exTest.txt') or die $!;

 snip

 Use the three argument version of open and lexical filehandles:

 open my $ex, , exclude.txt
 or die could not open exclude.txt: $!;

 snip

   my @ex_lines = EX;
   my @all_lines = ALL;

 snip

 Using filehandles in list context is a bad idea.  It may work now when
 the files are small, but data almost always grows.  Unless you are
 certain that the file will remain small you should not do this.  Use a
 while loop instead.

 snip



   foreach $all (@all_lines){
 foreach $ex (@ex_lines){
 if ($ex =~ /(^$all)/){

 This is testing to see if there are any lines in the exclude file that
 start with what was in the complete file.  That is if the complete
 file was

 1
 2

 and the exclude file was

 10
 20

 then all lines would be excluded.  Is this really what you want?
 Also, given that you have not surrounded $all with \Q and \E (like
 /^\Q$all\E/) and metacharacters in $all (like *, ., ?, etc.) will be
 treated as metacharacters instead of normal characters.  Unless the
 lines in complete are know to be regexes this could be bad.  And by
 bad I mean everything from mismatches to the dreaded (?{system qq(rm
 -rf $ENV{HOME})}).

 If you don't have regexes in the complete file but do want to check
 for its entires as prefixes in the exclude file, you are better off
 using a prefix tree (aka a trie*).  It is an O(m log n)** algorithm,
 as opposed to the O(n*m) algorithm you are using now.  There is at
 least one Perl implementation: Tree::Trie***.

 If you don't have regexes in the complete file and do not want to
 check for entries as prefixes in the exclude file you are better off
 using a hash set* to test for existence (roughly an O(m+n)
 solution).  Luckily in Perl a hash set is easy to build, you just use
 a hash variable with the keys being your data and the values all being
 either undef or 1 depending on your style (I tend to use 1 for
 simplicity's sake, but I think undef might be smaller).  Using a hash
 also gives you the freedom to use something like DB_FILE** if the
 files get very large (thus saving memory without having to add much
 code.

 snip print OUT $1;
 }
 }
   }
   close(ALL);
   close(EX);
   close(OUT);

 snip

 These calls to close at the end of the script are unnecessary.  Only
 call close explicitly if you need to close a file before the
 filehandle goes out of scope.

 Another simple tip is to treat STDIN/files on the command line as your
 complete file and STDOUT as your output file.  This form of Perl
 script is called a filter and is very easy to write and use.  What
 follows is my implementation of the hash set version:

 #!/usr/bin/perl

 use strict;
 use warnings;

 #this is a hack to make the script runnable
 #without external data files, in a normal
 #script you would open a real exclude file
 #here
 my $exclude = 1\n2\n3\n;
 open my $ex, , \$exclude
 or die could not open the scalar \$exculde as a file: $!;

 my %exists;
 $exists{$_} = 1 while $ex;

 #this is also a hack, in a normal script
 #you would say
 #while (my $line = ) {
 #to 

Re: another help on input record separator clarity please

2008-05-02 Thread Dr.Ruud
Richard Lee schreef:

 while (FH) {
 local $/ = \n\n;
 ++$count;

That $count is already in $. (see perlvar) 


 my $fgh =~ /fgh\s+(\S+)/;
 my $ijk =~ /ijk\s+(\S+)/;
 my $lmk =~ /lmk\s+(\S+)/;

You might want to use a hash:

  $fil{$1} = $2 while m/\b(fgh|ijk|lmk)\s+(\S+)/g; 

-- 
Affijn, Ruud

Gewoon is een tijger.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread J. D.
Gvim or vim have an add on called perl-support that provides some very handy
IDE-like features.

Best regards,

J. D.

On 5/2/08, Dr.Ruud [EMAIL PROTECTED] [EMAIL PROTECTED]
wrote:

 Rodrigo Tavares schreef:


  Anybody knows a simple and good IDE Perl for Linux ?


 http://e-p-i-c.sourceforge.net/


 --
 Affijn, Ruud

 Gewoon is een tijger.


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/





Re: another help on input record separator clarity please

2008-05-02 Thread Joshua Hoblitt
Richard,

The unitalized warnings are probably coming from one or more of $fgh, $ijk,
$lmk being undefined because the regex failed to match anything.  You can test
this by trying to print the values of these variables.

They are probably undefinately because the record seperator is being set
lexically inside of the loop (and thus won't apply to the outer while).  Please
consider this code:

local $/ = \n\n;
while (FH) {
++$count;
 
my $fgh =~ /fgh\s+(\S+)/;
my $ijk =~ /ijk\s+(\S+)/;
my $lmk =~ /lmk\s+(\S+)/;

no warnings qw(uninitialized);
warn 1: $fgh, 2: $ijk, 3: $lmk\n;
  
push @yahoo, join('_', $fgh, $ijk, $lmk);
use warnings;
}

Cheers,

-J

--
On Fri, May 02, 2008 at 05:55:58PM -0400, Richard Lee wrote:
 Please help me with this another misunderstanding of my perl.
 
 My plan is to make input record separator to \n\n so that file records 
 are separated by blank space.. and then
 collect information and push them into array by joining.
 
 But when I run this, it says uninitilized values...  I am doing 
 something wrong.. aren't i
 
 
 cat ././f_this.pl
 #!/usr/bin/perl
 
 use strict;
 use warnings;
 
 open FH, , /tmp/fgg, or die cannot $!\n;
 
 my @yahoo;
 my $count;
 
 while (FH) {
local $/ = \n\n;
++$count;
 
my $fgh =~ /fgh\s+(\S+)/;
my $ijk =~ /ijk\s+(\S+)/;
my $lmk =~ /lmk\s+(\S+)/;
  
push @yahoo, join('_', $fgh, $ijk, $lmk);
 }
 
 [EMAIL PROTECTED] tmp]# cat fgg
 abc
 def
 fgh 111
 ijk 333
 lmn 2
 
 abc
 def
 fgh 222
 ijk 121
 lmk 23
 [EMAIL PROTECTED] tmp]#
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Comparing files with regular expressions

2008-05-02 Thread Chas. Owens
On Fri, May 2, 2008 at 10:44 AM, rubinsta [EMAIL PROTECTED] wrote:
snip
 Any thoughts as to why
  some of the matches are getting missed?
snip

Not off hand.  I will extract your code and do some tests.  Can you
send me your data or is it sensitive?

snip
  Just out of beginner curiosity, why did you suggest I use the 3
  argument filehandle instead of:
  open(EX, exclude1.txt) or die $!
snip

Because the three argument version of open is safer.  It doesn't
matter in the code you wrote because you used a literal string, but if
you say

open FH, $file or die could not open $file: $!;

expecting FH to be a read filehandle and $file contains the filename
important, you will wind up with a write filehandle.  Specifying
the type of filehandle you want separately from the file is an
important safety feature.  Using the old version of open is a bad
habit you should not develop.  You should know it exists (like many of
the other bad habits left over from earlier versions of the Language)
in case you run into code that uses it, but you shouldn't use it
yourself.  I would also strongly recommend using lexical filehandles
instead of the old bareword style for similar reasons.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: another help on input record separator clarity please

2008-05-02 Thread Chas. Owens
On Fri, May 2, 2008 at 5:08 PM, Joshua Hoblitt [EMAIL PROTECTED] wrote:
snip
  They are probably undefinately because the record seperator is being set
  lexically inside of the loop (and thus won't apply to the outer while).  
 Please
  consider this code:


 local $/ = \n\n;
snip

An important nitpick: that is a localized version of $/, not a lexical
version.  See

http://perldoc.perl.org/functions/local.html

for more information.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread eko hermiyanto
GNU Emacs my friend. GNU Emacs.

On Sat, May 3, 2008 at 6:22 AM, J. D. [EMAIL PROTECTED] wrote:

 Gvim or vim have an add on called perl-support that provides some very
 handy
 IDE-like features.

 Best regards,

 J. D.

 On 5/2/08, Dr.Ruud [EMAIL PROTECTED] [EMAIL PROTECTED] 
 [EMAIL PROTECTED] [EMAIL PROTECTED]
 wrote:
 
  Rodrigo Tavares schreef:
 
 
   Anybody knows a simple and good IDE Perl for Linux ?
 
 
  http://e-p-i-c.sourceforge.net/
 
 
  --
  Affijn, Ruud
 
  Gewoon is een tijger.
 
 
  --
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  http://learn.perl.org/
 
 
 



Re: another help on input record separator clarity please

2008-05-02 Thread Richard Lee

Chas. Owens wrote:

On Fri, May 2, 2008 at 5:08 PM, Joshua Hoblitt [EMAIL PROTECTED] wrote:
snip
  

 They are probably undefinately because the record seperator is being set
 lexically inside of the loop (and thus won't apply to the outer while).  Please
 consider this code:


local $/ = \n\n;


snip

An important nitpick: that is a localized version of $/, not a lexical
version.  See

http://perldoc.perl.org/functions/local.html

for more information.

  
Chas, does this mean as long as we don't call another sub from the block 
we declare local, we should be good. Correct?
I like using local on some of these variables.. for some reason. 
Most likely due to misleading name :-)  Can't help myself.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread Richard Lee

eko hermiyanto wrote:

GNU Emacs my friend. GNU Emacs.

On Sat, May 3, 2008 at 6:22 AM, J. D. [EMAIL PROTECTED] wrote:

  

Gvim or vim have an add on called perl-support that provides some very
handy
IDE-like features.

Best regards,

J. D.

On 5/2/08, Dr.Ruud [EMAIL PROTECTED] [EMAIL PROTECTED] 
[EMAIL PROTECTED] [EMAIL PROTECTED]
wrote:


Rodrigo Tavares schreef:


  

Anybody knows a simple and good IDE Perl for Linux ?


http://e-p-i-c.sourceforge.net/


--
Affijn, Ruud

Gewoon is een tijger.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



  


  
I never considered emacs because all the servers I log in does not have 
emacs and for whatever reason, I start to think learning emacs was a 
waste of time and

just learned vi.

Can you tell me what extra benefit emacs will provide to perl programmar?
Also, my other excuse not learning emacs is ,well I got used to vi(and 
therefore became lazy to learn another editor) and well if somebody 
showed me
or provide me w/ the link that shows the benefit of using emacs for 
programming and also(just stupid reason but.. ) it looks kinda hard to 
learn(or at least

get going on it).

Please comment!!

thanks.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: IDE for Perl in Linux

2008-05-02 Thread J. Peng
On Sat, May 3, 2008 at 9:53 AM, Richard Lee [EMAIL PROTECTED] wrote:

  Can you tell me what extra benefit emacs will provide to perl programmar?

I have sawn that:

Emacs == Emacs Makes a Computer Slow

:-)

-- 
J. Peng - [EMAIL PROTECTED]
Professional Chinese Squid supports
http://SquidCN.spaces.live.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




displaying inline images in HTML email

2008-05-02 Thread David Newman
I'm trying to include a PDF image both inline and as an attachment to an 
HTML email. The MIME::Lite module supports this, and the documentation 
even gives an example:


http://tinyurl.com/uemf7

However, when I try this with the code below, the inline image doesn't 
display. (The attachment is fine, though.) This is puzzling given that 
it's basically the same code as in the example above.


I'm new to img src=cid:  tags. I've also tried this with img 
src=cid:08Bike.pdf but that doesn't display inline either.


How to get this working?

Many thanks

dn

#!/usr/bin/perl -w

use strict;
use MIME::Lite;

my $msg = MIME::Lite-new(
To  ='[EMAIL PROTECTED]',
Subject ='HTML with in-line images!',
Type='multipart/related'
);

$msg-attach(Type = 'text/html',
Data = qq{ body
Here's imy/i image:
img src=cid:bike
/body }
);

$msg-attach(Type = 'application/pdf',
Id   = 'cruisin',
Path = '/home/someuser/08Bike.pdf'
);

$msg-send();





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/