Re: Formatting

2002-06-20 Thread Marco Antonio Valenzuela Escárcega

On Thu, 2002-06-20 at 22:42, Shishir K. Singh wrote:
> Hi, 
hi
> 
> I need to format a string in a fixed width field. The string may be less than the 
>length of the format, or may be greater. If less, then it should get  padded with 
>spaces (left or right justified , like using - in sprintf), if greater, then the 
>string should get truncated to the exact length. 
> 
> 
> eg $myVar = 'ABCDEFGHIJKLMNOP';
> $newVar = sprintf("%10s,$myVar);
> $newVar should have 'ABCDEFGHIJ'; # But has the full length i.e. 'ABCDEFGHIJKLMNOP'
> 
> eg $myVar = 'ABCD'; (Right Aligned, padded with spaces)
> $newVar = sprintf("%10s,$myVar);
> $newVar should have '  ABCD'; # Works
> 
> eg $myVar = 'ABCD'; (Left Aligned, padded with spaces)
> $newVar = sprintf("%-10s,$myVar);
> $newVar should have 'ABCD  '; # Works
> 
> 
> I am not able to lay my finger on the correct format to achieve 1st and the 2nd with 
>the same format. Am I missing something , or is there another way out? Any help would 
>be greatly appreciated.
> 
you can try using pack:

$myVar = 'ABCDEFGHIJKLMNOP';
$newVar = pack 'A10', $myVar;
$newVar eq 'ABCDEFGHIJ';

$myVar = 'ABCD';
$newVar = pack 'A10', $myVar;
$newVar eq 'ABCD  ';



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Fwd: Re: Please explain forking

2002-06-20 Thread Marco Antonio Valenzuela Escárcega

On Thu, 2002-06-20 at 23:30, Langa Kentane wrote:
> Greetings,
> I wish to use the fork() function on one of my scripts. I would like more
> clarity on the way it works.
> 
> Take for instance the ff code:
> 
> Sub mysub
> {
>   while ()
>   If ($_ eq "SCANME")
>   {
>   fork()
>   system("nessus" "thathost");
system() does an implicit fork(), so you don't have to do the fork()
yourself

>   print "Forked!\n";
>   }
>   Else
>   {
>   fork()
>   system("nmap", "-sS", "-T", "insane", "mynetwork")
>   print "Forked on else\n";
>   }
>   close THATFILE;
> }
> 
> What I would like to know when I do the fork on the if statement, does the
> child just do the stuff under if block & exit?? 
> What happens exactly? Is this the right way of doing this?
> 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Debug and dereferrencing a hash

2002-06-22 Thread Marco Antonio Valenzuela Escárcega

On Sat, 2002-06-22 at 13:53, Shawn wrote:
> Hello all,
>   I am working on a script that I have several debug statements that try to tell me 
>what is contained in a hash referrence.  While I am tailing the log file, the hashes 
>derefference (%$hash_ref) to n/n (ei: 1/8) instead of the actual referrence (ie: 
>user=>'1', pass=>'2').  The arrays referrences derefference (@$array_ref) just fine.
> 
> Is there a reason for this, and is there a way I can see what the actuall values 
>are?  The values are passed just fine to the sub routines, I just want my debug log 
>to show that values passed without having to set up a for loop (ie: print %$hash_ref).
> 
> TIA,
> Shawn

probably you are trying to use the hash in scalar context,

you should be able to se the values like this:

print "@{ [ values %$hash_ref ] }\n";



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Debug and dereferrencing a hash

2002-06-22 Thread Marco Antonio Valenzuela Escárcega

On Sat, 2002-06-22 at 13:53, Shawn wrote:
> Hello all,
>   I am working on a script that I have several debug statements that try to tell me 
>what is contained in a hash referrence.  While I am tailing the log file, the hashes 
>derefference (%$hash_ref) to n/n (ei: 1/8) instead of the actual referrence (ie: 
>user=>'1', pass=>'2').  The arrays referrences derefference (@$array_ref) just fine.
> 
> Is there a reason for this, and is there a way I can see what the actuall values 
>are?  The values are passed just fine to the sub routines, I just want my debug log 
>to show that values passed without having to set up a for loop (ie: print %$hash_ref).
> 
> TIA,
> Shawn

probably you are trying to use the hash in scalar context,

you should be able to se the values like this:

print "@{ [ values %$hash_ref ] }\n";


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: calculating disk space in perl

2002-06-22 Thread Marco Antonio Valenzuela Escárcega

On Sat, 2002-06-22 at 20:34, J. Lundeen wrote:
> Hello,
> 
> I'm writing a control panel for my web hosting company. I need to be
> able to calculate how much disk space a user is taking up in their home
> directory and its sub-directories, so that I can compare against the
> "allowed" disk space of the selected hosting plan.
> 
> I'm aware of the shell command: du -hs foldername
> 
> But is there an easy way to get the info into Perl so that I can use it
> in a calculation?

use the backticks or qx//:

my ($diskspace) = split ' ', qx/du -hs $foldername/;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: To Croak or Undef?

2002-06-22 Thread Marco Antonio Valenzuela Escárcega

On Sat, 2002-06-22 at 21:27, drieux wrote:
> 
> volks,
> 
> I'm playing around with a little perl module and I came
> across to different solutions - thought I would ask for
> opinions as to which would be more Kosher - so why not
> ask folks who might have an opinion:
> 
> Plan A: The simple 'undef' trick
> 
>   #
>   sub get_Lock {
>  my ($self, $name) = @_;
>   (exists $self->{$name}) ? $self->{$name}->{var_lock} : undef;
>   } # end of get_Lock
> 
> Plan B: your basic Croak Play
> 
>   #
>   sub get_Lock {
>   my ($self, $name) = @_;
>   croak "No such variable $name \n"
>   unless (exists $self->{$name}) ;
>   $self->{$name}->{var_lock};
>   } # end of get_Lock
> 
> Under Plan A - one would code it like:
>   
>   my $lock = $global->get_Lock('var1');
> 
>   BailOut("bad var1\n") unless ( defined( $lock ) ) ;
> 
> since, well - it is possible that 'var1' does not exist...
> 
> Under Plan B - one gets the SCREAM from croak right off
> 
>   [jeeves:pbl/bloopers/dumbDrieux] drieux% perl OO_GlobalGame.txt
>   No such variable var1
>at OO_GlobalGame.txt line 14
>   [jeeves:pbl/bloopers/dumbDrieux] drieux%
> 
> and one could go look at line 14 of their code and go
> "Oh dear, never really meant that one - meant to check 'var'
> or what ever it should have been
> 
> Opinions?
> 
I think Plan A is a more general solution, because maybe there will be 
some times when you don't want a warning, instead you'll want to be able
to make some test:

if ($lock = $global->get_Lock('var1') {
# var1 exists
# run this code
} else {
# var1 doesn't exist
# do something else
}

just my 2 cents



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Debug and dereferrencing a hash

2002-06-22 Thread Marco Antonio Valenzuela Escárcega

On Sat, 2002-06-22 at 21:42, Shawn wrote:
> >>> On Sat, 2002-06-22 at 13:53, Shawn wrote:
>  Is there a reason for this, and is there a way I can see what the 
>  actuall values are?  The values are passed just fine to the sub routines,
>   I just want my debug log to show that values passed without having to 
>  set up a for loop (ie: print %$hash_ref).
> 
>  TIA,
>  Shawn
> >>>
> >>> probably you are trying to use the hash in scalar context,
> >>>
> >>> you should be able to se the values like this:
> >>>
> >>> print "@{ [ values %$hash_ref ] }\n";
> >>
> >> way Close
> >>
> >> my %hash = (user => 1,pass => 2,thing => 3,ptr => [qw(4 5)],);
> >>
> >> my $hash_ref = \%hash;
> >>
> >> print split(/ /,%$hash_ref ), "\n";
> >> print "Not Split: ", %$hash_ref, " :\n";
>  >> my @array = %$hash_ref;
> >> print "Array Game: @array :\n";
> >>
> >> print " Player: @{ [  %$hash_ref ] }\n";
> >>
> >> generates
> >>
> >> 4/8
> 
> >
> > This is the number of buckets used over the number of buckets.  Probably
> > not terribly useful unless you are optimising hashing algorithms.  In
> > this case it shows that perl has done a pretty good job with the hash.
> 
> 
> Can you elaborate more on the 'buckets' Paul, or point me to a doc that explains it 
>more?  All the hash elements should be (are) filled when I dereferrence the hash in 
>PACKAGEB.  So why would I ever see 1/8?
> 
> >> Not Split: pass2thing3ptrARRAY(0x6410)user1 :
> >> Array Game: pass 2 thing 3 ptr ARRAY(0x6410) user 1 :
> >>   Player: pass 2 thing 3 ptr ARRAY(0x6410) user 1
> 
> Ok, well, I think I am basically doing this (Not Split)... let me give you a snippet 
>(a very basic one anyway):
> 
> MAIN script:
> use PACKAGEA;
> use PACKAGEB;
> $A=PACKAGEA->new; # OBJ A gathers all url params
> $B=PACKAGEB->new;
> print $B->somesub(&make_hash);
> print $B->{DEBUG};
> exit;
> 
> sub make_hash {
>   my %hash;
>   for('1','2','3','4') {
> $hash{$_}=$A->params($_);
>   }
>   return(\%hash);
> }
> 
> PACKAGEB:
> 
> sub somesub {
>   my $self=shift;
>   $self->{DEBUG}.="B::somesub called (line # ".__LINE__.")\n";
>   $self->{SOMEPARAM}=shift;
>   $self->{DEBUG}.="SOMEPARAM: ".%{$self->{SOMEPARAM}}."\n";
here's the problem: when you try to concatenate the hash to the string
you are evaluating it in scalar context, and that's why it gives you the
number of used buckets

this should work:
$self->{DEBUG}.="SOMEPARAM: @{[%{$self->{SOMEPARAM}}]}\n";

after you dereference the hash, you populate an anonymous array with it,
and then you dereference the array and interpolate it in the string

its very ugly, but it'll work ;-)
>   ...
> }
> 
> 
> The data sets get a bit more complex from here, but that is not an issue for me at 
>this point.  I can print out the referenced arrays and strings fine.  It is only the 
>hashes that I am having issues with...
> 
> Again, TIA,
> Shawn
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: To Croak or Undef?

2002-06-22 Thread Marco Antonio Valenzuela Escárcega

On Sat, 2002-06-22 at 21:54, Marco Antonio Valenzuela Escárcega wrote:
> On Sat, 2002-06-22 at 21:27, drieux wrote:
> > 
> > volks,
> > 
> > I'm playing around with a little perl module and I came
> > across to different solutions - thought I would ask for
> > opinions as to which would be more Kosher - so why not
> > ask folks who might have an opinion:
> > 
> > Plan A: The simple 'undef' trick
> > 
> > #
> > sub get_Lock {
> >  my ($self, $name) = @_;
> > (exists $self->{$name}) ? $self->{$name}->{var_lock} : undef;
> > } # end of get_Lock
> > 
> > Plan B: your basic Croak Play
> > 
> > #
> > sub get_Lock {
> > my ($self, $name) = @_;
> > croak "No such variable $name \n"
> > unless (exists $self->{$name}) ;
> > $self->{$name}->{var_lock};
> > } # end of get_Lock
> > 
> > Under Plan A - one would code it like:
> > 
> > my $lock = $global->get_Lock('var1');
> > 
> > BailOut("bad var1\n") unless ( defined( $lock ) ) ;
> > 
> > since, well - it is possible that 'var1' does not exist...
> > 
> > Under Plan B - one gets the SCREAM from croak right off
> > 
> > [jeeves:pbl/bloopers/dumbDrieux] drieux% perl OO_GlobalGame.txt
> > No such variable var1
> >  at OO_GlobalGame.txt line 14
> > [jeeves:pbl/bloopers/dumbDrieux] drieux%
> > 
> > and one could go look at line 14 of their code and go
> > "Oh dear, never really meant that one - meant to check 'var'
> > or what ever it should have been
> > 
> > Opinions?
> > 
> I think Plan A is a more general solution, because maybe there will be 
> some times when you don't want a warning, 
for some reason I was thinking in carp instead of croak.

I think the user of the module is the one who should decide what to do
(kill the script or anything else), and you're giving him what he needs
to make that decision by returning undef
instead you'll want to be able
> to make some test:
> 
> if ($lock = $global->get_Lock('var1') {
> # var1 exists
> # run this code
> } else {
> # var1 doesn't exist
> # do something else
> }
> 
> just my 2 cents
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: if-statement and grep in one go

2002-06-25 Thread Marco Antonio Valenzuela Escárcega

On Tue, 2002-06-25 at 03:28, David vd Geer Inhuur tbv IPlib wrote:
> 
> Hello,
> 
> Thanks for the solution Bob.
> Changed some stuff and have 2 questions open.
> 
>   my $line;
>   my (%u, %g);
>   open(FILE, "< ${dir}/user.perm") or print "Failed opening file $!"; ## 1
>   while ($line = ) {## 2
>   if ($line =~ /^user:/) {
>  $u{$_} = 1 for split(/,/, substr($line, 6)); ## 3
>   }
>   if ($line =~ /^group:/) {
>  $g{$_} = 1 for split(/,/, substr($line, 7)); ## 4
>   }
>   }
>   close(FILE);
> 
>   print "Invalid login"; exit unless $u{$pwuser} || $g{$group};   ## 5
> 
> 1) I don't like to die in my script as there are many files to read. And if I
>can't open the current file I just want to continue with the rest of the files
>So I always prefer printing.
> 2) while my $line () didn't work, just a notation error.
> 3) I don't need to split on spaces. The script that fills the user.perm is 
>designed to always : "user: vdgeerd,and,other  ## Never mind.
>BUT, What do you do with the <1> ?? Changing it to $1 doesn't matter too.
>I keep empty places when doing a " foreach my $value(%u) { print $value; } "
>chomp %u; didn't work either.
> 4) Same as 3
> 5) I changed the && into || cause the user can have acces due to group or user 
>rights.
>BUT, how do I combine a printing error message and an exit within the unless ?

