Can you please Help perl readdir multiple folders and files with pre same name

2009-12-17 Thread Simphiwe Mkhize


Please Help

I have multiple folders want to read trough each folder and search for certian file. I have code which is working fine but does not do what I want. I dont want to hard code path as it read different folder within 


sub get_phone_log {
#--
$p_dir=D:/43895 ;
print  $p_dir;
opendir(DIR, $p_dir) or die cant open directory: $!\n;
  
while(defined($folder = readdir(DIR))) {

 if (length $folder  5)
 {
   print $folder; 
 }
  if (substr($folder,0,8) eq 'PHONELOG') {  
   print_out_contents; 
   }

   }

closedir(DIR) ;

#--
}

sub print_out_contents {
#--
my $phonelog = $p_dir/$folder;

open  (P1FILE, $phonelog) or die can't open $phonelog: $!;

flock (P1FILE, LOCK_SH) or die can't lock $phonelog: $!;

while (P1FILE)
{
   chomp;
   @linex = split(/\|/);
   plfields;
 
build_days;

}

close   (P1FILE);

#--
}


#--
sub plfields {
#--

$plcallername= $linex[0];
$plcallerphone   = $linex[1];
$plcalleremail   = $linex[2];

$plsentdate  = $linex[3];
$plsentdate_ccyy = substr($plsentdate,0,4) ;
$plsentdate_yy   = substr($plsentdate,2,2) ;
$plsentdate_mm   = substr($plsentdate,4,2) ;
$plsentdate_dd   = substr($plsentdate,6,2) ;

$pltime  = $linex[4];
$plfilebase  = $linex[5];
$pltext  = $linex[6];

($plfilebase_type,$plfilebase_num,$plfilebase_name) = split (/\_/, $plfilebase);



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Can you please Help perl readdir multiple folders and files with pre same name

2009-12-17 Thread Shlomi Fish
On Thursday 17 Dec 2009 17:07:19 Simphiwe Mkhize wrote:
 Please Help
 
 I have multiple folders want to read trough each folder and search for
  certian file. I have code which is working fine but does not do what I
  want. I dont want to hard code path as it read different folder within

You can input it from the command-line using @ARGV:


my ($directory_to_search) = @ARGV;

# Do something with $directory_to_search


Now a few comments on your code:

1. You should add use strict; and use warnings; at the top and fix all 
problems that they report.

 
 sub get_phone_log {
 #--
  
 $p_dir=D:/43895 ;

Use my here.

  print  $p_dir;

You probably want:


print $p_dir, \n;
L

here instead.

 opendir(DIR, $p_dir) or die cant open directory: $!\n;
 

Use lexical dir-handles. Kudos for the or die.

 while(defined($folder = readdir(DIR))) {
   if (length $folder  5)
   {
 print $folder;

Again \n;

   }
if (substr($folder,0,8) eq 'PHONELOG') {
 print_out_contents;
 }
 }

This is more idiomatically written as:

if ($folder =~ /\APHONELOG/)
{
}

And please don't use leading ampersands in subroutine calls:

http://www.perlfoundation.org/perl5/index.cgi?subroutines_called_with_the_ampersand

If it's an OS with case-insensitive filenames, you should use the /i flag:

 ... =~ /\Aphonelog/i

 
 closedir(DIR) ;
 
 #--
  }
 
 sub print_out_contents {
 #--
  
 my $phonelog = $p_dir/$folder;

Please don't use such #--- comments before and after the 
beginning of the block. They make the code mcuh harder to follow.

 
 open  (P1FILE, $phonelog) or die can't open $phonelog: $!;

Use lexical filehandles, and three args open:

http://stackoverflow.com/questions/1479741/

 $phonelog  is probably better written as just  $phonelog .

 
 flock (P1FILE, LOCK_SH) or die can't lock $phonelog: $!;
 
 while (P1FILE)

It's better to do:


while (my $line = $p1file)
{


instead and use $line instead. $_ can be destroyed way too easily.

 {
 chomp;
 @linex = split(/\|/);
 plfields;
 
  build_days;

@linex is a package variable - it should be a lexical - declared with my. 
Either pass it to the functions by reference or declare them after its 
declarations.

And don't use call-with-ampersand

 }
 
 close   (P1FILE);
 
 #--
  }
 
 
 #--
  sub plfields {
 #--
 
 
 $plcallername= $linex[0];
 $plcallerphone   = $linex[1];
 $plcalleremail   = $linex[2];

This is better done as:


my ($plcallername, $plcallerphone, $plcalleremail) = @lines;


That way it is easier to insert a variable in between.

You should also use underscores, because several words concatenated like that 
are hard to read. 

 
 $plsentdate  = $linex[3];
 $plsentdate_ccyy = substr($plsentdate,0,4) ;
 $plsentdate_yy   = substr($plsentdate,2,2) ;
 $plsentdate_mm   = substr($plsentdate,4,2) ;
 $plsentdate_dd   = substr($plsentdate,6,2) ;

This is better done using a regex with {4} , {2}, etc.

 
 $pltime  = $linex[4];
 $plfilebase  = $linex[5];
 $pltext  = $linex[6];
 

Again, use the list form.

And make these variables lexicals - using my.

 ($plfilebase_type,$plfilebase_num,$plfilebase_name) = split (/\_/,
  $plfilebase);
 

Why are you using /\_/ instead of /_/, or /\\_/? 

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

Bzr is slower than Subversion in combination with Sourceforge. 
( By: http://dazjorz.com/ )

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Perlcritic complains about assignment within conditional

2009-12-17 Thread Peter Scott
Although it turned out not to be what was going on here, just to address 
your subject line' it's not just Perlcritic that will complain about 
assignment in a conditional, but also warnings:

% perl -wle 'print 42 if $x = 0'
Found = in conditional, should be == at -e line 1.

And of course you should always have warnings enabled, and always make 
sure your program doesn't output any.

Note that there is or at least used to be also a side effect in perl that 
would cause that warning if there was an assignment nearby a 
conditional, even if not where you'd think it ought to be to complain.  I 
forget the details exactly and haven't hit it again in eons.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: capture error - the better way?

2009-12-17 Thread Peter Scott
On Wed, 16 Dec 2009 10:09:38 +, Philip Potter wrote:

 2009/12/16 Shlomi Fish shlo...@iglu.org.il:
 On Tuesday 15 Dec 2009 17:14:25 Philip Potter wrote:
 If evaluating a constant expression results in a runtime exception,
 that runtime exception must happen at runtime, and not at compile
 time. In general, it is the duty of an optimizer never to change
 program behaviour, only performance.


 But this is a case where the compiler evaluates a constant expression
 and it results in an exception. So it cannot store it as a folded
 constant on the side for the run-time place. As a result, it throws a
 compile-time error.
 
 Yes, it can't fold the constant. That's no excuse for changing the
 behaviour of the program. What it should do is what I wrote in my
 previous email -- replace it with code that raises a runtime exception
 when executed -- ie a die Illegal division by zero.

This is Perl.  The distinction between the compilation and run time phases 
is almost totally academic.  (For instance, use strict throws some 
exceptions at compile time and others at run time.)  Since there's no 
compiled executable produced, the only difference in behavior is that your 
program dies sooner than it would have.  Of course, conceivably it would 
not have died at all if the constant folding occurred in conditionally 
unexecuted code.  

So think of this as compile-time validation made possible by the use of 
constants: if your program *were* to execute that code, it would die.  
Better to know about that in advance than 49 hours into a 50 hour duty 
cycle.

Perl has never made a promise about compilation phase vs runtime phase 
separation of duties you appear to want.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: being smart about script structure

2009-12-17 Thread Peter Scott
On Wed, 16 Dec 2009 17:47:16 -0600, Bryan R Harris wrote:
 Okay, here's one I struggle with often -- is one of these better than
 the other?
 
 **
   A.
 if ( isFlat($tire) ) { changeTire($tire); }
 
   B.
 checkFlatAndChangeTireIfNecessary($tire);
 
 **

Ah, a recovering Java programmer :-)

Seriously, the letsRunAsManyWordsAsPossibleTogether style bugs the heck 
out of me.  The Perlish style (perldoc perlstyle) would be

check_flat_and_change_tire_if_necessary( $tire )

and I agree with Shawn's suggestion of object methods.  But even if you 
don't use objects, you can clearly remove at least some redundancy:

check_flat_and_change_if_necessary( $tire )

Personally I would, all things being equal, probably write that as

is_flat( $tire) and change_tire( $tire )

putting the tire back in because a subroutine named just change is too 
vague.  Oh, I see that's exactly what Shawn had.  GMTA :-)  There is also

change_tire( $tire ) if is_flat( $tire );

depending on what you think is more important, the changing or the 
checking.  TIMTOWDI.

The question you pose is one I see raised often in Java circles.  It comes 
up rarely among Perl programmers.  Possibly because we do not use nearly 
as many IDEs and automated refactoring tools.  Neither will you find Perl 
programmers enamored of Hungarian notation, for instance.  But remember 
that in Java these would be method calls anyway.  Design Patterns in Perl 
look rather different.  Many design patterns exist only to solve problems 
that don't exist in Perl (think typing).

Java programmers are far more obsessed with learning correct ways of 
structuring methods.  I find their discussions interesting but mostly 
academic.  As long as my methods and subroutines are under a screen's 
length each and reusability is maximized (Don't Repeat Yourself), I'm 
happy.  Whether the condition goes inside or outside the subroutine may 
depend on usage.

The older I get, the shorter my methods get.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: being smart about script structure

2009-12-17 Thread Shawn H Corey
Peter Scott wrote:
 Neither will you find Perl 
 programmers enamored of Hungarian notation, for instance.

That's because there are few datatypes in Perl but context is
everything.  It's hard to describe context using Hungarian notation.  :)


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Perlcritic complains about assignment within conditional

2009-12-17 Thread Steve Bertrand
Peter Scott wrote:
 Although it turned out not to be what was going on here, just to address 
 your subject line' it's not just Perlcritic that will complain about 
 assignment in a conditional, but also warnings:
 
 % perl -wle 'print 42 if $x = 0'
 Found = in conditional, should be == at -e line 1.
 
 And of course you should always have warnings enabled, and always make 
 sure your program doesn't output any.

heh.

Thanks for picking up on my thread.

fyi, and fwiw, I always, _always_, *ALWAYS* start my scripts, tests,
packages and modules with the shebang line:

#!/usr/bin/perl # doesn't everyone have it here ;P

...followed by a single empty line...

...followed by warnings and then strict. I don't have any particular
order for the two. I often wonder though whether I have a psychological
issue that has me type 'use warnings' before 'use strict' some days, and
then on other days, I do it the opposite.

Either way. You will *never* find anything that I have written,
regardless of how bad it may be, that doesn't start with:

#!/usr/bin/perl

use strict;
use warnings;

use Something::Else;

use My::Something;

...yes, I truly do ALWAYS leave the empty line before the next use
statement after the mandatory pragmas. I'll also leave a blank line
between CPAN modules and the modules I wrote myself.

I've been getting into a habit however of removing the whitespace
between use Something::Else and MY::Module in cases where I feel that my
work is CPAN semi-quality but I haven't received my PAUSE ID yet ;)

 Note that there is or at least used to be also a side effect in perl that 
 would cause that warning if there was an assignment nearby a 
 conditional, even if not where you'd think it ought to be to complain.  I 
 forget the details exactly and haven't hit it again in eons.

Does the side effect affect Perl or perl?

I'm content in knowing that if Uri, John, Shlomi, Shawn etc had noticed
such a nearby side-effect, I'dve heard about it eons ago ;)

Cheers,

Steve

ps. even in my one-liners, I have a hell of a time not using
strict/warnings, and I especially cringe at not lexicalizing my variables!



 


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/