Conserving memory

2008-10-21 Thread kenTk
If I populate @array
Then
@array=();
Is the memory that was used for that array now freed?

Similarly
If I populate the anonymous array  @{$arrayREFS[$index]}
Then
@{$arrayREFS[$index]}=();
Is the memory that was used for that anonymous array now freed?

Thanks in advance


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




Problem with Perl upload with $CGI::POST_MAX

2008-10-21 Thread leolim818
Hi All,

I try to upload file to my perl upload program and I have restricted
the file upload with 100KB ($CGI::POST_MAX = 1024 * 100;), when a
uploaded file exceed the size limit, an error message will be shown by
calling $query-cgi_error().

It's working fine with all type of files but except for file with the
image content. When the file with image content exceed the size limit,
it show the following error at internet explorer:

The page cannot be displayed

Cannot find server or DNS Error
Internet Explorer 

Please help and advice.

Thanks  Regards,
Leo


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




Reading from multiple sockets.

2008-10-21 Thread Kammen van, Marco, Springer SBM NL
Hi All!

I'm pretty new to working with sockets in perl, looked around for days
for a proper solution for my IRC/DCC problem but couldn't find one.

The connection to the server uses a socket with a neverending while
loop: 
 
sub connection {
  # Make The Initial Server Connection! #
  $con = IO::Socket::INET-new(PeerAddr=$server,
 PeerPort=$port,
 Proto='tcp',
 Timeout='30') || print Error! $!\n;

while($answer = $con) {

http://www.perlmonks.org/?abspart=1;displaytype=displaycode;node_id=718
388;part=1  

All works fine untill i want to make a DCC connection using another
socket: 
$dcc = IO::Socket::INET-new(PeerAddr=$2,
   PeerPort=$3,
   Proto='tcp',
   Timeout='30') || print Error! $!\n;
  print $dcc Connection Established!\n;
  while ($talk = $dcc) {

http://www.perlmonks.org/?abspart=1;displaytype=displaycode;node_id=718
388;part=2  

The first loop is being blocked? 
I only receive input/output from the second loop untill this one is
closed again, then the first loop continues
How can i get a continuous flow from both?
I've read some bits about IO::Socket::Select, and have also been playing
with multiple threads, but i prefer the first option... Any help is very
appreciated!

 
-   
Marco van Kammen
Springer
System Manager  Postmaster
-   
Van Godewijckstraat 30 | 3311 GX
Office Number: 05E21 
P.O. Box 17 | 3300 AA
Dordrecht | The Netherlands 
tel  +31 (0) 78 657 6446
fax  +31 (0) 78 657 6302
[EMAIL PROTECTED]
www.springer.com http://www.springer.com/  
-   
 
 


Re: Problem with Perl upload with $CGI::POST_MAX

2008-10-21 Thread Jeff Pang
2008/10/21  [EMAIL PROTECTED]:


 It's working fine with all type of files but except for file with the
 image content. When the file with image content exceed the size limit,
 it show the following error at internet explorer:

 The page cannot be displayed

 Cannot find server or DNS Error
 Internet Explorer 


For CGI programming, you should always read the log items in
error_log, that will tell you what problems happened.


-- 
Jeff Pang
http://home.arcor.de/pangj/

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




expect error

2008-10-21 Thread Sandeep Kumar

hi all,

i am using expect module to connect to a remote server. i am getting the 
following error.


Cannot sync with child: Interrupted system call at 
/perl_path/lib/Expect.pm line 134,


somebody please let me know a solution for this.

thanks,
sandeep.


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




two questions on my perl exam.

2008-10-21 Thread birdinforest
I am a student and enrolled Unix programing this semester.
There are two questions relating to perl  I can not work out in the
last exam ( Actually I have write out my code, however the exam system
marked it as wrong). Please help me to point out the fault. Thanks.

QUESTION 1
Write a perl script called perl_has_number.pl which takes one
argument
which is any string. The script should output true if the string
contains any digits, and false otherwise.

MY CODE:
#!/usr/bin/perl -w
if ($ARGV[0]=~ /\d+/)
{
  print true\n;
}
else
{
  print false\n;
}

QUESTION 2
Write a perl script called perl_digits.pl which takes any number of
arguments. The script should return the number found by deleting all
non-digits.

Example:

./perl_digits.pl and1 pan2d 30rr and 4

will return 12304

MY CODE:
#!/usr/bin/perl -w
sub outcom
{
  $outcome=;
foreach $a (@ARGV)
  {
if ($a=~/\d+/)
{
  $outcome=$outcome.$;
}
  }
return $outcome;
}

$digital=outcom(@ARGV);
print $digital . \n;

In my view, both code could give out right results. Could you help me
to find the fault?


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




last element during foreach loop

2008-10-21 Thread six24hourdays
Hello,

I would like to know how to test for the last element of a list during
a foreach loop, e.g.

foreach $element (@List) {
if (this is the last element) {
do something
}
}

What would I use for the IF condition?

Thanks,
Dave


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




RE: Reading from multiple sockets.

2008-10-21 Thread Kammen van, Marco, Springer SBM NL

Hi All!

I'm pretty new to working with sockets in perl, looked around for days
for a proper solution for my IRC/DCC problem but couldn't find one.

The connection to the server uses a socket with a neverending while
loop: 

All works fine untill i want to make a DCC connection using another
socket: 

The first loop is being blocked? 
I only receive input/output from the second loop untill this one is
closed again, then the first loop continues
How can i get a continuous flow from both?
I've read some bits about IO::Socket::Select, and have also been
playing
with multiple threads, but i prefer the first option... Any help is
very
appreciated!

Marco van Kammen

You need to use select, or put a timeout on can_read on both sockets.

See the following for how to use timeouts:
http://perlmonks.org?node_id=716603

http://perlmonks.org?node_id=602621

zentara

Thanks for your input
The whole select thingie is still a bit blurry to me...(I really need a
Book). 

Which I forgot to mention is that both my sockets are connected to
different ports... (54321  1024)
And also, one socket is connected to a server, and the other socket is
accepting from a client 
Does this change anything??

So Loop 1 is reading stuff that is happening on the server.
And Loop 2 is reading stuff that a direct client is sending.

Marco!





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




Re: last element during foreach loop

2008-10-21 Thread Jeff Pang
2008/10/21 six24hourdays [EMAIL PROTECTED]:
 Hello,

 I would like to know how to test for the last element of a list during
 a foreach loop, e.g.

 foreach $element (@List) {
if (this is the last element) {
do something
}
 }


my $i =0;
for (@list) {
 if ($i == $#list) {

 }
 $i ++;
}


-- 
Jeff Pang
http://home.arcor.de/pangj/

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




eval, ualarm and IO::Socket

2008-10-21 Thread Deviloper
Hi there,

I just wanted to ask if somebody has implementet an nonblocking socket reader,
which uses Time::HiRes ualarm() function (or nativ perls alarm function) to 
break the waiting on data in socket (if the socket has no data)
after a view microsecond... (in eval block) then do something else, like 
looking for signals etc... and then start listening on the socket again.

(Or is this a stupid idea anyway?)

I ask, because i don´t want to implement a real nonblocking io::socket reader, 
because this is very though and when reading nonblocking I can´t handle 
incoming signales fast enough.

(Somehow POE came to my mind... maybe there is something better (like 
wheel:socket or something like that...) )

Thanks,
D.



Re: $o-document vs $o-document()

2008-10-21 Thread Jay Savage
On Thu, Oct 9, 2008 at 6:04 AM, Peter Scott [EMAIL PROTECTED] wrote:
 On Wed, 08 Oct 2008 04:59:19 -0700, John W. Krahn wrote:
 oldyork90 wrote:
 I am using a module having documentation saying document() is a
 method.  However, I see it used as

 $o-document;

 Can you reference a method in this way?  (I takes no args).  I always
 thought

 $o-document() and $o-document meant different things, function
 verses attribute.

 perldoc perlsub

  To call subroutines:

  NAME(LIST);#  is optional with parentheses.
  NAME LIST; # Parentheses optional if predeclared/imported.
  NAME(LIST);   # Circumvent prototypes.
  NAME; # Makes current @_ visible to called subroutine.

 That isn't particularly relevant to the effect of leaving parentheses off
 method calls.  You don't use  with methods (unless you're being
 perverse).  Methods ignore prototypes.  And @_ is not passed through when
 parens are left out on method calls.

 In fact, I can't find anything in perldoc about parens being optional on
 method calls.  All the examples I find leave in even empty ones.  Of
 course you can leave them out.  Parenthesis-less calls are documented in
 the Camel (Method Invocation) but not given any special attention.
 I guess that got added in the third edition when there started to be
 divergence between the Camel and perldoc.


Actually, it's quite relevant. The important thing to remember is that
Perl has no inherent concept of objects or methods. Objects are just
subroutines that call bless() on themselves. The rules for passing
arguments to all subroutines apply to objects. The perlobj perldoc
also information on the indirect object syntax and how it applies,
in often quirky ways, specifically to method invocations.

HTH,

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!


Re: two questions on my perl exam.

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 08:49, birdinforest [EMAIL PROTECTED] wrote:
 I am a student and enrolled Unix programing this semester.
 There are two questions relating to perl  I can not work out in the
 last exam ( Actually I have write out my code, however the exam system
 marked it as wrong). Please help me to point out the fault. Thanks.

 QUESTION 1
 Write a perl script called perl_has_number.pl which takes one
 argument
 which is any string. The script should output true if the string
 contains any digits, and false otherwise.

 MY CODE:
 #!/usr/bin/perl -w
 if ($ARGV[0]=~ /\d+/)
 {
  print true\n;
 }
 else
 {
  print false\n;
 }
snip

Things an automated system may find wrong:
1. the spec says to print true or false and you are printing
true\n or false\n
2. the spec says that the script should receive only one argument, but
your script will process the first argument if more than on is given
3. you are using \d which in modern (5.8 and above, possibly some 5.6
versions) Perl matches non-English number characters as well (e.g.
Mongolian five: \x{1815})
4. the interpreter may not be in /usr/bin

Additional things I find wrong with it:
1. you aren't using strict
2. you are using -w to turn on warnings instead of the warnings pragma or $^W=1
3. the bracing style is horrible

snip
 QUESTION 2
 Write a perl script called perl_digits.pl which takes any number of
 arguments. The script should return the number found by deleting all
 non-digits.

 Example:

 ./perl_digits.pl and1 pan2d 30rr and 4

 will return 12304

 MY CODE:
 #!/usr/bin/perl -w
 sub outcom
 {
  $outcome=;
foreach $a (@ARGV)
  {
if ($a=~/\d+/)
{
  $outcome=$outcome.$;
}
  }
return $outcome;
 }

 $digital=outcom(@ARGV);
 print $digital . \n;
snip

Many of the earlier statements hold true for this one as well, but the
error is probably on this case:

./perl_digits.pl and1 pan2d 3rr4 and 5

which should print out

12345

but instead prints out

1235

Things I find wrong:
1. everything I said before
2. you are passing @ARGV to the function, but aren't using @_ in the
function (instead you go back to @ARGV)
3. you misspelled outcome which is a very bad named for the function
to start with
4. you are using $a which has special meaning to the sort function
5. you are using $ instead of a capture (thereby slowing down all regexes)
6. you are using a loop where you should be using a core function
7. you are spelling for as foreach (those extra four letters don't do
anything and are being dropped in Perl 6)
8. you are spacing the assignments as if it were a shell script

-- 
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: Reading from multiple sockets.

2008-10-21 Thread Bob McConnell
From: Kammen van, Marco, Springer SBM NL
Hi All!

I'm pretty new to working with sockets in perl, looked around for days
for a proper solution for my IRC/DCC problem but couldn't find one.

The connection to the server uses a socket with a neverending while
loop: 
 
All works fine untill i want to make a DCC connection using another
socket: 
 
The first loop is being blocked? 
I only receive input/output from the second loop untill this one is
closed again, then the first loop continues
How can i get a continuous flow from both?
I've read some bits about IO::Socket::Select, and have also been
playing
with multiple threads, but i prefer the first option... Any help is
very
appreciated!

You need to use select, or put a timeout on can_read on both sockets.

See the following for how to use timeouts:
http://perlmonks.org?node_id=716603
 
http://perlmonks.org?node_id=602621
 
zentara
 
 Thanks for your input
 The whole select thingie is still a bit blurry to me...(I really need
a
 Book). 
 
 Which I forgot to mention is that both my sockets are connected to
 different ports... (54321  1024)
 And also, one socket is connected to a server, and the other socket is
 accepting from a client 
 Does this change anything??
 
 So Loop 1 is reading stuff that is happening on the server.
 And Loop 2 is reading stuff that a direct client is sending.
 
 Marco!


At this level, the only difference between a client and a server is the
opening sequence. After the sockets are open, they can be treated
exactly the same.

So once you have the sockets open, and as long as there are no pending
listens for additional connections, there are no differences in how you
read or write on multiple sockets. You need a single loop where select
will return when one or both sockets have received data, when it times
out or when a signal is received, whichever comes first. The timeout can
be used to check for other events, like keyboard input, before you loop
back into the select call.

Bob McConnell

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




Re: two questions on my perl exam.

2008-10-21 Thread Rob Dixon
birdinforest wrote:

 I am a student and enrolled Unix programing this semester.
 There are two questions relating to perl  I can not work out in the
 last exam ( Actually I have write out my code, however the exam system
 marked it as wrong). Please help me to point out the fault. Thanks.
 
 QUESTION 1
 Write a perl script called perl_has_number.pl which takes one
 argument
 which is any string. The script should output true if the string
 contains any digits, and false otherwise.
 
 MY CODE:
 #!/usr/bin/perl -w
 if ($ARGV[0]=~ /\d+/)
 {
   print true\n;
 }
 else
 {
   print false\n;
 }
 
 QUESTION 2
 Write a perl script called perl_digits.pl which takes any number of
 arguments. The script should return the number found by deleting all
 non-digits.

Any or all of the following, depending on the examination criteria:

- You should

use strict;
use warnings;

  instead of using the command line -w switch.

- You don't check the number of arguments provided. If there are none at all
  then your code will throw the warning Use of uninitialized value in pattern
  match and print false.

- You needlessly check for multiple digits, when only one is necessary to fulfil
  the condition

Here is what I would hav written

  use strict;
  use warnings;

  die Exactly one argument expected unless @ARGV == 1;

  my $arg = shift;

  print $arg =~ /[0-9]/ ? true\n : false\n;


 Example:
 
 ./perl_digits.pl and1 pan2d 30rr and 4
 
 will return 12304
 
 MY CODE:
 #!/usr/bin/perl -w
 sub outcom
 {
   $outcome=;
 foreach $a (@ARGV)
   {
 if ($a=~/\d+/)
 {
   $outcome=$outcome.$;
 }
   }
 return $outcome;
 }
 
 $digital=outcom(@ARGV);
 print $digital . \n;
 
 In my view, both code could give out right results. Could you help me
 to find the fault?

I would guess that that is just far too complex, and shows that you haven't
fully grasped regular expressions yet. You should also know not to use $ $a.

Here is my program.

  use strict;
  use warnings;

  print join('', @ARGV =~ /[0-9]/g), \n;

Rob



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




Re: last element during foreach loop

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 09:39, six24hourdays [EMAIL PROTECTED] wrote:
 Hello,

 I would like to know how to test for the last element of a list during
 a foreach loop, e.g.

 foreach $element (@List) {
if (this is the last element) {
do something
}
 }

 What would I use for the IF condition?
snip

This is an odd desire; are you sure you really need to do this?  Can
you tell us what you are trying to achieve?  Jeff's answer does what
you want, but most likely there is a better approach to be using for
your actual problem.

-- 
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: last element during foreach loop

2008-10-21 Thread Jeff Pang
2008/10/21 Chas. Owens [EMAIL PROTECTED]:


 This is an odd desire; are you sure you really need to do this?  Can
 you tell us what you are trying to achieve?  Jeff's answer does what
 you want, but most likely there is a better approach to be using for
 your actual problem.


Agree.
He probably want the last element from the array, then try,
my $last = $list[-1];


-- 
my @name = glob {JeffP}{a,e}{ng}
http://home.arcor.de/pangj/

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




Re: two questions on my perl exam.

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 10:43, Rob Dixon [EMAIL PROTECTED] wrote:
snip
snip
  use strict;
  use warnings;

  print join('', @ARGV =~ /[0-9]/g), \n;
snip

The perl interpreter in my brain throws a syntax error on the line 4
character 15.

Since you have put your code out there here is mine:

#!/usr/bin/perl

use strict;
use warnings;

(my $digits = join , @ARGV) =~ s/[^0-9]//g;
print $digits\n;


-- 
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: last element during foreach loop

2008-10-21 Thread Mr. Shawn H. Corey
On Tue, 2008-10-21 at 22:47 +0800, Jeff Pang wrote:
 2008/10/21 Chas. Owens [EMAIL PROTECTED]:
 
 
  This is an odd desire; are you sure you really need to do this?  Can
  you tell us what you are trying to achieve?  Jeff's answer does what
  you want, but most likely there is a better approach to be using for
  your actual problem.
 
 
 Agree.
 He probably want the last element from the array, then try,
 my $last = $list[-1];

if( @list ){
  my $last_item = pop @list;
  for my $item ( @list ){
# do stuff for all items but last
  }
  # do stuff for $last_item
}


-- 
Just my 0.0002 million dollars worth,
  Shawn

Linux is obsolete.
-- Andrew Tanenbaum


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




Re: expect error

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 09:40, Sandeep Kumar
[EMAIL PROTECTED] wrote:
 hi all,

 i am using expect module to connect to a remote server. i am getting the
 following error.

 Cannot sync with child: Interrupted system call at /perl_path/lib/Expect.pm
 line 134,

 somebody please let me know a solution for this.

 thanks,
 sandeep.

Can you duplicate the problem with a small script and post the small
script here so others can replicate your issue?  Often I find the
problem just by writing a smaller version of what I am trying to do.

-- 
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: last element during foreach loop

2008-10-21 Thread Mr. Shawn H. Corey
On Tue, 2008-10-21 at 10:55 -0400, Mr. Shawn H. Corey wrote:
 if( @list ){
   my $last_item = pop @list;
   for my $item ( @list ){
 # do stuff for all items but last
   }
   # do stuff for $last_item
 }
 

If you want @list restored after you're done processing:

if( @list ){
  my $last_item = pop @list;
  for my $item ( @list ){
# do stuff for all items but last
  }
  # do stuff for $last_item
  push @list, $last_item;
}


-- 
Just my 0.0002 million dollars worth,
  Shawn

Linux is obsolete.
-- Andrew Tanenbaum


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




Re: last element during foreach loop

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 10:56, David Stiff [EMAIL PROTECTED] wrote:
 Hi Chas,

 There probably is a better approach.

 I am going through a list of Subversion branch names, e.g.

 BRANCH_1
 BRANCH_1
 BRANCH_2
 BRANCH_2
 BRANCH_3
 BRANCH_3
 BRANCH_4
 BRANCH_4

 and checking to see if $branch eq $lastBranch. Then I do something.

 The problem is that the last time through the loop, e.g. BRANCH_4,
 nothing happens because the IF never evaluates to true.

If I understand you correctly then this code should work for you:

#!/usr/bin/perl

use strict;
use warnings;

#open my $fh, -|, svn somehing
#   or die could not run 'svn something': $!;

my $old_branch = '';
while (my $new_branch = DATA) { #use $fh here instead of DATA
chomp $new_branch;
if ($old_branch eq $new_branch) {
print doing stuff to $new_branch\n;
}
$old_branch = $new_branch;
}

__DATA__
BRANCH_1
BRANCH_1
BRANCH_2
BRANCH_2
BRANCH_3
BRANCH_3
BRANCH_4
BRANCH_4



-- 
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: Conserving memory

2008-10-21 Thread Mr. Shawn H. Corey
On Mon, 2008-10-20 at 23:29 -0700, kenTk wrote:
 If I populate @array
 Then
 @array=();
 Is the memory that was used for that array now freed?

Yes, providing no other variable contains those items.  For example:

my @a = qw( a b c );
my @b = @a;
@a = ();

Since @b contains everything is @a, its contents are not freed until the
contents of @b are replaced.

 
 Similarly
 If I populate the anonymous array  @{$arrayREFS[$index]}
 Then
 @{$arrayREFS[$index]}=();
 Is the memory that was used for that anonymous array now freed?

Yes but as above.


-- 
Just my 0.0002 million dollars worth,
  Shawn

Linux is obsolete.
-- Andrew Tanenbaum


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




Re: last element during foreach loop

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 11:06, Chas. Owens [EMAIL PROTECTED] wrote:
snip
 #open my $fh, -|, svn somehing
 #   or die could not run 'svn something': $!;
snip

Whoops, that is what I get for going too fast.  The multiple argument
version is faster because it doesn't need to spawn a sub shell:

open my $fh, -|, svn, arg1, arg2, etc



-- 
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: expect error

2008-10-21 Thread Sandeep Kumar

Chas. Owens wrote:

On Tue, Oct 21, 2008 at 09:40, Sandeep Kumar
[EMAIL PROTECTED] wrote:
  

hi all,

i am using expect module to connect to a remote server. i am getting the
following error.

Cannot sync with child: Interrupted system call at /perl_path/lib/Expect.pm
line 134,

somebody please let me know a solution for this.

thanks,
sandeep.



Can you duplicate the problem with a small script and post the small
script here so others can replicate your issue?  Often I find the
problem just by writing a smaller version of what I am trying to do.

  
i have been using expect regularly, and its the first time seeing this 
problem. i already did what you just suggested, and it worked fine.

the problem is not even a persistent one, and happening only sometimes.
below is the shortened version, which worked fine.
i would like to know, what interrupted system call means.


use strict;
use warnings;
use Expect;
use IO::Tty;

$host = 192.168.1.72;
$passwd= xyz;
$user = xyz;

$t= file.txt;
my $connect = Expect-spawn(scp $t [EMAIL PROTECTED]:/work/$user/); 
$connect-expect(30,\s) ||die junk1;

print $connect $passwd.\n;
$connect-expect(30,-re,'$\s');
$connect-soft_close();



Re: expect error

2008-10-21 Thread Juan Pablo Feria Gomez
 use strict;
 use warnings;
 use Expect;
 use IO::Tty;

 $host = 192.168.1.72;
 $passwd= xyz;
 $user = xyz;

 $t= file.txt;
 my $connect = Expect-spawn(scp $t [EMAIL PROTECTED]:/work/$user/);
 $connect-expect(30,\s) ||die junk1;



 print $connect $passwd.\n;

Do you want to send the password?, let expect wait for the prompt

$connect-expect(30, word:);
$connect-send($passwd\n);



 $connect-expect(30,-re,'$\s');
 $connect-soft_close();



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




Re: using algorithm-permute

2008-10-21 Thread Sharanbr
On Oct 19, 6:38 am, [EMAIL PROTECTED] (Sisyphus) wrote:
 On Oct 17, 3:04 am, [EMAIL PROTECTED] (Sharan Basappa) wrote:
 .
 .



  #!/usr/bin/perl
  use warnings;
  use Algorithm::Permute;
  my @array = (1..4);
  Algorithm::Permute::permute { print @array\n } @array;

 use warnings;
 use strict;
 use Algorithm::Permute;

 my @array = (1..9);
 my $p = new Algorithm::Permute([EMAIL PROTECTED]);

 # print out the first 20 permutations of @array,
 # assigning each permutation to @new, and
 # printing it out:
 for(1..20) {
   my @new = $p-next;
   print @new\n;

 }

 Cheers,
 Rob

Thanks, Rob

I have modified the code a little bit to suit my requirements. But
still the code does not seem to work i.e.
the final print of @x does not display any value. However, I change
the code foreach (@array) to for (1..)
the way you have coded, it works fine. My requirement is to put all
the permutations into a new array,
not just (1..20)

I have another basic doubt. After permute is called with @array
argument, does it now contain
new permutations or still (1..4).

#!/usr/bin/perl
use warnings;
use Algorithm::Permute;

my @array = (1..4);
my $p  = new Algorithm::Permute([EMAIL PROTECTED]);
foreach (@array)
{
  my @x = $p-next;
  print @x \n;
}

Regards


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




Re: two questions on my perl exam.

2008-10-21 Thread John W. Krahn

birdinforest wrote:

I am a student and enrolled Unix programing this semester.
There are two questions relating to perl  I can not work out in the
last exam ( Actually I have write out my code, however the exam system
marked it as wrong). Please help me to point out the fault. Thanks.

QUESTION 1
Write a perl script called perl_has_number.pl which takes one
argument
which is any string. The script should output true if the string
contains any digits, and false otherwise.

MY CODE:
#!/usr/bin/perl -w
if ($ARGV[0]=~ /\d+/)
{
  print true\n;
}
else
{
  print false\n;
}

QUESTION 2
Write a perl script called perl_digits.pl which takes any number of
arguments. The script should return the number found by deleting all
non-digits.

Example:

./perl_digits.pl and1 pan2d 30rr and 4

will return 12304

MY CODE:
#!/usr/bin/perl -w
sub outcom
{
  $outcome=;
foreach $a (@ARGV)
  {
if ($a=~/\d+/)
{
  $outcome=$outcome.$;
}
  }
return $outcome;
}

$digital=outcom(@ARGV);
print $digital . \n;

In my view, both code could give out right results. Could you help me
to find the fault?


You should find out from your examiners why they are wrong.  Perhaps 
they want you to use tr/// instead of m//:


#!/usr/bin/perl -l
print $ARGV[0] =~ tr/0-9// ? true : false;



#!/usr/bin/perl -l
($_ = @ARGV) =~ tr/0-9//cd;
print;




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

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




Re: last element during foreach loop

2008-10-21 Thread John W. Krahn

six24hourdays wrote:

Hello,


Hello,


I would like to know how to test for the last element of a list during
a foreach loop, e.g.

foreach $element (@List) {
if (this is the last element) {
do something
}
}

What would I use for the IF condition?


You can't because a list does not have a name.

perldoc -q What is the difference between a list and an array


You can however do it with an array by comparing references:

$ perl -le'
my @x = a .. z;
for my $elem ( @x ) {
print $elem if \$elem == \$x[-1];
}
'
z



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

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




Re: Conserving memory

2008-10-21 Thread Chas. Owens
On Tue, Oct 21, 2008 at 11:08, Mr. Shawn H. Corey [EMAIL PROTECTED] wrote:
 On Mon, 2008-10-20 at 23:29 -0700, kenTk wrote:
 If I populate @array
 Then
 @array=();
 Is the memory that was used for that array now freed?

 Yes, providing no other variable contains those items.  For example:
snip

Not necessarily, even if it is garbage collected perl might not return
the memory to the system.  The perl interpreter will most likely hold
onto that memory for its own future use.  The only protection against
this is to use as little memory as possible.

snip
 my @a = qw( a b c );
 my @b = @a;
 @a = ();

 Since @b contains everything is @a, its contents are not freed until the
 contents of @b are replaced.
snip

@b contains copies of what is in @a, not references; therefore
emptying @a has no effect on @b.  If you had said

@b = map { \$_} @a;

then your statement makes sense.


 Similarly
 If I populate the anonymous array  @{$arrayREFS[$index]}
 Then
 @{$arrayREFS[$index]}=();
 Is the memory that was used for that anonymous array now freed?

 Yes but as above.
snip

No, as above.

-- 
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/




Using a variable in a =~ match??

2008-10-21 Thread [EMAIL PROTECTED]
Hi all,

Serious noob here (one week into llama 5th ed.).

I'm trying to write a script to pull a specific file from a directory
and search that file for specific phrases. I am having a problem
searching for the file in the directory. If I type in the actual file
name (line 26) I can find the phrase file.zip (line 30). Obviously,
this will only work once. If I use a variable to search for a file I
get nothing. What is the proper format for line 26?I can't use
File::Find so please don't suggest it.  Thx.

 #!/usr/bin/perl
  2 use strict;
  3
  4 my $Log_Dir;
  5 my $file;
  6 my $Date;
  7 my $File_Name;
  8 my @array;
  9 my $file_name;
 10
 11 $Log_Dir = /var/log/apache/;
 12 opendir DH, $Log_Dir or die Cannot open $Log_Dir: $!;
 13 while ($file = readdir DH){
 14 push(@array, $file);
 15 }
 16
 17 closedir DH;
 18
 19 $Date = `date --date=yesterday +%Y%m%d-2400-0`;
 20 # The file name always starts with file111-11-23.abctyu_X but the
time stamp changes daily (predictable)
 21 # Example Filename: file111-11-23.abctyu_X.20081020-2400-0
 22 $File_Name = file111-11-23.abctyu_X.$Date;
 23 print $Date;
 24 foreach $file_name (@array){
 25 chomp;
 26 if ($file_name =~ /$File_Name/){
 27 open LOGFILE, $File_Name;
 28 while (LOGFILE){
 29 #Search log file for the word file.zip
 30 if(/file.zip/){
 31 print $_\n;
 32 }
 33 }
 34 }
 35 }


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




Re: Conserving memory

2008-10-21 Thread Dr.Ruud
Mr. Shawn H. Corey schreef:
 kenTk:

 If I populate @array
 Then
 @array=();
 Is the memory that was used for that array now freed?

 Yes, providing no other variable contains those items.  For example:

 my @a = qw( a b c );
 my @b = @a;
 @a = ();

 Since @b contains everything is @a, its contents are not freed until
 the contents of @b are replaced.


 Similarly
 If I populate the anonymous array  @{$arrayREFS[$index]}
 Then
 @{$arrayREFS[$index]}=();
 Is the memory that was used for that anonymous array now freed?

 Yes but as above.

This comes closer, but still isn't the whole story:

OS-memory allocated by perl (let's call it perl-memory) gets detached
from a container (like a variable) when the container itself is
destroyed, for example when it gets out of scope.
Detaching perl-memory from one container doesn't mean that the
perl-memory becomes perl-free (or rather perl-reusable), because it
can still be attached to other containers.

Only when perl-memory is no longer attached to any container, it is
possible to mark it as perl-free.
perl-free doesn't mean that it is returned to the OS. Normally it just
remains claimed by the perl process, but can be reused by other
containers.

It also depends on which memory allocation strategy your perl binary
uses, see `perl -V |grep alloc`.

Just assume that perl-the-binary, while running, never gives back memory
to the OS. By coding your Perl properly, you can facilitate it to reuse
some parts of all OS-memory that it ever claimed (so that it doesn't
need to claim even more OS-memory).

-- 
Affijn, Ruud

Gewoon is een tijger.


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




Re: last element during foreach loop

2008-10-21 Thread Dr.Ruud
John W. Krahn schreef:
 six24hourdays:

 I would like to know how to test for the last element of a list
 during a foreach loop [...]

 You can't because a list does not have a name. [...]
 You can however do it with an array by comparing references:
 
 $ perl -le'
 my @x = a .. z;
 for my $elem ( @x ) {
  print $elem if \$elem == \$x[-1];
 }
 '
 z

There are of course ways in Perl to make this fail. One way:

$ perl -MData::Alias -wle'
  my @x = a..z;
  alias $x[3] = $x[-1];
  $x[-1] = test;   
  for my $elem (@x) {
print $elem if \$elem == \$x[-1];
  }
'
test
test

-- 
Affijn, Ruud

Gewoon is een tijger.

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




Re: last element during foreach loop

2008-10-21 Thread six24hourdays
On Oct 21, 11:06 am, [EMAIL PROTECTED] (Chas. Owens) wrote:
 On Tue, Oct 21, 2008 at 10:56, David Stiff [EMAIL PROTECTED] wrote:
  Hi Chas,

  There probably is a better approach.

  I am going through a list of Subversion branch names, e.g.

  BRANCH_1
  BRANCH_1
  BRANCH_2
  BRANCH_2
  BRANCH_3
  BRANCH_3
  BRANCH_4
  BRANCH_4

  and checking to see if $branch eq $lastBranch. Then I do something.

  The problem is that the last time through the loop, e.g. BRANCH_4,
  nothing happens because the IF never evaluates to true.

 If I understand you correctly then this code should work for you:

 #!/usr/bin/perl

 use strict;
 use warnings;

 #open my $fh, -|, svn somehing
 #       or die could not run 'svn something': $!;

 my $old_branch = '';
 while (my $new_branch = DATA) { #use $fh here instead of DATA
         chomp $new_branch;
         if ($old_branch eq $new_branch) {
                 print doing stuff to $new_branch\n;
         }
         $old_branch = $new_branch;

 }

 __DATA__
 BRANCH_1
 BRANCH_1
 BRANCH_2
 BRANCH_2
 BRANCH_3
 BRANCH_3
 BRANCH_4
 BRANCH_4

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

This works if there are 2 of each branch (as in the example). If there
are more then the IF condition is true more times than I want. I only
want a condition to be true for the transition to a new branch. Then I
email the branch owner with the list.

Right now I am just pushing a dummy entry onto the end of the list so
my IF condition (if $branch ne $lastBranch) is true at the transition.

Dave


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




Changing from FTP to Secure copy

2008-10-21 Thread Wagner, David --- Senior Programmer Analyst --- WGO
I am in the middle of moving from Solaris to Linux environment. On the 
current Solaris box, I use Net::FTP for all the work that needs to be done. On 
Linux, the ftp is not available, due to security concerns. The Linux 
environment had Perl at 5.8.0 and so I got the okay to bring down AS Linux for 
5.8.8. That is has been loaded and I have loaded a few other modules that I use 
in the Solaris area. I was lookng at installing Net::SFTP, but it is failing as 
follows:

Failed during this command:
 ILYAZ/modules/Math-Pari-2.010800.tar.gz  : writemakefile NO 
'/opt/ActivePerl-5.8/bin/perl-static Makefile.PL INSTALLDIRS=site' returned 
status 2304
 TURNSTEP/Net-SSH-Perl-1.32.tar.gz: make_test NO one dependency not 
OK (Math::Pari)
 DBROBINS/Net-SFTP-0.10.tar.gz: make_test NO

Did as quick look on web and found some related info on 2304. So tried 
installing the Math::Pari, but won't complete and fails with the status 2304.

If one needs to do the secure copy from machines, what is the best path 
to pursue( at this point, there are no libssh2 libraries that I can find, but 
probably could get them)? Prefer a Perl solution verses wrapping other 
commands, but I am following the lead of my backup who has more knowledge in 
this area ( and may not be alot, but does have more).

Any thoughts or ideas on how to advance, would be greatly appreciated.

 Thanks.
 
Wags ;)
David R. Wagner
Senior Programmer Analyst
FedEx Freight
1.408.323.4225x2224 TEL
1.408.323.4449  FAX
http://fedex.com/us 



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




Re: last element during foreach loop

2008-10-21 Thread Rob Dixon
six24hourdays wrote:
 On Oct 21, 11:06 am, [EMAIL PROTECTED] (Chas. Owens) wrote:
 On Tue, Oct 21, 2008 at 10:56, David Stiff [EMAIL PROTECTED] wrote:

 There probably is a better approach.
 I am going through a list of Subversion branch names, e.g.
 BRANCH_1
 BRANCH_1
 BRANCH_2
 BRANCH_2
 BRANCH_3
 BRANCH_3
 BRANCH_4
 BRANCH_4
 and checking to see if $branch eq $lastBranch. Then I do something.
 The problem is that the last time through the loop, e.g. BRANCH_4,
 nothing happens because the IF never evaluates to true.
 If I understand you correctly then this code should work for you:

 #!/usr/bin/perl

 use strict;
 use warnings;

 #open my $fh, -|, svn somehing
 #   or die could not run 'svn something': $!;

 my $old_branch = '';
 while (my $new_branch = DATA) { #use $fh here instead of DATA
 chomp $new_branch;
 if ($old_branch eq $new_branch) {
 print doing stuff to $new_branch\n;
 }
 $old_branch = $new_branch;

 }

 __DATA__
 BRANCH_1
 BRANCH_1
 BRANCH_2
 BRANCH_2
 BRANCH_3
 BRANCH_3
 BRANCH_4
 BRANCH_4

 --
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the ability to read.
 
 This works if there are 2 of each branch (as in the example). If there
 are more then the IF condition is true more times than I want. I only
 want a condition to be true for the transition to a new branch. Then I
 email the branch owner with the list.
 
 Right now I am just pushing a dummy entry onto the end of the list so
 my IF condition (if $branch ne $lastBranch) is true at the transition.

I'm sure what Chas mean was this

  if ($old_branch ne $new_branch) {
print doing stuff to $new_branch\n;
  }

which works fine.

If you prefer you could use a hash, which is the classical way to determine
whether a data item has been seen already. Use a loop like this.

  my %branches;
  while (my $name = DATA) {
chomp $name;
next if $branches{$name}++;

print doing stuff to $name\n;
  }

HTH,

Rob

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




using Net::SSH::Expect within a thread fails

2008-10-21 Thread Andy Greenwood
Hello,

I'm having problems with the following code. It is supposed to get a
list of IP addresses from the IAD module and then create a few threads
to each log into those devices and make changes in the config. The
only device that is allowed to access the devices I'm configuring is
the 'manage.example.com'. From there, the threads will telnet to the
devices and make whatever changes need to be made.

---start---
#!/usr/local/bin/perl

# be safe, not stupid
use strict;
use warnings;

# include the code to generate the list of IPs
use IAD;

# check to ensure that we're threadable
use Config;
$Config{useithreads} or die('Recompile Perl with threads to run this program.');

# include the thread stuff
use threads;
use Thread::Queue;
use Thread::Semaphore;

# include the ssh stuff
use Net::SSH::Expect;
use Term::ReadPassword;

# get the username/password
my $exe_user = get_exe_user();
my $exe_pass = get_exe_pass();

# set up changes to be made
my $TheChange = get_input();

# set up param list
my @paramlist = ($TheChange, $exe_user, $exe_pass);

my $semaphore = Thread::Semaphore-new(3); # can only make three
connections to the management server at a time
my $DataQueue = Thread::Queue-new();
my $thread1 = threads-create(\threadsub, @paramlist);
my $thread2 = threads-create(\threadsub, @paramlist);
my $thread3 = threads-create(\threadsub, @paramlist);

my $iad = IAD-new();
$DataQueue-enqueue(@{$iad-iads()});# $iad-iads() returns a
reference to a list of IP addresses to manage

$DataQueue-enqueue(DIE, DIE, DIE);

$thread1-join();
$thread2-join();
$thread3-join();
exit;

sub threadsub {
#threads-detach();
my $code = shift;
my $exe_user = shift;
my $exe_pass = shift;
my $threadNumber = threads-tid();

# create the ssh object
my $ssh = Net::SSH::Expect-new (
host = 'manage.example.com',
user = $exe_user,
password = $exe_pass,
raw_pty = 1
);

# login to management server
my $login_output = $ssh-login();  this is line 65
if ($login_output !~ /home]$/) {
die Login has failed. Login output was $login_output;
}

while (my $DataElement = $DataQueue-dequeue()) {
last if ($DataElement eq DIE);
$semaphore-down();
print(Thread $threadNumber got $DataElement\n);
 #   $ssh-exec(telnet $DataElement, 10);
 #   print $ssh-exec($code);
 #   $ssh-exec(exit);
#sleep(1);
$semaphore-up();
}
return;# Thread $threadNumber is dying!\n;
}

sub get_input {
print Please enter commands below (end with a . on a line by
itself)\n;
my $retval = '';
while (defined(my $line = STDIN)) {
last if ($line eq .\n);
$retval .= $line;
}
return $retval;
}

sub get_exe_user {
print Please enter management username: ;
my $user = ;
chomp($user);
return $user;
}

sub get_exe_pass {
# get password from user
my $password = read_password('Password: ');
return $password;
}
---end---

However, When I try connecting, I get the following example output.
This is my first time writing threaded programs, so if I'm doing
anything wrong or inefficiently, please let me know since this program
will make a lot of connections once it's up and running. Thanks for
any help you can provide!

---start-output---
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied (publickey,password).
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied (publickey,password).
Permission denied (publickey,password).
thread failed to start: SSHConnectionAborted at ./test_threads.pl line 65
thread failed to start: SSHConnectionAborted at ./test_threads.pl line 65
thread failed to start: SSHConnectionAborted at ./test_threads.pl line 65
---end-output---

-- 
-- 
I'm nerdy in the extreme and whiter than sour cream

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




Re: using algorithm-permute

2008-10-21 Thread Rob Dixon
Sharanbr wrote:
 On Oct 19, 6:38 am, [EMAIL PROTECTED] (Sisyphus) wrote:
 On Oct 17, 3:04 am, [EMAIL PROTECTED] (Sharan Basappa) wrote:

 #!/usr/bin/perl
 use warnings;
 use Algorithm::Permute;
 my @array = (1..4);
 Algorithm::Permute::permute { print @array\n } @array;

 use warnings;
 use strict;
 use Algorithm::Permute;

 my @array = (1..9);
 my $p = new Algorithm::Permute([EMAIL PROTECTED]);

 # print out the first 20 permutations of @array,
 # assigning each permutation to @new, and
 # printing it out:
 for(1..20) {
   my @new = $p-next;
   print @new\n;

 }
 
 I have modified the code a little bit to suit my requirements. But
 still the code does not seem to work i.e.
 the final print of @x does not display any value. However, I change
 the code foreach (@array) to for (1..)
 the way you have coded, it works fine. My requirement is to put all
 the permutations into a new array,
 not just (1..20)
 
 I have another basic doubt. After permute is called with @array
 argument, does it now contain
 new permutations or still (1..4).
 
 #!/usr/bin/perl
 use warnings;
 use Algorithm::Permute;
 
 my @array = (1..4);
 my $p  = new Algorithm::Permute([EMAIL PROTECTED]);
 foreach (@array)
 {
   my @x = $p-next;
   print @x \n;
 }

You shouldn't pass a real array to the 'new' method as it destroys the array.
It's bad but there it is, and the documentation does show it being called with
an anonymous array.

I'm not sure what you mean by 'put all the permutations into a new array', as
each permutation is held in an array and I'm guessing that you don't know about
arrays of arrays?

The program I've written below stores each permutation as a single string with
spaces between the elements. If you really do want a array of arrays instead of
an array of strings then it's very obvious how to modify it to do that.

HTH,

Rob


use strict;
use warnings;

use Algorithm::Permute;

my $p = Algorithm::Permute-new([1 .. 4]);

my @permutations;

while (my @array = $p-next) {
  push @permutations, @array;
}

print $_\n foreach @permutations;

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




how do I install perl into thumbdrive

2008-10-21 Thread itshardtogetone
Hi,
I move around quite alot. So how do I install perl into my thumbdrive. I did a 
yahoo search and I found one but too technical ==  
http://www.perlmonks.org/?node=Portable+perl%3A+usb+thumbdrive

Thanks

Re: Using a variable in a =~ match??

2008-10-21 Thread Chris Charley
- Original Message - 
From: [EMAIL PROTECTED]

Hi all,

Serious noob here (one week into llama 5th ed.).

I'm trying to write a script to pull a specific file from a directory
and search that file for specific phrases. I am having a problem
searching for the file in the directory. If I type in the actual file
name (line 26) I can find the phrase file.zip (line 30). Obviously,
this will only work once. If I use a variable to search for a file I
get nothing. What is the proper format for line 26?I can't use
File::Find so please don't suggest it.  Thx.

#!/usr/bin/perl
 2 use strict;
 3
 4 my $Log_Dir;
 5 my $file;
 6 my $Date;
 7 my $File_Name;
 8 my @array;
 9 my $file_name;
10
11 $Log_Dir = /var/log/apache/;
12 opendir DH, $Log_Dir or die Cannot open $Log_Dir: $!;
13 while ($file = readdir DH){
14 push(@array, $file);
15 }
16
17 closedir DH;
18
19 $Date = `date --date=yesterday +%Y%m%d-2400-0`;
20 # The file name always starts with file111-11-23.abctyu_X but the
time stamp changes daily (predictable)
21 # Example Filename: file111-11-23.abctyu_X.20081020-2400-0
22 $File_Name = file111-11-23.abctyu_X.$Date;
23 print $Date;
24 foreach $file_name (@array){
25 chomp;


chomp is not needed here. Results of readdir do not end with a newline



26 if ($file_name =~ /$File_Name/){


Or:if ($file_name eq $File_Name)



27 open LOGFILE, $File_Name;
28 while (LOGFILE){
29 #Search log file for the word file.zip
30 if(/file.zip/){
31 print $_\n;
32 }
33 }
34 }
35 }



One thing that probably will cause a problem is 'readdir' returns filenames 
and directories without the path.


So, when you want to open your file, (line 27), unless the current directory 
of the program file is the same as the file you wish to open, it won't open 
it. You need to say like:


open LOGFILE, $Log_Dir$File_Name or die Unable to open $Log_Dir$File_Name 
$!;



From the documentation for readdir:


If you're planning to filetest the return values out of a readdir, you'd 
better prepend the directory in question. Otherwise, because we didn't chdir 
there, it would have been testing the wrong file.


Chris



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




Re: Using a variable in a =~ match??

2008-10-21 Thread John W. Krahn

[EMAIL PROTECTED] wrote:

Hi all,


Hello,


Serious noob here (one week into llama 5th ed.).

I'm trying to write a script to pull a specific file from a directory
and search that file for specific phrases. I am having a problem
searching for the file in the directory. If I type in the actual file
name (line 26) I can find the phrase file.zip (line 30). Obviously,
this will only work once. If I use a variable to search for a file I
get nothing. What is the proper format for line 26?I can't use
File::Find so please don't suggest it.  Thx.


Why can't you use File::Find?  And why would you want to use it for this 
application?




 #!/usr/bin/perl


use warnings;


  2 use strict;
  3
  4 my $Log_Dir;
  5 my $file;
  6 my $Date;
  7 my $File_Name;
  8 my @array;
  9 my $file_name;


Why are you declaring all your variables here?


 11 $Log_Dir = /var/log/apache/;
 12 opendir DH, $Log_Dir or die Cannot open $Log_Dir: $!;
 13 while ($file = readdir DH){
 14 push(@array, $file);
 15 }


Why the while loop?

   my @array = readdir DH;

Why is the array named array?


 17 closedir DH;
 18
 19 $Date = `date --date=yesterday +%Y%m%d-2400-0`;


Assuming today's date is 21 Oct. 2008, $Date will now contain the string 
20081020-2400-0\n


perldoc -q How do I find yesterday.s date


 20 # The file name always starts with file111-11-23.abctyu_X but the
time stamp changes daily (predictable)
 21 # Example Filename: file111-11-23.abctyu_X.20081020-2400-0


It looks like your file name does not have a \n at the end?


 22 $File_Name = file111-11-23.abctyu_X.$Date;
 23 print $Date;
 24 foreach $file_name (@array){
 25 chomp;


Why are you chomp()ing $_?


 26 if ($file_name =~ /$File_Name/){


Why use a regular expression?

  if ( $file_name eq $File_Name ) {


 27 open LOGFILE, $File_Name;


You should *always* verify that the file opened correctly.


 28 while (LOGFILE){
 29 #Search log file for the word file.zip
 30 if(/file.zip/){


The . character in a regular expression matches every character (except 
newline) so you have to escape it to match a literal . character:


  if ( /file\.zip/ ) {


 31 print $_\n;
 32 }
 33 }
 34 }
 35 }


You are reading all the file names from a directory to find a file that 
you already know the name of.  You probably want some like this instead:


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

my $log_dir = '/var/log/apache';
chomp( my $yesterday = `date --date=yesterday +%Y%m%d` );
my $file_name = $log_dir/file111-11-23.abctyu_X.${yesterday}-2400-0;

open my $LOGFILE, '', $file_name or die Cannot open '$file_name' $!;

while ( $LOGFILE ) {
#Search log file for the word file.zip
print if /file\.zip/;
}

__END__



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

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




Send Mail with attachment

2008-10-21 Thread leolim818
Hi All,

I'm using perl module MIME::Lite to sent out email with attachments,
may I know what Type should I define to attach any type of files,
for instance .jpg, .xls, .doc, .pdf  and etc without checking the
attached file type. Is there any global variable to define instead of
Type = 'application/zip', Type = 'image/gif', Type = application/
xls and etc?

$msg-attach (
   Type = 'what type should I define without checking the attached
file type',
   Path = '$path',
   Filename = '$filename',
   Disposition = 'attachment'
)

Please helps.

Thanks  Regards,
Leo.


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




Re: two questions on my perl exam.

2008-10-21 Thread birdinforest
Thanks for your suggestions and all of them make sense. However, since
I am a totally beginner never learned programing, it should take me
sometime to understand them clearly.  I will also try to get answer
from my lecturer, then post here that what I have learned from both
questions.

 Programing is an interesting subject. Thank you again.


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




XML::Simple question

2008-10-21 Thread Richard Lee

while trying to study the article on perlmonks.org,

http://perlmonks.org/?node_id=490846

regarding XML parsing, I need bit of clarfication.

how do I parse out

image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif;
width=145 height=190 / 



I tried $book-{image}-{src}... but doesn't work.. I need some 
understanding on how these information is stored.




parsing code

 use XML::Simple qw(:strict);

 my $library  = XMLin($filename,
   ForceArray = 1,
   KeyAttr= {},
 );

 foreach my $book (@{$library-{book}}) {
   print $book-{title}-[0], \n 


 }

XML file

library
   book
 titlePerl Best Practices/title
 authorDamian Conway/author
 isbn0596001738/isbn
 pages542/pages
 image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif;
width=145 height=190 /
   /book
   book
 titlePerl Cookbook, Second Edition/title
 authorTom Christiansen/author
 authorNathan Torkington/author
 isbn0596003137/isbn
 pages964/pages
 image src=http://www.oreilly.com/catalog/covers/perlckbk2.s.gi
+f
width=145 height=190 /
   /book
   book
 titleGuitar for Dummies/title
 authorMark Phillips/author
 authorJohn Chappell/author
 isbn076455106X/isbn
 pages392/pages
 image src=http://media.wiley.com/product_data/coverImage/6X/07
+645510/076455106X.jpg
width=100 height=125 /
   /book
 /library



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