if you need to print an error message and exit, use die:
die 'Invalid login' unless $u{$pwuser} || $g{$group};



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: real beginner's question (s|{(.*?)}|$1=~ tr///|xg)

2002-07-08 Thread Marco Antonio Valenzuela Escárcega

On Mon, 2002-07-08 at 14:03, Kevin Pfeiffer wrote:
> bob ackerman writes:
> [...]
> > so:
> > s|{(.*?)}|$1=~ tr///|xg;
> 
> I'm trying to test this myself on a string (rather than $_), but don't 
> understand how to use tr/// on $1 and then place the result back into the 
> string (practice only).
you can't use tr/// on $1, because that's a read-only variable, instead,
try to assign the value of $1 to another variable and use tr/// on that
one:
($use_tr_on_me = $1) =~ tr/iw/JX/;
> 
> I tried a sort of nested expression similar to what you show
> 
> $string =~ s|{(.*?)}|{$1 =~ tr/iw/JX/)}|xg;
> 
> It's supposed to capture the text between curly brackets and then do a 
> transliteration (i-->J and x-->X)
> 
> but for: kdkdkiwiwdkdkdk {iwidkwidkw} kdkdkdwiwiwkdkdk {kdkdikdkddk}
> 
> I get: "kdkdkiwiwdkdkdk {iwidkwidkw =~ tr/iw/JX/} kdkdkdwiwiwkdkdk 
> {kdkdikdkddk =~ tr/iw/JX/}"
that's because you are substituting (.*?) for "$1 =~ tr/iw/JX/" as a
string, if you want to evaluate it as code, you need to add an e
modifier to s///, (and you can get rid of the x because you aren't using
it anyway):
$string =~ s|{(.*?)}|($t=$1) =~ tr/iw/JX/;"{$t}"}|eg;

