using (sharing) variables between perl files

2006-06-19 Thread Varga Pavol
Hi,
may be very simple, but I don't understand well how to simple use (share)
varables between perl files?
I have (very) big perl script and I would like to divide it into more small
scripts to make it all more transparent.
Or, how to use one @array or one $array_ref for more than one file.pl?

I just read book from OReilly - Learning perl and some chapters about
Intermediate perl, but I am little bit confused with objects, packages and
probably more ... :-(


Many thanks.

-- 
palo

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




Re: using (sharing) variables between perl files

2006-06-19 Thread M. Kristall

Varga Pavol wrote:

Hi,

Hello

may be very simple, but I don't understand well how to simple use (share)
varables between perl files?
I have (very) big perl script and I would like to divide it into more small
scripts to make it all more transparent.
perldoc is your friend. If you have separate 'package's within your 
file, see 'use' http://perldoc.perl.org/functions/use.html. Otherwise, 
you probably want 'require' 
http://perldoc.perl.org/functions/require.html (or 'do' 
http://perldoc.perl.org/functions/do.html if you don't want your 
program to immediately quit if a file couldn't be included).

Or, how to use one @array or one $array_ref for more than one file.pl?

After including files, you just use those variables as you would any others.


Many thanks.

Your welcome (and I hope I helped).

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




Re: using (sharing) variables between perl files

2006-06-19 Thread Prabu

Varga Pavol wrote:

Hi,
may be very simple, but I don't understand well how to simple use (share)
varables between perl files?
I have (very) big perl script and I would like to divide it into more small
scripts to make it all more transparent.
Or, how to use one @array or one $array_ref for more than one file.pl?

I just read book from OReilly - Learning perl and some chapters about
Intermediate perl, but I am little bit confused with objects, packages and
probably more ... :-(


Many thanks.

  

Hello ,

Hope this Small example give you some idea.

$cat file1.pl
#!/usr/bin/perl
$name=prabu;

$cat file2.pl
#!/usr/bin/perl
require file1.pl;
print $name.\n;

$ perl file2.pl
prabu


--
Prabu.M.A

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




Re: using (sharing) variables between perl files

2006-06-19 Thread Anthony Ettinger

On 6/19/06, Prabu [EMAIL PROTECTED] wrote:

Varga Pavol wrote:
 Hi,
 may be very simple, but I don't understand well how to simple use (share)
 varables between perl files?
 I have (very) big perl script and I would like to divide it into more small
 scripts to make it all more transparent.
 Or, how to use one @array or one $array_ref for more than one file.pl?

 I just read book from OReilly - Learning perl and some chapters about
 Intermediate perl, but I am little bit confused with objects, packages and
 probably more ... :-(


 Many thanks.


Hello ,

Hope this Small example give you some idea.

 $cat file1.pl
#!/usr/bin/perl
$name=prabu;

$cat file2.pl
#!/usr/bin/perl
require file1.pl;
print $name.\n;

$ perl file2.pl
prabu


--
Prabu.M.A

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





read about object oriented perl. or you can always use a config file.

--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html

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




Re: using (sharing) variables between perl files

2006-06-19 Thread Chad Perrin
On Mon, Jun 19, 2006 at 05:02:33PM +0200, Varga Pavol wrote:
 Hi,
 may be very simple, but I don't understand well how to simple use (share)
 varables between perl files?
 I have (very) big perl script and I would like to divide it into more small
 scripts to make it all more transparent.
 Or, how to use one @array or one $array_ref for more than one file.pl?

To summarize what others have said:

You can include the code of one Perl file in another via use or
require, and you can save variable data in an external file (such as a
config file) that each separate Perl script parses to populate
variables.  You could even simply have one script call another, and get
output from that other for use as variable data in the script that
called it.

The best way to do it in your case will vary, depending on what
exactly you're doing with your Perl scripts.

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.

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




Re: newlines on win32, old mac, and unix

2006-06-19 Thread Anthony Ettinger

   # order matters
   $raw_text =~ s/\015\012/\n/g;
   $raw_text =~ s/\012/\n/g unless \n eq \012;
   $raw_text =~ s/\015/\n/g unless \n eq \015;



Does it make any difference if I use s/\cM\cJ/cJ/ vs. s/\015\012/\n/g ?





Since the newline convention is not necessarily the one in the
runtime platform you cannot write a line-oriented script. If files
are too big to slurp then you'd work on chunks, but need to check by
hand whether a CRLF has been cut in the middle.



I'm reading each line in a while loop, so it should work fine on a large file?



--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html

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




Re: newlines on win32, old mac, and unix

2006-06-19 Thread John W. Krahn
Anthony Ettinger wrote:
# order matters
$raw_text =~ s/\015\012/\n/g;
$raw_text =~ s/\012/\n/g unless \n eq \012;
$raw_text =~ s/\015/\n/g unless \n eq \015;
 
 Does it make any difference if I use s/\cM\cJ/cJ/ vs. s/\015\012/\n/g ?

The string cJ in your example is completely different than the string \n
and even if you had used \cJ it would still not be the same some of the time
and you don't have the /g option on your example.


John
-- 
use Perl;
program
fulfillment

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




Re: newlines on win32, old mac, and unix

2006-06-19 Thread Anthony Ettinger

On 6/19/06, John W. Krahn [EMAIL PROTECTED] wrote:

Anthony Ettinger wrote:
# order matters
$raw_text =~ s/\015\012/\n/g;
$raw_text =~ s/\012/\n/g unless \n eq \012;
$raw_text =~ s/\015/\n/g unless \n eq \015;

 Does it make any difference if I use s/\cM\cJ/cJ/ vs. s/\015\012/\n/g ?

The string cJ in your example is completely different than the string \n
and even if you had used \cJ it would still not be the same some of the time
and you don't have the /g option on your example.



Not according to the perlport page, it reads as though they are
synonymous with each other. Also, why would a newline not be at the
end of a line? I don't see that /g *has* to be there except for the
mac files, which is what I have.




John
--
use Perl;
program
fulfillment

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






--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html

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




Re: newlines on win32, old mac, and unix

2006-06-19 Thread John W. Krahn
Anthony Ettinger wrote:
 On 6/19/06, John W. Krahn [EMAIL PROTECTED] wrote:
 Anthony Ettinger wrote:
 # order matters
 $raw_text =~ s/\015\012/\n/g;
 $raw_text =~ s/\012/\n/g unless \n eq \012;
 $raw_text =~ s/\015/\n/g unless \n eq \015;
 
  Does it make any difference if I use s/\cM\cJ/cJ/ vs. s/\015\012/\n/g ?

 The string cJ in your example is completely different than the
 string \n
 and even if you had used \cJ it would still not be the same some of
 the time
 and you don't have the /g option on your example.
 
 Not according to the perlport page, it reads as though they are
 synonymous with each other. Also, why would a newline not be at the
 end of a line? I don't see that /g *has* to be there except for the
 mac files, which is what I have.

I don't have the original post for context but a lot depends on what the input
record separator contains and what layer PerIO is using and what operating
system the program is running on.


John
-- 
use Perl;
program
fulfillment

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




Re: newlines on win32, old mac, and unix

2006-06-19 Thread Xavier Noria

On Jun 19, 2006, at 22:45, Anthony Ettinger wrote:


   # order matters
   $raw_text =~ s/\015\012/\n/g;
   $raw_text =~ s/\012/\n/g unless \n eq \012;
   $raw_text =~ s/\015/\n/g unless \n eq \015;



Does it make any difference if I use s/\cM\cJ/cJ/ vs. s/\015\012/\n/ 
g ?


The regexp is OK, the replacement string is not, because \cJ is not  
necessarily eq \n. The latter is portable, the former is not.



Since the newline convention is not necessarily the one in the
runtime platform you cannot write a line-oriented script. If files
are too big to slurp then you'd work on chunks, but need to check by
hand whether a CRLF has been cut in the middle.



I'm reading each line in a while loop, so it should work fine on a  
large file?


The while loops over lines ***as long as they are encoded using the  
conventions of the runtime platform***. The diamond operator uses $/  
as separator, which in turn is \n by default. Since the purpose of  
your script is to deal with *any* newline convention, in general a  
while loop like


  while (my $line = $fh) { ... }

looks suspicious. The variable should be called $chunk_of_text,  
instead of $line. You don't know whether you'll get a line.  
Suspicious, may signal the programmer does not fully understand  
what's going on.


For instance, TextWrangler is known to use old-Mac conventions by  
default (last time I checked). If you read a file like that with that  
while in either Unix or Windows you'll slurp the entire file in a  
single iteration. That is, $line will contain the whole file.


In general, to be robust to newline conventions you need to to some  
munging by hand before using regular, portable line-oriented idioms.


-- fxn


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




Re: using (sharing) variables between perl files

2006-06-19 Thread Jeff Peng




The best way to do it in your case will vary, depending on what
exactly you're doing with your Perl scripts.



I do agree that.If you develop large programs where multi-processes should 
share some variables (including scalar,array or hash),you could use DB_File 
or other database  to store and share varibles across those multi-processes.

Also you could see this article:
http://www.perl.com/pub/a/2002/04/23/mod_perl.html?page=1



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




unscrambler speedup rewrite

2006-06-19 Thread Jeremy Kister
I wrote a word descrambler that works very well, but is very slow 
compared to http://www.jumble.org


I'm wondering how others could write to code so that it'd find words faster.

#useful with dictionary from http://wordlist.sourceforge.net/


use strict;

my %dictionary;
opendir(DIR, $ENV{'HOME'}/data/dictionary/);
foreach my $file (grep {/^english-word/} readdir DIR){
open(DICT, $ENV{'HOME'}/data/dictionary/$file);
warn reading data/dictionary/$file\n;
while(DICT){
chop;
next unless(/^[a-z]+$/);
my $len = length($_);
push @{$dictionary{$len}}, $_;
}
close DICT;
}
closedir DIR;

while(1){
print word: ;
chop(my $scramble = STDIN);
$scramble =~ s/\s+//g;

my $slen = length($scramble);
my @s = split //, $scramble;
foreach my $word (@{$dictionary{$slen}}){
my @w = split //, $word;
my $found;
foreach my $sl (0..($slen-1)){
foreach my $l (0..($slen-1)){
if($s[$sl] eq $w[$l]){
delete $w[$l];
$found++;
last;
}
}
last unless($found);
}
next unless($found == $slen);
print $word\n;
}
}



--

Jeremy Kister
http://jeremy.kister.net./

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




Re: unscrambler speedup rewrite

2006-06-19 Thread Jeremy Kister

On 6/19/2006 11:54 PM, Jeremy Kister wrote:
I wrote a word descrambler that works very well, but is very slow 
compared to http://www.jumble.org


already found a very important piece that I missed ($lfound)..

while(1){
print word: ;
chop(my $scramble = STDIN);
$scramble =~ s/\s+//g;

my $slen = length($scramble);
my @s = split //, $scramble;
foreach my $word (@{$dictionary{$slen}}){
my @w = split //, $word;
my $found;
foreach my $sl (0..($slen-1)){
my $lfound;
foreach my $l (0..($slen-1)){
if($s[$sl] eq $w[$l]){
delete $w[$l];
$found++;
$lfound=1;
last;
}
}
last unless($found  $lfound);
}
next unless($found == $slen);
print $word\n;
}
}




--

Jeremy Kister
http://jeremy.kister.net./

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