RE: Reading from multiple sockets.

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

-Original Message-
From: Bob McConnell [mailto:[EMAIL PROTECTED] 

 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


Thanks Bob, that makes things a bit more clear.
So say that I have $socket1 and $socket2
How can I read from both using one loop then??

Marco!




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




Re: Send Mail with attachment

2008-10-22 Thread Jeff Pang
2008/10/22  [EMAIL PROTECTED]:
 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?

I don't think there is one.
If you don't specify the types distinctly, you break the rules (RFC 2822 etc).

-- 
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: Send Mail with attachment

2008-10-22 Thread Octavian Rasnita
From: [EMAIL PROTECTED]
 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?

You could use
Mail::Builder
to create the mail messages and and Email::Send to send them.

You should use something like:

use strict;
use warnings;
use Mail::Builder;
use Email::Send;

my $mail = Mail::Builder-new;
$mail-from('[EMAIL PROTECTED]');
$mail-to('[EMAIL PROTECTED]');
$mail-subject('Here is the subject');
$mail-plaintext('The plain text version');
$mail-htmltext('bThe html/b version');
$mail-attachment-add('file.pdf');
$mail-attachment-add('anotherfile.pdf');
$mail-image-add(image.jpg'); #An image you want to appear in the html file

my $mailer = Email::Send-new({mailer = 'SMTP'});
$mailer-mailer_args([Host = 'smtp.yourhost.com']);
$mailer-send($mail-stringify);

This module also encodes the headers as UTF-8, so they will be set correctly if 
the headers use special chars from other languages than english.
HTH.

Octavian


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




Re: XML::Simple question

2008-10-22 Thread Dr.Ruud
Richard Lee schreef:

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

   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 [...]
 /book
   /library

You can easily convert the above code to a small-but-complete Perl
script, for example with the XML-data in the __DATA__ section, and use
Data::Dumper at several stages to show you the data structures.

-- 
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-22 Thread Vijaya
 
Hi Dave,
 
I think there are 3 possible ways to check the last element in your IF
condition inside Foreach loop.
 
1. $list[-1] eq $element
2. $list[$#list] eq $element
3. $list[scalar(@list)-1] eq $element
 
  See the example code and replace the if with other set of conditions given
above
  
@list = qw/a d b y I s n p/;
Foreach $element (@list) {
If ($list[-1] eq $element) {
Print last element is $element;
 }
 } 
 
Output : last element is p 
 
Regards,
Vijaya 
 
 
 IMSTP9.gif

Re: last element during foreach loop

2008-10-22 Thread Dr.Ruud
Vijaya schreef:

 I think there are 3 possible ways to check the last element in your IF
 condition inside Foreach loop.

 1. $list[-1] eq $element
 2. $list[$#list] eq $element
 3. $list[scalar(@list)-1] eq $element

Those all will not work (reliably), because you compare the value, and
the values of an array are not necessarily unique.

-- 
Affijn, Ruud

Gewoon is een tijger.


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




RE: :Simple question

2008-10-22 Thread Stewart Anderson

 -Original Message-
 From: Richard Lee [mailto:[EMAIL PROTECTED]
 Sent: 22 October 2008 06:00
 To: Perl Beginners
 Subject: XML::Simple question
 
 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
 
 
 
 --
I gave up with XML::Simple  it did not seem to  work  (well I could not
getit  to work)  in the way that  the documentation suggested.  

I  switched to XML::Smart and it works perfectly.  I use XML Spy to dig
out the XPath etc and work with that.

Stu




Information in this email including any attachments may be privileged, 
confidential and is intended exclusively for the addressee. The views expressed 
may not be official policy, but the personal views of the originator. If you 
have received it in error, please notify the sender by return e-mail and delete 
it from your system. You should not reproduce, distribute, store, retransmit, 
use or disclose its contents to anyone. Please note we reserve the right to 
monitor all e-mail communication through our internal and external networks. 
SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and 
are used under licence. British Sky Broadcasting Limited (Registration No. 
2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home 
Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited 
(Registration No. 2340150) are direct or indirect subsidiaries of British Sky 
Broadcasting Group plc (Registration No. 2247735). All of the companies 
mentioned in this paragraph are incorporated in England and Wales and share the 
same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

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




RE: Send Mail with attachment

2008-10-22 Thread Stewart Anderson
 
 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.
 
I think  that  with MIME::Lite you can  just set the  type as BINARY or
TEXT.  I experimented with  lots of   types  for  zip files and xls/csv
files and whilst  is  may break the RFC  it  does not seem to break how
the item is attached  eg I send  xls files as text and  it made no
difference  when  the mail arrived in outlook with the attachement.

That's  probably heresy though :)

Stu



Information in this email including any attachments may be privileged, 
confidential and is intended exclusively for the addressee. The views expressed 
may not be official policy, but the personal views of the originator. If you 
have received it in error, please notify the sender by return e-mail and delete 
it from your system. You should not reproduce, distribute, store, retransmit, 
use or disclose its contents to anyone. Please note we reserve the right to 
monitor all e-mail communication through our internal and external networks. 
SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and 
are used under licence. British Sky Broadcasting Limited (Registration No. 
2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home 
Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited 
(Registration No. 2340150) are direct or indirect subsidiaries of British Sky 
Broadcasting Group plc (Registration No. 2247735). All of the companies 
mentioned in this paragraph are incorporated in England and Wales and share the 
same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

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




Re: :Simple question

2008-10-22 Thread Jack Butchie
Wouldn't it be more productive if what the question was was actually 
inserted into the subject area. instead of a generic term. Subscribers could 
instantly see if it's something they might be interested in instead of 
opening the email to see what Simple Questions actually is.



- Original Message - 
From: Stewart Anderson [EMAIL PROTECTED]
To: Richard Lee [EMAIL PROTECTED]; Perl Beginners 
beginners@perl.org

Cc: Stewart Anderson [EMAIL PROTECTED]
Sent: Wednesday, October 22, 2008 3:16 AM
Subject: RE: :Simple question




-Original Message-
From: Richard Lee [mailto:[EMAIL PROTECTED]
Sent: 22 October 2008 06:00
To: Perl Beginners
Subject: XML::Simple question

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



--

I gave up with XML::Simple  it did not seem to  work  (well I could not
getit  to work)  in the way that  the documentation suggested.

I  switched to XML::Smart and it works perfectly.  I use XML Spy to dig
out the XPath etc and work with that.

Stu




Information in this email including any attachments may be privileged, 
confidential and is intended exclusively for the addressee. The views 
expressed may not be official policy, but the personal views of the 
originator. If you have received it in error, please notify the sender by 
return e-mail and delete it from your system. You should not reproduce, 
distribute, store, retransmit, use or disclose its contents to anyone. 
Please note we reserve the right to monitor all e-mail communication through 
our internal and external networks. SKY and the SKY marks are trade marks of 
British Sky Broadcasting Group plc and are used under licence. British Sky 
Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited 
(Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 
2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are 
direct or indirect subsidiaries of British Sky Broadcasting Group plc 
(Registration No. 2247735). All of the companies mentioned in this paragraph 
are incorporated in England and Wales and share the same registered office 
at Grant Way, Isleworth, Middlesex TW7 5QD.


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

2008-10-22 Thread Peter Scott
On Tue, 21 Oct 2008 12:53:53 +0200, Kammen van, Marco, Springer SBM NL
wrote:
 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.

If you'd like to do this without grubbing around at the socket level,
check out POE on CPAN and the POE::Component::IRC modules.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


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




RE: Reading from multiple sockets.

2008-10-22 Thread Kammen van, Marco, Springer SBM NL
 
From: Peter Scott [mailto:[EMAIL PROTECTED] 
Subject: Re: Reading from multiple sockets.

On Tue, 21 Oct 2008 12:53:53 +0200, Kammen van, Marco, Springer SBM NL
wrote:
 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.

If you'd like to do this without grubbing around at the socket level,
check out POE on CPAN and the POE::Component::IRC modules.

-- 
Peter Scott

Thanks Peter..

But I do want to fiddle around with sockets and stuff, complicated or
not... Everyone needs to learn sometime right? :-D
And I want to keep the number of additional modules as minimal as
possible...
I know this means more coding, but that doesn't matter... Its all good
learning experience...

Marco. 

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




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

2008-10-22 Thread Peter Scott
On Tue, 21 Oct 2008 10:38:27 -0400, Jay Savage wrote:
 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:
 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.

Objects are not subroutines, they are references to blessed storage.  The
question was not about indirect method calls but using the arrow syntax
(see subject).  Perl may not have objects as tightly integrated into the
language as other O-O languages, but it does have some of the principles
baked in, in particular method calls.  If it had no inherent concept of
methods then 

$dog-eat( $kibble )

would be translated exactly to

Dog::eat( $dog, $kibble )

But this is not the case if Dog inherits eat().  Perl knows about
inheritance hierarchies, which counts as some form of concept of methods.

The quoted section of perlsub did not address the original question of
where it is documented that you can leave the parentheses off argumentless
method calls.  It's not the same as NAME LIST because method dispatch is
handled quite differently from subroutine calls.  See about halfway
through perly.y.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


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




Re: how do I install perl into thumbdrive

2008-10-22 Thread Peter Scott
On Wed, 22 Oct 2008 07:12:52 +0800, itshardtogetone wrote:
 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

Look for Perl on a stick:

http://use.perl.org/article.pl?sid=08%2F07%2F05%2F1338257

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


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




Re: XML::Simple question

2008-10-22 Thread Rob Dixon
Richard Lee wrote:
 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

I agree with Stewart that XML::Simple is far from simple in practice. For the
selection of options for XMLin you have used, you can access the image data like
this:

foreach my $book (@{$library-{book}}) {
  my $title = $book-{title}[0];
  my $image = $book-{image}[0];
  print $title\n;
  print   $image-{src}\n;
  print   $image-{width}\n;
  print   $image-{height}\n;
}

but the structure of the data will change depending on what options are set, and
in general it is very difficult to use XML::Simple without also using
Data::Dumper to inspect the data structure that has actually been generated.

HTH,

Rob

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




Re: :Simple question

2008-10-22 Thread Rob Dixon
Jack Butchie wrote:

 Wouldn't it be more productive if what the question was was actually 
 inserted into the subject area. instead of a generic term. Subscribers could 
 instantly see if it's something they might be interested in instead of 
 opening the email to see what Simple Questions actually is.

The subject line is almost certainly the result of a bug in someone's email
client, which discarded everything up to the last colon. It used to read

  XML::Simple question

Rob

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




Re: Reading from multiple sockets.

2008-10-22 Thread John W. Krahn

Kammen van, Marco, Springer SBM NL wrote:
 
From: Peter Scott [mailto:[EMAIL PROTECTED] 
Subject: Re: Reading from multiple sockets.



On Tue, 21 Oct 2008 12:53:53 +0200, Kammen van, Marco, Springer SBM NL
wrote:
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.



If you'd like to do this without grubbing around at the socket level,
check out POE on CPAN and the POE::Component::IRC modules.



--
Peter Scott


Thanks Peter..

But I do want to fiddle around with sockets and stuff, complicated or
not... Everyone needs to learn sometime right? :-D
And I want to keep the number of additional modules as minimal as
possible...
I know this means more coding, but that doesn't matter... Its all good
learning experience...


Then you should probably get the book _UNIX Network Programming_ and/or 
_Network Programming with Perl_.




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/




Conditional replace

2008-10-22 Thread Brian
Hi

ARGV0 will = AB7Z001
ARGV1 will = AB7Z002
ARGV2 will = 01/01/1900

I would like to read a file, locate AB7Z001 (but not AB7Z0011,  so a space at 
position 8 in string )
Upon location of value in argv0 replace it with argv1.
Then, at the first instance of a date replace it with argv2.
Then, at the next instance of a date, replace it with today's date.
There will only be one instance of AB7Z001 in the file, but/so, is there a way 
to stop checking and go on to output after this one event

So that,

AB7Z001 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

will be

AB7Z001 blahblahblah 01/01/1900 moreblahblah evenmoreblahblah 22/10/2008
AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

open (IN, +dummy.txt);
@file = IN;
seek IN,0,0;
foreach $file (@file){
$file =~ s/./g; --- ??

print IN $file;
}
close IN;

Thanks
Brian


  

Re: Conditional replace

2008-10-22 Thread Rob Coops
On Wed, Oct 22, 2008 at 3:38 PM, Brian [EMAIL PROTECTED] wrote:

 Hi

 ARGV0 will = AB7Z001
 ARGV1 will = AB7Z002
 ARGV2 will = 01/01/1900

 I would like to read a file, locate AB7Z001 (but not AB7Z0011,  so a space
 at position 8 in string )
 Upon location of value in argv0 replace it with argv1.
 Then, at the first instance of a date replace it with argv2.
 Then, at the next instance of a date, replace it with today's date.
 There will only be one instance of AB7Z001 in the file, but/so, is there a
 way to stop checking and go on to output after this one event

 So that,

 AB7Z001 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
 AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

 will be

 AB7Z001 blahblahblah 01/01/1900 moreblahblah evenmoreblahblah 22/10/2008
 AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

 open (IN, +dummy.txt);
 @file = IN;
 seek IN,0,0;
 foreach $file (@file){
 $file =~ s/./g; --- ??

 print IN $file;
 }
 close IN;

 Thanks
 Brian





You are reading the the whole file into an array in one big gulp... *(@file
= IN;)* are you sure you will not at some point have a file that is a few
Gigabytes in size causing the whole thing to roll over and die?

Anyway, I'll stay with the big greedy dump it all in an array for now.

open (IN, +dummy.txt);
@file = IN;
close IN;
for ( my $i = 0; $i  scalar @file; $i++ ) { # loop trought the file line by
line
 if ( $file =~ m/$ARGV0/g ) { # look for argument $ARGV0
  $file =~ s/$ARGV0(\b.*)/$ARGV1$1/g; # replace argument $ARGV0 with $ARGV1
  $file =~
s/(.*?)(\d{2}/\d{2}/\d{2})(.*?)(\d{2}/\d{2}/\d{2})(.*)/$1$ARGV2$3'todays
date'$4/g; # replace the first date with $ARGV2 and the second date with
'todays date'
  $file[$i] = $file; # replace this line in the array
  $i = scalar @file; # make $i the same as the size of the file thus ending
the loop
 }
}
# open the existing file and overwrite with the updated array, or write out
to a new file.
That should do the trick I guess.
What you where trying to do with *seek IN,0,0;* does not work unless you are
happy not to see the rest of the file after $ARGV0 has been found, because
you wanted to stop looping through the file then. If you didn't want to stop
once the condition has been met that might just do the trick (never tried it
my self so I cannot confirm)


Re: Conditional replace

2008-10-22 Thread Brian






From: Rob Coops [EMAIL PROTECTED]
To: beginners@perl.org
Sent: Wednesday, October 22, 2008 3:24:12 PM
Subject: Re: Conditional replace

On Wed, Oct 22, 2008 at 3:38 PM, Brian [EMAIL PROTECTED] wrote:

 Hi

 ARGV0 will = AB7Z001
 ARGV1 will = AB7Z002
 ARGV2 will = 01/01/1900

 I would like to read a file, locate AB7Z001 (but not AB7Z0011,  so a space
 at position 8 in string )
 Upon location of value in argv0 replace it with argv1.
 Then, at the first instance of a date replace it with argv2.
 Then, at the next instance of a date, replace it with today's date.
 There will only be one instance of AB7Z001 in the file, but/so, is there a
 way to stop checking and go on to output after this one event

 So that,

 AB7Z001 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
 AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

 will be

 AB7Z001 blahblahblah 01/01/1900 moreblahblah evenmoreblahblah 22/10/2008
 AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

 open (IN, +dummy.txt);
 @file = IN;
 seek IN,0,0;
 foreach $file (@file){
 $file =~ s/./g; --- ??

 print IN $file;
 }
 close IN;

 Thanks
 Brian





You are reading the the whole file into an array in one big gulp... *(@file
= IN;)* are you sure you will not at some point have a file that is a few
Gigabytes in size causing the whole thing to roll over and die?

At this point, I'm only looking at using a small file, but a good point and one 
that I will bear in mind for the future.

Anyway, I'll stay with the big greedy dump it all in an array for now.

snip

That should do the trick I guess.
What you where trying to do with *seek IN,0,0;* does not work unless you are
happy not to see the rest of the file after $ARGV0 has been found, because
you wanted to stop looping through the file then. If you didn't want to stop
once the condition has been met that might just do the trick (never tried it
my self so I cannot confirm)

Understood, I'll play with the 2 options.

Thank you, very much appreciated.
regards
Brian


  

Re: using algorithm-permute

2008-10-22 Thread Sharan Basappa
On Wed, Oct 22, 2008 at 4:32 AM, Rob Dixon [EMAIL PROTECTED] wrote:
 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;

Thanks, Rob. The method works and also fits in the overall code I am writing.
The main reason for confusion from my side is my lack of understanding
of how different methods
of algo permute work (e.g. I was not aware that an anonymous array was
created. I was thinking that
the original array is modified to create permutations. Lack of good
doc also contributed to this confusion.

Regards

-- 
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-22 Thread [EMAIL PROTECTED]
On Oct 21, 10:57 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:
 [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

Hi John,

I picked up the Perl book last week so all of your whys will get one
answer. Because that the way I thought it would work!

perldoc -q How do I find yesterday.s date : This is cool I didn't
realize I could do this (FAQ keyword). Of course I haven't got into
man perldoc to much either.

CHOMP: I still trying to figure out CHOMP. The way I understand it it
is used remove new lines \n.

File Open Verification: I get that. By using something like or die
Cannot open $blahblah: $!;

Yep. I missed the escape character in the regex /file\.zip/

Yes. I will know what the files names are in advance.. I guess I was
caught up learning (I wanted to open a directory and read the
contents) and didn't think simplicity.

I will try your code out. It looks so much easier...

P.S. Thanks Chris for your initial comments. It's all helpful to me at
this point.

M


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




Re: XML::Simple question

2008-10-22 Thread Richard Lee

Rob Dixon wrote:

Richard Lee wrote:
  

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



I agree with Stewart that XML::Simple is far from simple in practice. For the
selection of options for XMLin you have used, you can access the image data like
this:

foreach my $book (@{$library-{book}}) {
  my $title = $book-{title}[0];
  my $image = $book-{image}[0];
  print $title\n;
  print   $image-{src}\n;
  print   $image-{width}\n;
  print   $image-{height}\n;
}

but the structure of the data will change depending on what options are set, and
in general it is very difficult to use XML::Simple without also using
Data::Dumper to inspect the data structure that has actually been generated.

HTH,

Rob
  

thanks all!

Will try few others now.

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




Re: Conditional replace

2008-10-22 Thread brian54321uk

Rob Coops wrote:
snip



open (IN, +dummy.txt);
@file = IN;
close IN;
for ( my $i = 0; $i  scalar @file; $i++ ) { # loop trought the file line by 
line
 if ( $file =~ m/$ARGV0/g ) { # look for argument $ARGV0
  $file =~ s/$ARGV0(\b.*)/$ARGV1$1/g; # replace argument $ARGV0 with $ARGV1
  $file =~ 
s/(.*?)(\d{2}/\d{2}/\d{2})(.*?)(\d{2}/\d{2}/\d{2})(.*)/$1$ARGV2$3'todays 
date'$4/g; # replace ...

Error message for the above line =
Unmatched ( in regex; marked by -- HERE in m/(.*?)( -- HERE \d{2}/ at 
check.pl



  $file[$i] = $file; # replace this line in the array
  $i = scalar @file; # make $i the same as the size of the file thus ending
the loop
 }
}


Just spotted, when I said today's date I meant the computers date at 
time of running (if I run it tomorrow, today's date will be 23/10/2008)


Confused? I am ;-)

regards
Brian

--
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-22 Thread John W. Krahn

[EMAIL PROTECTED] wrote:


Hi John,

I picked up the Perl book last week so all of your whys will get one
answer. Because that the way I thought it would work!


There are a *lot* of Perl books out there.  Which one in particular did 
you pick up?




perldoc -q How do I find yesterday.s date : This is cool I didn't
realize I could do this (FAQ keyword). Of course I haven't got into
man perldoc to much either.

CHOMP: I still trying to figure out CHOMP. The way I understand it it
is used remove new lines \n.


chomp() removes whatever the contents of $/ (Input Record Separator) is, 
if $/ contains a non-zero length string.  Normally $/ contains the 
string \n but it can be set to any string including '' (paragraph mode.)




File Open Verification: I get that. By using something like or die
Cannot open $blahblah: $!;


Yes, you had done that with opendir() and it is good practice to verify 
all perl functions that work with the file system or operating system.




Yep. I missed the escape character in the regex /file\.zip/

Yes. I will know what the files names are in advance.. I guess I was
caught up learning (I wanted to open a directory and read the
contents) and didn't think simplicity.

I will try your code out. It looks so much easier...

P.S. Thanks Chris for your initial comments. It's all helpful to me at
this point.




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/




form generator Perl... (The Holy Grail)

2008-10-22 Thread Pat Rice
Hi all
I'm wondering whats the best tool (preferably) open source, To use to
generate HTML forms in Perl that I can link in to a database after,
with optimistically code that is generated in a  readable way.

The holy grail would be:
- type out the form names for each value.
- press the button or run the script and out comes Perl and/or HTML
- give me place holders for a database connection

Ideally I can see this web app being session based, so any ideas
greatly appreciated.

I suppose I don't ask for much !

Thanks in advance
Pat

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




Re: Conditional replace

2008-10-22 Thread John W. Krahn

Brian wrote:

Hi


Hello,


ARGV0 will = AB7Z001
ARGV1 will = AB7Z002
ARGV2 will = 01/01/1900

I would like to read a file, locate AB7Z001 (but not AB7Z0011,  so a space at 
position 8 in string )
Upon location of value in argv0 replace it with argv1.
Then, at the first instance of a date replace it with argv2.
Then, at the next instance of a date, replace it with today's date.
There will only be one instance of AB7Z001 in the file, but/so, is there a way to stop 
checking and go on to output after this one event

So that,

AB7Z001 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

will be

AB7Z001 blahblahblah 01/01/1900 moreblahblah evenmoreblahblah 22/10/2008
AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999

open (IN, +dummy.txt);
@file = IN;
seek IN,0,0;
foreach $file (@file){
$file =~ s/./g; --- ??

print IN $file;
}
close IN;


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

@ARGV == 3 or die usage: $0 search for replace with date\n;
my ( $search, $replace, $date ) = @ARGV;

my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%02d/%02d/%04d', $day, $mon + 1, $year + 1900;

( $^I, @ARGV ) = ( '', 'dummy.txt' );

while (  ) {
if ( ?^$search ? ) {
s/^$search/$replace/;
s!\d\d/\d\d/\d{4}!$date!
and s!($date.*?)\d\d/\d\d/\d{4}!$1$today!;
}
print;
}

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




Re: form generator Perl... (The Holy Grail)

2008-10-22 Thread Wiggins d'Anconia
Pat Rice wrote:
 Hi all
 I'm wondering whats the best tool (preferably) open source, To use to
 generate HTML forms in Perl that I can link in to a database after,
 with optimistically code that is generated in a  readable way.
 
 The holy grail would be:
 - type out the form names for each value.
 - press the button or run the script and out comes Perl and/or HTML
 - give me place holders for a database connection
 
 Ideally I can see this web app being session based, so any ideas
 greatly appreciated.
 
 I suppose I don't ask for much !
 
 Thanks in advance
 Pat
 

You may want to look at Rose::HTML::Objects and Rose::DB::Object... I've
used the latter, not the former.

http://search.cpan.org/~jsiracusa/

HTH,

http://danconia.org

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




Re: Conditional replace

2008-10-22 Thread Brian

John W. Krahn wrote:

Brian wrote:

Hi


Hello,


snip



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

@ARGV == 3 or die usage: $0 search for replace with date\n;
my ( $search, $replace, $date ) = @ARGV;

my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%02d/%02d/%04d', $day, $mon + 1, $year + 1900;

( $^I, @ARGV ) = ( '', 'dummy.txt' );

while (  ) {
if ( ?^$search ? ) {
s/^$search/$replace/;
s!\d\d/\d\d/\d{4}!$date!
and s!($date.*?)\d\d/\d\d/\d{4}!$1$today!;
}
print;
}

__END__




John


Oops, forgot to reply to the group.
Thanks John, however I get error message:-
Can't do inplace edit without backup at check2.pl line 13
All I can find so far with google is that this is an issue under 
Windows, and can be resolved by specifing and extension after the -i 
switch, but as there is no -i switch in this script I will keep googling.

regards
Brian

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




Re: Conditional replace

2008-10-22 Thread Brian

Brian wrote:

John W. Krahn wrote:

Brian wrote:

Hi


Hello,


snip



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

@ARGV == 3 or die usage: $0 search for replace with date\n;
my ( $search, $replace, $date ) = @ARGV;

my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%02d/%02d/%04d', $day, $mon + 1, $year + 1900;

( $^I, @ARGV ) = ( '', 'dummy.txt' );



Oops 2!
I changed  ( $^I, @ARGV ) = ( '', 'dummy.txt' );
to ( $^I, @ARGV ) = ( '.bak', 'dummy.txt' );

that got rid of the error, created a copy of dummy.txt to dummy.txt.bak
but failed to make the desired changes to dummy.txt

1 question though, while I understand why $year is + 1900, why is $mon +1 ?

Brian

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




Re: Conditional replace

2008-10-22 Thread Brian

Brian wrote:

Brian wrote:

John W. Krahn wrote:

Brian wrote:

Hi


Hello,


snip



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

@ARGV == 3 or die usage: $0 search for replace with date\n;
my ( $search, $replace, $date ) = @ARGV;

my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%02d/%02d/%04d', $day, $mon + 1, $year + 1900;

( $^I, @ARGV ) = ( '', 'dummy.txt' );



Oops 2!
I changed  ( $^I, @ARGV ) = ( '', 'dummy.txt' );
to ( $^I, @ARGV ) = ( '.bak', 'dummy.txt' );

that got rid of the error, created a copy of dummy.txt to dummy.txt.bak
but failed to make the desired changes to dummy.txt

1 question though, while I understand why $year is + 1900, why is $mon +1 ?

Brian



Partial success.

The value search for is normally located starting at the 35th char 
into the line.
I split the line so it was at the beginning of a new line and replace 
with worked.

Unfortunately the dates never changed.
I will sleep on this and attack it again in the morning.

Brian

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




Re: Conditional replace

2008-10-22 Thread John W. Krahn

Brian wrote:

Brian wrote:

John W. Krahn wrote:

Brian wrote:


snip



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

@ARGV == 3 or die usage: $0 search for replace with date\n;
my ( $search, $replace, $date ) = @ARGV;

my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%02d/%02d/%04d', $day, $mon + 1, $year + 1900;

( $^I, @ARGV ) = ( '', 'dummy.txt' );


Oops 2!
I changed  ( $^I, @ARGV ) = ( '', 'dummy.txt' );
to ( $^I, @ARGV ) = ( '.bak', 'dummy.txt' );

that got rid of the error, created a copy of dummy.txt to dummy.txt.bak
but failed to make the desired changes to dummy.txt


That's probably because the regular expression didn't match for some reason.


1 question though, while I understand why $year is + 1900, why is $mon +1 ?


perldoc -f localtime
[ snip ]
$mon is the month itself, in the range 0..11 with 0 indicating January 
and 11 indicating December.





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: Conditional replace

2008-10-22 Thread John W. Krahn

Brian wrote:


Partial success.

The value search for is normally located starting at the 35th char 
into the line.
I split the line so it was at the beginning of a new line and replace 
with worked.

Unfortunately the dates never changed.
I will sleep on this and attack it again in the morning.


Based on the example you provided I assumed that search for was at the 
beginning of the line.  If it isn't then you need to use different 
anchors for the pattern, for example:


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

@ARGV == 3 or die usage: $0 search for replace with date\n;
my ( $search, $replace, $date ) = @ARGV;

my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ];
my $today = sprintf '%02d/%02d/%04d', $day, $mon + 1, $year + 1900;

( $^I, @ARGV ) = ( '.bak', 'dummy.txt' );

while (  ) {
if ( ? $search ? ) {
s/(?= )$search(?= )/$replace/;
s!\d\d/\d\d/\d{4}!$date!
and s!($date.*?)\d\d/\d\d/\d{4}!$1$today!;
}
print;
}

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