hope that helps :-)

Marco Antonio Valenzuela Escárcega



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: a real beginner's question, pt. 2

2002-07-08 Thread Marco Antonio Valenzuela Escárcega

if what you are trying to do is change 'M' for the string 'U+1E43' you
can try using a hash:

%table = ( M => 'U+1E43'); # or maybe ( M => "\x{1E43}")
$string =~ s<
 {(.*?)}
> <
 '{'.(join '', map { $table{$_} || $_ } split //, $1).'}';
>exg;

or you can try anything else, the point is that using the /e modifier
with s/// you can use any piece of code that works for you as the
replacement of the substitution


Marco Antonio Valenzuela Escárcega


On Mon, 2002-07-08 at 20:14, David Carpenter wrote:
> Thanks to all those who responded to my original post.  I posted the
> clarifcation below as a reply to the original thread, but I think it was
> lost in the shuffle.
> 
> I think I failed to make something clear. What I need is to selectively
> replace only
> certain characters when they appear in a string within brackets. I don't
> need to transform the string as a whole. So if I
> have:
> 
> Don't Match the capital M unless {the M is inside brackets} then Match it
> 
> I need to be able to change the M inside the brackets in such a way that the
> entire line is output, but with, in this case, the single change that the
> one M within the brackets has been changed to another character, say an X,
> so that the line would now read:
> 
> Don't Match the capital M unless {the X is inside brackets} then Match it
> 
> I need to process a very large file (c. 20MB) in this way, and there are
> about a dozen
> different single characters what would have to be systematically changed to
> an alternative character.  Actually, what I want to do is change the M, for
> instance, to the Unicode character U+1E43.  I've been able to apply these
> changes to the file as a whole, but don't know how to apply them only to
> certain characters that appear in text within brackets.
> 
> I was assuming (wrongly?) that I would have to set up some kind of loop to
> run through each string within brackets to test for the M (and a number of
> other characters) and change it to the U+1E43 (an a number of other Unicode
> characters) if found.  Can I do all this just using regular expressions with
> s/// or tr///?
> 
> I hope this is more clear.  Thanks again to everyone who has offered help..
> I'm learning a lot about regular expressions!  (or at least I'm trying to!)
> 
> David
> 
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: script too slow?

2002-07-13 Thread Marco Antonio Valenzuela Escárcega

maybe you should check this out:
http://search.cpan.org/search?dist=RTF-Tokenizer
http://search.cpan.org/search?dist=RTF-Parser

On Sat, 2002-07-13 at 19:35, Paul Tremblay wrote:
> I just finished my first version of a script that converts rtf to
> xml and was wondering if I went about writing it the wrong way.
> 
> My method was to read in one line at a time and split the lines
> into tokens, and then to read one token at a time. I used this
> line to split  up the text:
>  
> @tokens = split(/({\\[^\s\n\}{]+)|(\\[^\s\n\\}]+)|()|(})|(\\})/,$line);
> 
> Splitting up the text on my test file of 1.8 megabytes tooks 25
> seconds. The entire script took 50 seconds. 
> 
> I had written a previous uncompleted version in which I relied on
> regular expressions rather than tokens, and this script took only
> 10 seconds to run. I gave up on this method because it seemed
> there would always be an excpetion that would require another
> regexp.
> 
> So why does splitting a text into tokens take so long? Has
> anybody done something similar to what I am trying, and do you
> have any advice? 
> 
> The good news is that relativley speaking, perl is very, very
> fast. I tried a similar script in python using a lexer called
> plex, and the 1.8 megabyte file took 12 minutes to parse!
> 
> In case you are wondering why I'm seemingly obsessed with speed,
> I would like to make this script available to anyone. Right now
> the only free utilities for converting rtf to xml are a java
> utility call majix, which deletes your footnotes and only allows
> for 9 user-defined styles. If my perl script is too slow, it won't be
> very useful.
> 
> Thanks
> 
> Paul
> 
> 
> -- 
> 
> 
> *Paul Tremblay *
> *[EMAIL PROTECTED]*
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Interpreter

2002-07-23 Thread Marco Antonio Valenzuela Escárcega

tal vez te sirva leer esto:
http://www.perl.com/pub/a/2001/06/13/recdecent.html

es un tutorial de Parse::RecDescent, y como ejemplo se implementa un
mini-lenguaje, y se sugieren algunas ideas acerca de que se le puede
agregar.  con suerte y este ejemplo es suficiente como base de tu
proyecto.

suerte!
Marco Antonio Valenzuela Escárcega

On Mon, 2002-07-22 at 11:16, Omar Shariff wrote:
> Hi Timothy
> 
>  --- Timothy Johnson <[EMAIL PROTECTED]> escribió:
> > 
> > Para clarificacion, estas buscando la programa que
> > interpreta los scripts
> > creado en Perl, no?  Si estas usando Windows, lo
> > puede encontrar aqui:
> > http://www.activestate.com.  Se llama ActivePerl. 
> > Si estas usando UNIX, se
> > que lo puedes buscar en http://www.perl.org o
> > http://www.cpan.org.  Ahi
> > tambien puedes encontrar muchas "modules" para Perl.
> 
> No... i'm looking to write an interpreter in Perl
> code, i download a perl code interpreter for Basic and
> one for prolog, this modules can to read a basic code
> program and run it, it parse, check lexical and
> sitax, and then run it... i need to do a compiler or
> interpreter (for university) it's don't need to be in
> perl but perl mannage the regular expresions and
> gramaticals too good!!! i hope thats understand... 
> 
> > 
> > Me falta la Argentina.  Ahorita he olvidado mucho
> > del Castellano y lo demas
> > que aprendia sobre ella, pero cuando yo era nino,
> > vivia en Cerrillos acerca
> > de Salta.
> > 
> 
> Now Salta la linda! yo vivo en Buenos Aires...
> 
> 
> Ahora podés usar Yahoo! Messenger desde tu celular. Aprendé cómo hacerlo en Yahoo! 
>Móvil: http://ar.mobile.yahoo.com/sms.html
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Hi, newbie question

2002-07-24 Thread Marco Antonio Valenzuela Escárcega

On Mon, 2002-07-22 at 14:41, Desmond Lee wrote:
> Hi guys
> 
> I'm trying to read a file, but it's just one massive line. I think that the 
> ^M  is suppose to be an indication that that's wehre teh newline is suppose 
> to be. I've tried to replace ^M with a newline by executing something that i 
> found on the web:
> 
> perl -pi.bak -e 's/\^M/\n/g' moby_threads_install.txt

try: 
perl -pi.bak -e 's/\r/\n/g' moby_threads_install.txt



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]