RE: One Question

2006-01-09 Thread Larsen, Errin M HMMA/IT
> -Original Message-
>From: Gomez, Juan [mailto:[EMAIL PROTECTED] 
>Sent: Monday, January 09, 2006 7:48 AM
>To: beginners@perl.org
>Subject: One Question
>
>
>Good Morning all!!!
>

Good Morning, Juan,

>
>I have working on several shell scripts using KSH 
>but i like to know is there can be a way to use Perl/tk 
>to make them run like a inside of a menu?
>
>
>thanks guys
>
>
>
>
>Armando Gomez Guajardo 
>Process Engineer
>Work Ph   956 547 6438 
>Beeper956 768 4070



Juan, perhaps you are unaware of the nature of this mailing list.  I
will help you out by filling you in.

The beginners@perl.org list is for (quoted from the FAQ, which can be
found at this link - http://learn.perl.org/beginners-faq):


"  2.1 - What is the list for?
A list for beginning Perl programmers to ask questions in a friendly
atmosphere.

   2.2 - What is this list _not_ for?
SPAM
Homework
Solicitation
Things that aren't Perl related
Monkeys
Monkeys solicitating homework on non-Perl related SPAM. "



So, in the spirit of those FAQs and their answers ... I will answer your
question:

>I have working on several shell scripts using KSH 
>but i like to know is there can be a way to use Perl/tk 
>to make them run like a inside of a menu?

Yes.



... If you need more help, you need to try writing the Perl code to
implement what you are suggesting and come back to this list with
specific questions about any problems with your code you have.

--Errin 

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




RE: Finding directories within a tree

2005-10-07 Thread Larsen, Errin M HMMA/IT
[EMAIL PROTECTED] wrote:
> Good Afternoon
> 
> I am attempting to develop a script that will parse a directory
> listing and return only directory names that match a given
> expression.  
> 
> It would make sense to me to use File::Find to do this but based on
> the dir structure I am parsing, the amount of overhead to do this is
> enourmous !  
> 
> Basically, I have a file structure similar to:
> 
> Dir1\Dir2\Support\119404\dirx\diry
> Dir1\Dir3\Support\119893\dirx
> Dir1\Dir4\Support\14\dirx\diry\dirz
> .
> Dir1\Dir1000\Support\100858
> 
> I am simply interested in finding the directories directley under the
> Support dir (ex.119404 from the 1st example) . There is no
> consistancy to the naming convention other then Dir1 and Support.
> Dir2 can be many different values.   
> 
> I tried functionality similar to the following that did work on a
> much smaller test bed: 
> 
> my $dirs="I:\\ID_00_000999";
> find sub { push @dirs, $File::Find::dir if $File::Find::dir =~
> m/.+[Ss]upport\/\d+$/;}, $dirs; 
> 
> But in a larger scale dir structure. The performance of this was
> horrible !! (Since it is looking through the entire structure
> including dirs under the directory I am trying to match on).  
> 
> As you can see form the I:\\ this is on windows, so ls and similar
> UNIX commands are not available. 
> 
> Any thoughts on how I can accomplish this task with the lowest amount
> of overhead? 
> 
> Thanks!
> Jason

Hi Jason,

Did you look into globs?  You need to make a list of every directory
you're interested in ONCE, then process that list.

This worked for me:


# I don't have access to a windows system with Perl
# installed, but as this is all native perl, it
# should be fine

my $dirs='/ID_00_000999';
my @dirs_found;

push @dirs_found, $_ while <$dirs/*/[Ss]upport/*>;



You can check out these perldocs for further info:

perldoc -f glob
perldoc File::Glob
perldoc perlop (look for I/O Operators) 

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




RE: here tag and internal variable

2005-09-23 Thread Larsen, Errin M HMMA/IT
Frank Geueke, III wrote:
> Thanks for the help with the reverse if and else.  I
> ended up  using a single line forward if on a
> single line.  Now my next question.  I'm using a jumbo
> here tag (I think that's what they're called) to add
> html to a string.  I need to use a variable within the
> tag (done this before, no problem) but I need to have
> text immediately following the variable name.  How do
> I tell Perl that the text is not part of the variable
> name?  Here's my code:
> 
> sub print_html_beginning
> {
> my $html_ref = shift(@_);
> my $get_to_index = shift(@_);
> ${$html_ref} .= <<"___HTML___";
> Content-type: text/html
> 
>  Transitional//EN"
>  "http://www.w3.org/TR/html4/loose.dtd";>
> 
>   
>CONTENT="text/html; charset=iso-8859-1">
>  Hotspot Tracker 
>  HREF="$get_to_indexcss/hotspot.css" TYPE="text/css"
>  MEDIA="screen">
> 
> in the link tag Perl thinks the var name is
> $get_to_indexcss...
> 
> Thanks.  ~Frank
> 

Hi Frank,

In your example, you could escape the '_' (underscore) out.  Like this:

HREF="$get\_to_indexcss/hotspot.css" ...

That seems to work for me.

--Errin


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




RE: Compiling perl scripts

2005-08-05 Thread Larsen, Errin M HMMA/IT
Larsen, Errin M HMMA/IT wrote:
> Binish A R wrote:
>> Does anyone knows how to compile perl scripts to executables? I'm
>> working on RH Linux and would like to know the details. I know that I
>> can use perlcc to do that ... but it isn't compiling anything other
>> than a simple 'hello world' program :(
>> 
>> Is there any other tools besides perlcc ??
>> 
>> Thanks in advance
>> 
>> 
>> /binish/
> 
> Wow.
> 
> I KNEW something was missing lately on this list: I haven't seen a
> good "How do I compile my Perl" thread in quite some time!  I look
> forward to all the posts about why and why not to compile your Perl!!
> Just be glad you didn't indicate that you wanted to compile for
> obfuscation reasons, that really sets some people off!!
> 
> In the meantime, Binish, check out this earlier thread for some links:
> 
>
http://groups.google.com/group/perl.beginners/browse_frm/thread/c5e3ef71
> 88def85e
> 
> --Errin Larsen

OK ... That link didn't work AT ALL!!

I'll just cut and paste the discussion below:



1. De Joe May 29 2003, 6:48 pm show options
Newsgroups: perl.beginners
From: [EMAIL PROTECTED] (De Joe) - Find messages by this author
Date: Thu, 29 May 2003 18:30:49 -0500
Local: Thurs, May 29 2003 6:30 pm
Subject: Can I compile perl so I can run a perl script without having
perl installed?
Reply to Author | Forward | Print | Individual Message | Show original |
Report Abuse

Can I compile perl so I can run a perl script without having perl
installed?

Thanks,
Jackie
 

2. Jenda Krynicky May 30 2003, 8:00 am show options
Newsgroups: perl.beginners
From: [EMAIL PROTECTED] (Jenda Krynicky) - Find messages by this author
Date: Fri, 30 May 2003 14:56:31 +0200
Local: Fri, May 30 2003 7:56 am
Subject: Re: Can I compile perl so I can run a perl script without
having perl installed?
Reply to Author | Forward | Print | Individual Message | Show original |
Report Abuse

From: "De Joe, Jackie" <[EMAIL PROTECTED]>

> Can I compile perl so I can run a perl script without having perl
> installed?

Yes. And you may even choose which tool to use:

PAR - free
http://search.cpan.org/search? mode=all&query=PAR

PerlApp - part of Perl Development Kit
http://www.activestate.com/Pro ducts/Perl_Dev_Kit/

Perl2Exe
www.perl2exe.com

HTH, Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery 

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




RE: Compiling perl scripts

2005-08-05 Thread Larsen, Errin M HMMA/IT
Binish A R wrote:
> Does anyone knows how to compile perl scripts to executables? I'm
> working on RH Linux and would like to know the details. I know that I
> can use perlcc to do that ... but it isn't compiling anything other
> than a simple 'hello world' program :(  
> 
> Is there any other tools besides perlcc ??
> 
> Thanks in advance
> 
> 
> /binish/

Wow.

I KNEW something was missing lately on this list: I haven't seen a good
"How do I compile my Perl" thread in quite some time!  I look forward to
all the posts about why and why not to compile your Perl!!  Just be glad
you didn't indicate that you wanted to compile for obfuscation reasons,
that really sets some people off!!

In the meantime, Binish, check out this earlier thread for some links:

http://groups.google.com/group/perl.beginners/browse_frm/thread/c5e3ef71
88def85e

--Errin Larsen

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




Wrapper Script

2005-06-27 Thread Larsen, Errin M HMMA/IT
(I'm mailing this after posting through Google Groups.  I didn't see it
get posted, so I'm sending email instead.  I hope this doesn't double
post.  If it does, I'm sorry.)

Hi Perl folks,

  I am trying to write a wrapper script around an executable file so
that I am able to better control the results of other programs calling
that file.  I have written a short and easy script that collects the
options in a log file, and then execs the original command (just for
testing).

#!/usr/bin/perl

use strict;
use warnings;

my $datestampe = localtime(time);

open LOG, ">>/tmp/options.log" or die "Can't open /tmp/options.log:
$!";
print LOG "$datestamp: Command and options: $0 @ARGS";
exec( '/usr/bin/original-executable', @ARGS);

However, I have found that some open the original file in a pipe.  Is
there a way I can view the contents of what is being sent through the
pipe?

Thanks,

--Errin 

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




RE: how to append blocks in same file

2005-06-22 Thread Larsen, Errin M HMMA/IT
Aditi Gupta wrote:
> Hi Perlers,
> 
> I have a file as follows:
> 
> #block1
> aaa
> aaa
> 
> #block2
> bbb
> bbb
> 
> #block3
> ccc
> ccc
> 
> and i want to append(or i should say merge) these blocks and get a
> file like this:
> 
> aaabbbccc
> aaabbbccc
> 
> how can this be done using perl?
> please help..
> 
> regards
> Aditi

Hi,

That looks like a fun and challenging problem.  I bet Perl would be good
at that.

Have you tried any Perl code yet?

This list works best when you present us some code you have tried that
is not working.  Then, we can help you fix it.  There are many on the
list who would be happy to write code for you for a reasonable fee,
however.  Is that what you are looking for?

--Errin

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




RE: Use one option more than once with Getopts

2005-06-17 Thread Larsen, Errin M HMMA/IT
Larsen, Errin M HMMA/IT wrote:
> Hi Perl folks,
> 
>   Is there a way to use the Getopts modules to accept more than one
>   instance of a single option? Here is an example command line that I
> would like to see: 
> 
>   # mailfiles -f filename1 -f filename2 [EMAIL PROTECTED]
> [EMAIL PROTECTED] 
> 
>   I'm writing a script to send an email (using Mail::Sender) to all
> the addresses on the command line.  After the script pulls the
> addresses, it takes the final filename on the command line (or STDIN,
> if no filename   
> listed) and sends that as the message body.  I want to be able to add
> an attached file or files.  Any suggestions.  I could send my code
> here, if you'd like, but I really just need to know if I can use an
> option (like the above -f) more than once on the same command line.   
> 
> --Errin

Hello again, everyone.  I think I found it.  It looks like I can specify
an '@' at the end of an option in Getopts::Long to get an array of all
the values entered multiple times.

Cool.

Sorry to bother the list.  Next time I'll read more before I mail!

--Errin

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




Use one option more than once with Getopts

2005-06-17 Thread Larsen, Errin M HMMA/IT
Hi Perl folks,

  Is there a way to use the Getopts modules to accept more than one
instance of a single option?
  Here is an example command line that I would like to see:

  # mailfiles -f filename1 -f filename2 [EMAIL PROTECTED]
[EMAIL PROTECTED]

  I'm writing a script to send an email (using Mail::Sender) to all the
addresses on the command line.  After the script pulls the addresses, it
takes the final filename on the command line (or STDIN, if no filename
listed) and sends that as the message body.  I want to be able to add an
attached file or files.  Any suggestions.  I could send my code here, if
you'd like, but I really just need to know if I can use an option (like
the above -f) more than once on the same command line.

--Errin

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




Top Posting (was RE: host id)

2005-06-10 Thread Larsen, Errin M HMMA/IT

<< VERY fascinating discussions about the pros and cons of top-posting
SNIPPED>>

While I'm sure the arguments for and against the top-posting practices
of Internet email lists are wonderful, perhaps we can change the subject
of these emails.  I keep hoping someone will tell me more about the OP's
hostname questions and instead I read these (again, fascinating)
arguments instead.

--Errin


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




Using $# in a splice with split

2005-05-24 Thread Larsen, Errin M HMMA/IT
Hi Perl buddies,

  Can I do something like this:

 my $line = 'One Two Three Four Five Six';
 my( $first, $last ) = (split(' ', $line))[0,$#(split(' ', $line))];

This does not work.  What I want to do is to find the index of the last
element of a list created by split, and use it in a slice on the same
line.  Wow, that sounds garbled when I re-read it.  With the above
example, I want the two variables to contain the following:

  $first = 'One';
  $last = 'Six';

I realize I can do this in two steps:

  my $line = 'One Two Three Four Five Six';
  my @line = split(' ', $line);
  my($first, $last) = @line[0,$#line];

But I was wondering if it can be done on one line.

--Errin

PS    oops ... I just remembered negative indexing.  This works:

  my $line = 'One Two Three Four Five Six';
  my( $first, $last ) = (split(' ', $line )[0, -1];

We!

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




RE: Urgent Help with functions

2005-05-10 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Madhur Kashyap [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, May 10, 2005 11:34 AM
> To: beginners@perl.org
> Subject: Urgent Help with functions
> 
> 
> Can someone help me out with this simple code? It gives an 
> error saying:
> 
> Illegal division by zero at test.pl line 4.
> Exit 255
> 
> Code:
> -
> #!/usr/local/bin/perl
> sub test {
> =09my @[EMAIL PROTECTED];
> =09my $div=3D$pars[0]/$par[1];
> =09print "Pars were @{_}\n";
> }
> 
> test (1,2,3,4,5);
> 
> PS: I understand that there could be several workarounds to 
> write the same code but whatif I do want to divide one of the 
> local variable with one of the passed parameters? Its lot 
> confusing for me as I have a strong C/C++ background.
> 
> Another silly question ... is it possible to use the regular 
> expressions capability of perl and combine it with C++ codes 
> in some fashion. I mean is it possible to write programs 
> having both C++ and perl code snippets.
> 
> Regards
> Madhur Kashyap
> 
> -- 

I ran the following code:


#!/usr/bin/perl

use warnings;
use strict;
# Always use warnings and strict!  They are very helpful in finding
bugs!

sub test {
  my @pars = @_;
  my $div = $pars[0] / $pars[1];
  print "Pars were @_\n";
# I don't know why you put the '_' (underscore) in parens '(' ')' in
your code.  I took them out.
  print "Division was $div\n";
}

test (1,2,3,4,5);


This code ran fine on my machine.  I could not reproduce your divide by
zero error.  Are you sure you transcribed the function call exactly in
your original email?


I don't know about using the C++ code snippets.  I bet that if you show
us your c++ code snippets, we can show you how to do it in Perl,
though!!

--Errin 

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




RE: Fork - Process ID

2005-05-09 Thread Larsen, Errin M HMMA/IT


> -Original Message-
> From: Paul D. Kraus [mailto:[EMAIL PROTECTED] 
> Sent: Monday, May 09, 2005 9:19 AM
> To: Perl Beginners List
> Subject: Fork - Process ID
> 
> 
> Sceniro.
> I have many cron jobs that run perl scripts. These perl 
> scripts launch TbredBasic Applications. The problem I keep 
> finding these processes hung but I have noway of finding out 
> which program is actually causing the problem. When the 
> tbredbasic application launches the processes name is always BASIC.
> 
> I wanted to write a program that would write out the process 
> id and the perl program that spawned it to a file then 
> destroy that file when the program fails. This way I can just 
> look at the "log" and see that program X is what keeps hanging.
> 
> So I have been experminting with the fork command but I am 
> confused as to the out put I get. Below is the code and its 
> output follows. First please explain why the pid prints twice 
> and second let me know if there is a better way to achive 
> what I am trying to do.
> 
> TIA
> Paul
> 

Hi Paul.  Fork is fun to play with!

I've put some comments below:

> #!/bin/perl
> 
> use strict;
> use warnings;
> 
> defined( my $pid = fork ) or die "Cannot fork: $!";

  # right here, there are two processes running.  The first one, and the
one that got forked.
  # both processes have the $pid variable.  The original process should
receive the new processes
  # PID as the return value from fork, so it's $pid =  (or, whatever
the system returns).
  # The new process will receive a 0 (zero) from fork.  Now you can use
that $pid value to split
  # your code down two different paths.

> print "pid:$pid\n"

  # When I ran your code, the following was printed:
  #  pid: 1275
  #  pid: 0
  # Your output was not included in the email.  Was your output
different than this?

> unless( $pid ) {
>   exec "date";
>   die "cannot exec date: $!";
> }

  # This unless block should run date command if the process is the NEW
process.  The old process
  # with a 0 (zero) value for $pid, will skip the 'exec' and 'die' lines
of code.  Is that what you intended?



Here is a subroutine template I use in my daemon scripts, if it helps.
The 'setsid' function requires that you import the POSIX module
somewhere in your code:

use POSIX 'setsid';

sub daemonize {
  # A forked process usually doesn't want to be in some random
directory, so we change to root (/)
  chdir '/' or die "Can't chdir to /: $!";

  # The forked process doesn't want to read from standard-in, nor do we
want it to spam the 
  # standard-out with random messages.
  open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
  open STDOUT, '/dev/null' or die "Can't write to /dev/null: $!";

  # Here is where the actual fork takes place
  defined( my $pid = fork ) or die "Can't fork the daemon: $!";

  # Here the script that starts the forked process will end
  exit if $pid; # original (terminal) process dies here

  # This will give the forked process it's own session and make the
process it's own session leader
  setsid or die "Can't start a new session: $!";

  # finally, we'll duplicate standard-error to wherever standard-out is,
again to prevent spamming standard-out
  open STDERR, '>&STDOUT' or die "Can't dup STDOUT: $!";
}


Hope this helps you some.

--Errin



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




gcc on Solaris

2005-05-06 Thread Larsen, Errin M HMMA/IT
Hi Perl folks,

  I want to compile the latest version of Perl on my Solaris 9 server,
targeting that same Solaris 9 environment.  I have downloaded and
installed gcc (because, in it's infinite wisdom, Sun does not ship
Solaris with a pre-installed compiler?!) and intend to use it.  As a
test, I created the following file:

Helloworld.c:
Main() {
  printf( "Hellow World!\n" );
}

And I execute the following command line to test the gcc installation:

# gcc helloworld.c -o helloworld
ld.so.1: gcc: fatal: libiconv.so.2: open failed: No such file or
directory
Killed

So ... I know this is a Perl list, but I just don't know what's going on
here.  

I have another server that has the latest version of Perl and the latest
version of gcc and this command works on that server.  I'm using it to
compare and I can't seem to find the difference.  I see that I can
download and install a libiconv package from sunfreeware, but I don't
think that happened on the server that worked, so I don't want to do it
on the target server.

I guess I'm looking for some Sun Solaris knowledgable person to explain
to me the error above and how best (least intrusive/environment
changing) to fix it.

On other thing.  How can I (on the server that works) see where/what
library file is being used to compile.  What I mean is, if the server
that works does have a libiconv file, how can I find it.  I tried
searching for it, but no luck.  Is there some command line switch for
gcc that I can use to print out what librarys are being loaded?

--Errin

Btw, feel free to yell at me for off-topic (non-perl) posting.  Sorry.

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




RE: REGEXP removing - il- - -b-f and - il- - - - f

2005-04-29 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: John Doe [mailto:[EMAIL PROTECTED] 
> Sent: Friday, April 29, 2005 8:50 AM
> To: beginners@perl.org
> Subject: Re: REGEXP removing - il- - -b-f and - il- - - - f
> 
> 
> Am Freitag, 29. April 2005 14.43 schrieb [EMAIL PROTECTED]:
> > So which is safer more ideal to use :   || , or
> >
> >
> > Derek B. Smith
> > OhioHealth IT
> > UNIX / TSM / EDM Teams
> [...]
> 
> The only difference between "||" and "or" is the precedence, and the 
> precedence of "=" lies between them.
> 
> To my understanding, in the "assign or die" special case,
> 
>  my $a=do_something_which_can_fail() or handle_exception();



I'm sure I'll be corrected if I am wrong, but I believe that Perl sees
the above line of code like this:

  ( my $a = do_something_which_can_fail() ) or ( handle_exception() );

Because of the low precedence of the "or" operator.  Therefore, Perl
will try to assign the return value of "do_something_which_can_fail()"
subroutine to $a, and if it can't, THEN perl will execute the
"handle_exception()" subroutine.  This is probably what the coder
wanted.



> 
> is more logic than
> 
>  my $a=do_something_which_can_fail() || handle_exception();


Perl sees THIS line of code like this:

  my $a = ( ( do_something_which_can_fail() ) || ( handle_exception() )
);

Because of the high precedence of the "||" operator.  Therefore, perl
will run the "do_something which can fail()" subroutine, and if it
succeeds, assign the value returned to $a.  OTHERWISE, if it fails, the
return value from "handle_exception()" subroutine will be assigned to
$a.  This is probably NOT what you want to happen.


> 
> because something should be assigned to $a, and if that 
> fails, the app should 
> e.g. die. This way, the exception handling is not part of the 
> assignement.
> 
> On the other side, I would use 
> 
>  my $a=do_something_which_can_fail() || provide_some_default();
> 
> because the exception handling consists of providing a value.
> 
> Just my personal way to look at it :-)
> 
> joe
> 

--Errin Larsen

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




RE: Perl One-liner de-compile?

2005-04-25 Thread Larsen, Errin M HMMA/IT


> -Original Message-
> From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED] 
> Sent: Monday, April 25, 2005 2:42 PM
> To: Larsen, Errin M HMMA/IT
> Cc: beginners@perl.org
> Subject: RE: Perl One-liner de-compile?
> 
 <>
> 
> You're using a lowercase @f here.
> 
 <>
> 
> Perl will magically produce the @F array for you, as shown.  
> It's up to 
> YOU not to make the typo.
> 


  Ooops.  Hehe.

  Thx for the 2nd set of eyes, Jeff.

  --Errin

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




RE: Perl One-liner de-compile?

2005-04-25 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Paul Johnson [mailto:[EMAIL PROTECTED] 
> Sent: Monday, April 25, 2005 1:53 PM
> To: Larsen, Errin M HMMA/IT
> Cc: beginners@perl.org
> Subject: Re: Perl One-liner de-compile?
> 
> 
> On Mon, Apr 25, 2005 at 01:45:15PM -0500, Larsen, Errin M 
> HMMA/IT wrote:
> 
> > Hi everyone,
> > 
> >   Here is an example from the perlrun perldoc page:
> > 
> > perl -ane 'print pop(@F), "\n";'
> > 
> >   is equivalent to
> >  
> > while(<>) {
> > @F = split(' ');
> > print pop(@F), "\n";
> > }
> > 
> > 
> >   My question is, can I get Perl to evaluate a command line (like 
> > above) and print out the equivalent code that command line will 
> > produce?
> > 
> >   I hope that makes sense.  I thought I saw something 
> similar to this 
> > on this list before.
> 
> $ perl -MO=Deparse -ane 'print pop(@F), "\n";'
> 
> -- 
> Paul Johnson - [EMAIL PROTECTED]
> http://www.pjcj.net
> 


Ok ... So I tried this:

  # perl -MO=Deparse -nae 'print $f[4]' /some/directory/somefile
  LINE: while (defined($_ = )) {
  our(@F) = split(" ", $_, 0);
  print $f[4];
  }
  -e syntax OK

My question now is, where did the @f array come from?  I searched
through the perlvar perldoc page, but I only found an explanation for
the @F array.  Is this an example of Perl making a typo?  Or is the @f
array a secret array I'm not cleared to know about?

--Errin


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




Perl One-liner de-compile?

2005-04-25 Thread Larsen, Errin M HMMA/IT
Hi everyone,

  Here is an example from the perlrun perldoc page:

perl -ane 'print pop(@F), "\n";'

  is equivalent to
 
while(<>) {
@F = split(' ');
print pop(@F), "\n";
}


  My question is, can I get Perl to evaluate a command line (like above)
and print out the equivalent code that command line will produce?

  I hope that makes sense.  I thought I saw something similar to this on
this list before.

--Errin

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




Perl one-liner to print columns

2005-04-25 Thread Larsen, Errin M HMMA/IT
Hi Perlers,

  I typically will type the following to collect a specific column of
data from some command's output:

  # some_command | awk '{print $4}'


  I wanted to start using perl one-liners more often, so I thought I'd
try the column thing first.  This is what I came up with:

  # some_command | perl -pe '$_ = (split)[3] . "\n";'


  That feels/seems rather awkward.  Is there an easier, "cleaner" way of
doing this?

--Errin

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




RE: initialize arrays

2005-04-05 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, April 05, 2005 9:24 AM
> To: Charles K. Clarkson
> Cc: beginners@perl.org
> Subject: RE: initialize arrays
> 

<>

> [EMAIL PROTECTED]  wrote:
> 
> : Are these two statements the same?
> :
> : my (@array, @array1) = ( );
> :
> : my @array = my @array1 = ( );
> 
> According to Data::Dumper the results are the same.
> 
> print Dumper [EMAIL PROTECTED], [EMAIL PROTECTED];
> 
> 
> But then this has the same result.
> 
> my( @array, @array1 );
> 
> 
> Why are you trying to initialize perl arrays?
> 
> 
> Charles K. Clarkson
> --
> Mobile Homes Specialist
> 254 968-8328
> 

Hi everyone,

  this one made me wonder.  Isn't there a way to get Perl to print out
exactly how it evaluates the statements that are written?  If so, we
could use that to see what Perl does with those two different
assignments above.

--Errin

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




RE: Simplify perl -e '$a = [1,2,3,4,7]; print $a->[$#{@$a}]'

2005-03-10 Thread Larsen, Errin M HMMA/IT

  <>

> >  # or, since [EMAIL PROTECTED] will always be the index of the last 
> element of 
> > the
> > array:
> >  print $a->[-1]
> > 
> > 
> > Did I get it right?  That looks like homework to me ... Why 
> would you 
> > ever do that in a practical script?
> > 
> > --Errin
> > 
> 
> I think you got it. Ever want the last item in a list, I can think of 
> lots of reasons to want that...
> 
> http://danconia.org
> 

Yeah.  I agree.  However, I always use the $ARRAY[-1] (or,
$ARRAYREF->[-1]) syntax to get at the last element.

Why would I ever type: [EMAIL PROTECTED] ?!?

Btw ... What perldoc can I read to read about '$#'?

--Errin

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




RE: Simplify perl -e '$a = [1,2,3,4,7]; print $a->[$#{@$a}]'

2005-03-10 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, March 10, 2005 8:55 AM
> To: Marcos Rebelo
> Cc: Perl Beginners
> Subject: Re: Simplify perl -e '$a = [1,2,3,4,7]; print $a->[EMAIL PROTECTED]'
> 
> 
> Marcos Rebelo wrote:
> > This is correctly printing '7' but '$a->[EMAIL PROTECTED]' seems to be 
> > encripted code.
> > 
> > Can I write this in a cleaner way?
> > 
> > 
> 
> $a->[-1]; ???
> 
 Hi Wiggins,

  for those of us tryin' to keep up at home, can you walk us through
that bit a little?

  Here's what I spot:

  $a = [1,2,3,4,7] # this is initializing a scalar, $a, with a reference
to an array, [1,2,3,4,7]

 # $a-> this is dereferencing the array
 # as I understand it, and I really don't, the $#ARRAYNAME will give you
the number of elements, minus one, of an array?
 #  if that is the case, and then [EMAIL PROTECTED] ALSO derefernces the array, 
so
then
 #  [EMAIL PROTECTED] will be the number of elements in the array referenced by
$a, minus one (or, '4', in this example)

 # so
 print $a->[EMAIL PROTECTED]

 # is equivelant to
 print $a->[4]

 # or, since [EMAIL PROTECTED] will always be the index of the last element of 
the
array:
 print $a->[-1]


Did I get it right?  That looks like homework to me ... Why would you
ever do that in a practical script?

--Errin

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




RE: Defined revisited

2005-03-04 Thread Larsen, Errin M HMMA/IT
> Sorry to bother you, since I notice other people have also 
> been asking 
> about defined.  But the answers to their questions have not 
> helped me to 
> this point.

  No problem.  That's what we're here for.  As long as you've put in the
time to try to solve the problem yourself (which it looks like you've
done), you're not bothering anyone!

> 
> This morning I started seeing a vague, unhelpful warning 
> message about an 
> undefined variable from a program after months of successful 
> use.  The 
> warning occurs in a test line of the form
> 
> if ($xx > $yy) {
> 
> So I 
> 
>   1.  Checked the input data visually.
> 
>   2.  Checked the data again by going through the algorithm, 
>using the actual data in the input files.
> 
>   3.  Added print statements for $xx and $yy.
> 


  What did you see when you printed $yy?


> Finding nothing wrong, I created the following test program 
> containing the 
> essential logic, and with a variable deliberately undefined.
> 

  What are you really trying to fix.  Did the logic of your code break?
Or are you merely concerned with the warning message?  Perl is only
doing what you asked it to do.  By using the 'warnings' pragma, you've
specifically asked Perl to warn you when you use a variable that has not
been defined yet.  To remove the warning message, remove the "use
warnings;" line from your code!
  However, the warning is valuable.  You are probably assuming that $yy
== 0, which it most certainly is not.  It is "undefined".  If you want
your code to assume that this ($yy) variable is zero when it is
undefined, why not put in a test and change it to zero if it is
"undefined"?

  $yy = 0 unless( defined $yy );

> ==
> use strict;
> use warnings;
> use diagnostics;
>
> my $xx = 7;
> my $yy;
>
> if ($xx > $yy) {
> print "xx > yy, so do stuff\n";
> } else {
> print "Do not do stuff\n";
> }
> ==

  <>

> ==
> use strict;
> use warnings;
> use diagnostics;
> 
> my $xx = 7;
> my $yy;
> 
> if ((defined $yy) == 0) {


  by the way, this is easier to say like this:
   if (!defined $yy)
  or better yet:
   unless( defined $yy )


> print "\nyy is NOT defined, so Exit\n";
> exit;
> }
> 
> if ((defined $xx) == 0) {
> print "\nxx is NOT defined, so Exit\n";
> exit;
> }
> 
> if ($xx > $yy) {
> print "xx > yy, so do stuff\n";
> } else {
> print "Do not do stuff\n";
> }
> ==

> 
> In the test program, this fixed the problem, as did several 
> other versions 
> of the tests.  

  What problem was solved?  I'm confused here.

> So I added the tests to the real program, 
> intending to add 
> a bunch of print statements before "exit" so I could analyze the 
> underlying problem.  Unfortunately, the tests using "defined $yy" and 
> "defined $xx" failed in the real program:  absolutely no change in 
> behavior, except that the line number printed by the warning message 
> increased to allow for the test code.  I tried a whole bunch 
> of tests, and 
> every single one of them failed.
> 
> What have I missed?
> 
> Thanks,
> Walt
> 
> -- 

Hope to help! 

--Errin

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




RE: calling an invocation argument from @ARGV

2005-02-25 Thread Larsen, Errin M HMMA/IT

  <>

> > #!/bin/perl -w
> > use strict;
> 
>   ##  you should:
> use warnings;
>   ##  it will help you in the future
> 

<>

Oops!  I just realized your using that '-w' switch to perl up there.
That IS using the warnings pragma, I believe.  Sorry about that!

--Errin

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




RE: calling an invocation argument from @ARGV

2005-02-25 Thread Larsen, Errin M HMMA/IT

Hi!  And, welcome to Perl!

> 
> I'm trying to automate g++ through a Perl script. 
> Here is what I have written so far:
> 
> #!/bin/perl -w
> use strict;

  ##  you should:
use warnings;
  ##  it will help you in the future

> 
> my $counter;
> $counter = 0;

  ##  With Perl, it's not really necessary to initialize this variable.
It doesn't hurt, but it's just a line you didn't need to type.

> 
> for (my $i = 0; $i < @ARGV; $i++) {
> $counter++;
> }

  ##  This whole loop is not needed.  If you use the @ARGV variable in a
Scalar context, it will report how many elements it has
  my $number_of_args = @ARGV;

> 
> if ($counter == 0) {
> print "Not enough arguments!";
> print "Usage: ./cedit somefile.C++";
> print "Usage (optional): ./cedit somefile.C++
> outputfile";
> } elsif ($counter == 1) {
> system "g++", (first invocation argument); 
> 

  ##  This can be easily re-written to simplify and make it more
understandable:
  if( @ARGV ) {
## Do stuff here
  } else {
die "Not enough arguments!\nUsage: $0 somefile.C++\nUsage
(optional): $0 somefile.C++ outputfile";
  }

> How do I call individual arguments from the @ARGV
> array?  Basically, the usage of the script (called
> cedit) is:
> 
  
  The answer to this question is two-ways.
  1)  you can access the specific element in the array:
my $first_arg = $ARGV[0];
my $second_arg = $ARGV[1];

  2)  you can shift the args off the top of the array:
my $first_arg = shift;
my $seconf_arg = shift;

  There is a lot going on there.  There are also numerous other ways to
get at that info.  You should read up on some Perl!


> $./cedit somefile.C++
> 
> or
> 
> $./cedit somefile.C++ outputfile
> 
> The first argument is the .C++ file to be compiled,
> and the second argument is the name of the .exe file.
> 

Check out the Perl Documentation:

perldoc perldoc
perldoc -f shift

To be honest, I'm not sure where to send you to find out about Perl
arrays in general, or the special @ARGV array specifcally.

You can check out the perldoc FAQ for it:

perldoc -q array

--Errin

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




RE: Diamond Operator and Filenames

2005-02-18 Thread Larsen, Errin M HMMA/IT
> Larsen, Errin M HMMA/IT wrote:
> > Hi Perl-Crunchers,
> > 
> >   I've got the following code:
> > 

  <>

> > push @ARGV, @logs;
> > while( <> ) {
> > print "$filename:\n$_" if( /with errors/ );
> > }
> > 
> > Of course, my problem is that I'm not filling in $filename in that 
> > print statement with a value.  I'd really like to be able 
> to put the 
> > name of the file the diamond ('<>') operator is currently 
> parsing in 
> > there.  Is that possible?  Do I have access to the individual 
> > filenames AS they are being used in the while statement?
> > 
> > Thanks ahead of time,
> > 
> > --Errin
> 
>   I believe that $ARGV[0] has the file being processed. 
> So I think you could do something like:
> 
>   if ( $. == 1 ) {  # on the first read of the current file
>  print "Filename: " . $ARGV[0] . "\n";
>}
> 
> Wags ;)


Hi again,

  So, the <> operator shift()s the filenames off the top of @ARGV?
That's usefull knowledge.

  On a side note, out of curiosity, can I manipulate the @ARGV array
inside the while loop?  Or will that cause "unexpected behaviour"?

--Errin 

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




Diamond Operator and Filenames

2005-02-18 Thread Larsen, Errin M HMMA/IT
Hi Perl-Crunchers,

  I've got the following code:

#!/usr/bin/perl

use warnings;
use strict;

my $logdir = '/some/application/logs';
my @logs = <$logdir/*>;

push @ARGV, @logs;
while( <> ) {
print "$filename:\n$_" if( /with errors/ );
}

Of course, my problem is that I'm not filling in $filename in that print
statement with a value.  I'd really like to be able to put the name of
the file the diamond ('<>') operator is currently parsing in there.  Is
that possible?  Do I have access to the individual filenames AS they are
being used in the while statement?

Thanks ahead of time,

--Errin

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




RE: Example of hash and array movement correct?

2005-02-18 Thread Larsen, Errin M HMMA/IT

> Subject: Example of hash and array movement correct?
> 
> 
> 
>   Hello, 


  Hello


>   I am looking for ways to move data 
>   between array and hashes.
> 

  In all 3 of your examples, you don't show us the declaration of your
variables nor how they are filled with data.  I can make some
assumptions, though.


>   Does anybody has a better way or idea to do some of this:
>   
>   Example 1:
>   @indatas = $datas [0 .. 4];


  If I assume that the @datas variable is declared like this:
my @datas = qw( One Two Three Four Five Six Seven );

  then I can take a slice of that array to initiate the next array:
my @indatas = @datas[0 .. 4];

  I think in Example 1, you just need to reference the @datas array with
an '@' sign at the front (instead of a '$' sign) when you are using it
to create a slice.


>   Example 2:
>   $nkey = $dataln[3] . $dataln[4];


  If I assume that the @dataln variable is declared like this:
my @dataln = qw( A B C D E F G H );

  then I can easily create a scalar variable, $nkey, by concatanating
two of the elements of the @dataln array:
my $nkey = $dataln[3] . $dataln[4];

  It looks to me that you got this one right.  Is this what you were
asking?


>   Example 3:
>   $base{$bv[0]} = [ $bv[1], $bv[2], $bv[3], $bv[4] ];


  If I assume that the @bv array and the %base hash are declared like
this:
my @bv = qw( Alpha Bravo Charlie Delta Echo );
my %base;

  then I can initiate an element of that hash with a slice from the @bv
array:
$base{$bv[0]} = [EMAIL PROTECTED] .. 4]];

  Again, when taking a slice from an array, you need to reference the
array with the '@' sign.  Your example works, but you asked for
simpler/easier ways to do things.



  Were these examples helpful?  Maybe if you gave us a better idea of
what you wanted to do with what kind of data we could be more specific
helping you.

--Errin

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




RE: Loops & regexps

2005-02-10 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Larsen, Errin M HMMA/IT 
> Sent: Thursday, February 10, 2005 2:37 PM
> To: beginners@perl.org
> Subject: Loops & regexps
> 
> 
> Hi everyone,
> 
> I've got a command (if you're familiar with EMC and 
> PowerPath, you'll recognize the output) that outputs a long 
> list of information like this:
> 

  << example output snipped>>

> 
> That's the output from ONE device, there are sometimes hundreds of
> these.  What I'd like to do is transform this   into the following:
> 

  << desired output snipped>>

> 
> I wrote some code to attempt this, but I'm way off ... I know 
> there's a way to do this, it's just eluding me.  The code I 
> was trying out is below.  I wouldn't run it, it fails miserably:
> 

  << Horrible Code snipped>>

> Thanks ahead-of-time for any help!
> 
> --Errin
> 

Ok, I got it working.  This code only works if you assume a correct
order, but with the command I'm using that order does not change:

  #!/usr/bin/perl

  use warnings;
  use strict;

  my @powerout = `powermt display dev=all`;
  my $name;
  my @devices;

  foreach( @powerout ) {
$name = $1 if( /Pseudo.+(emc.+)$/ );
push @devices, "$name -> $1" if( /(c\dt.+)s\d/ ); 
  }

  print "$_\n" foreach @devices

It turns out it was lot simpler than I was thinking.

--Errin

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




Loops & regexps

2005-02-10 Thread Larsen, Errin M HMMA/IT
Hi everyone,

I've got a command (if you're familiar with EMC and PowerPath, you'll
recognize the output) that outputs a long list of information like this:

  # powermt display dev=all
  Pseudo name=emcpower0a
  Symmetrix ID=000187720658
  Logical device ID=00AD
  state=alive; policy=SymmOpt; priority=0; queued-IOs=0
 

==
   Host ---   - Stor -   -- I/O Path -  --
Stats ---
  ### HW Path I/O PathsInterf.   ModeState
Q-IOs Errors
 

==
  3072 [EMAIL PROTECTED]/SUNW,[EMAIL PROTECTED] 
c5t50060482CC18F487d109s0 FA  8aA
active  alive  0  0
  3073 [EMAIL PROTECTED]/SUNW,[EMAIL PROTECTED] 
c6t50060482CC18F497d109s0 FA  8bA
active  alive  0  0
  3074 [EMAIL PROTECTED]/SUNW,[EMAIL PROTECTED] 
c7t50060482CC18F488d109s0 FA  9aA
active  alive  0  0
  3075 [EMAIL PROTECTED]/SUNW,[EMAIL PROTECTED] 
c8t50060482CC18F498d109s0 FA  9bA
active  alive  0  0

That's the output from ONE device, there are sometimes hundreds of
these.  What I'd like to do is transform this   into the following:

  emcpower0a -> c5t50060482CC18F487d109
  emcpower0a -> c6t50060482CC18F497d109
  emcpower0a -> c7t50060482CC18F487d109
  emcpower0a -> c8t50060482CC18F497d109

I wrote some code to attempt this, but I'm way off ... I know there's a
way to do this, it's just eluding me.  The code I was trying out is
below.  I wouldn't run it, it fails miserably:

  #!/usr/bin/perl

  use warnings;
  use strict;

  my @powerout = `powermt display dev=all`;
  my $name;
  my @device;

  foreach( @powerout ) {
if( /Pseudo name/ .. /^$/ ){
  $name = $1 if /Pseudo name.*(emc.*)$/;
  push @device, $1 if /(c\dt.+)s\d/;
}

print "$name = $_\n" foreach( @device );
$name = undef;
@device = undef;
  }

Thanks ahead-of-time for any help!

--Errin

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




RE: Variable-sized hash of booleans

2005-02-04 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Chris Charley [mailto:[EMAIL PROTECTED] 
> Sent: Friday, February 04, 2005 10:45 AM
> To: beginners@perl.org
> Subject: Re: Variable-sized hash of booleans
> 
> 
> > You can use grep.
> >
> > my %hash = (ONE => 1, TWO => 0, THREE => 1);
> >
> > if (grep {! $hash{$_}} keys %hash) {
> > print "false\n";
> > }
> > else {
> > print "true\n";
> > }
> >
> > Prints 'false'.
> 
> Guess it would be helpful to explain how grep works here. 
> From the perlfunc 
> man page:
> Evaluates the BLOCK or EXPR for each element of LIST (locally 
> setting $_ to 
> each element) and returns the list value consisting of those 
> elements for 
> which the expression evaluated to true. In scalar context, 
> returns the 
> number of times the expression was true.
> 
> In the code above, ! $hash{$_} evals to true when $hash{TWO} 
> is evaluated, 
> so grep, being in scalar context, would return 1, the number 
> of elements in 
> %hash which evaluated to 'true '.If all values were 'true', 
> (1's), then grep 
> would return 0 since !$hash{$_} would be false for every 
> element in that 
> case.
> 
> Chris
> 

Oh!

I got my true/false 's backwards cause of the '!' in there.  Thanks for
the explanation Chris.

--Errin

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




RE: Variable-sized hash of booleans

2005-02-04 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Chris Charley [mailto:[EMAIL PROTECTED] 
> Sent: Friday, February 04, 2005 10:21 AM
> To: beginners@perl.org
> Subject: Re: Variable-sized hash of booleans
> 
> 
> You can use grep.
> 
> my %hash = (ONE => 1, TWO => 0, THREE => 1);
> 
> if (grep {! $hash{$_}} keys %hash) {
>  print "false\n";
> }
> else {
>  print "true\n";
> }
> 
> Prints 'false'.
> 
> Chris 
> 

Thanks for the prompt reply, Chris

My mind has the same problem with grep that it does with map.  I don't
know why.  Maybe it's just doing too much at once for me to wrap my
brain around.  What exactly does that grep statement do?  I've read the
perldoc on it, and I still can't grasp it.  Let me try restating what it
does:  grep will iterate through each item in the list, returning a new
list composed of items that the block (or expression) returns true.  Is
that right?  So:
  
  my @new_list = grep {1} @orig_list;

Would set @new_list = @orig_list, and:
  
  my @new_list = grep {0} @orig_list;

Would set @new_list = empty.  Right?  And, in the block (or expression),
$_ will interpolate to each item in the @orig_list.  Have I got it
right?  Also, used in a scalar context, it's gonna give me the number of
returned items, right?  So in your example above, it will break when
there are 2 false values in the original hash?

Nope.  I tried it and it still works with more than one false value.

Can you help me understand what that grep is doing?

--Errin

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




Variable-sized hash of booleans

2005-02-04 Thread Larsen, Errin M HMMA/IT
Hi everyone,

  As always, we'll start with some necessary code:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

  Let's say I have a hash, but I don't know at compile time how many
keys it has in it:

my %things;
die "You have an odd number of arguments.\n" if @ARGV % 2;
$things{shift()} = shift() while( @ARGV );

  And furthermore, I will assume that the values in the hash are boolean
(i.e. '1' (ones) or '0' (zeros)).  Now, I want to see if the WHOLE hash
is true, this being defined as every key having a value of '1'.  If even
one key has a value of '0', I want it to be false.  I decided I could do
this by counting 'true' values:

my $num_things = keys %things;
my $true_things = 0;

foreach( keys %things ) {
  $true_things++ if $things{$_};
}

if( $true_things == $num_things ) {
  print "The \%things hash is true\n";
} else {
  print "The \$things hash is false\n";
}

  But ... This seems less than elegant.  Is there an easier way, perhaps
with map somehow?  I'm not very good at looking at problems and seeing
ways that map can help me.

  With this last bit of code I can check the validity of the true/false
test:

print Dumper( \%things );

  The whole code together is here:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my %things;
die "You have an odd number of arguments.\n" if @ARGV % 2;
$things{shift()} = shift() while( @ARGV );

my $num_things = keys %things;
my $true_things = 0;

foreach( keys %things ) {
  $true_things++ if $things{$_};
}

if( $true_things == $num_things ) {
  print "The \%things hash is true\n";
} else {
  print "The \$things hash is false\n";
}

Print Dumper( \%things );


  Now, my output looks like this:

# things_test 1 One 1 Two 1 Three
The %things hash is true
$VAR1 = {
  'Three' => '1',
  'Two' => '1',
  'One' => '1'
};

  Or ... A false one:

# things_test 1 One 0 Two 1 Three
The %things hash is false
$VAR1 = {
  'Three' => '1',
  'Two' => '0',
  'One' => '1'
};

Thanks in advance for any help!

--Errin

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




RE: Perl module to compare images?

2004-11-24 Thread Larsen, Errin M HMMA/IT
Hi Brian,

I don't have an answer to your question.  But ...

Did you know that O'reilly has a whole book to answer questions like
these?  Check out:
http://www.oreilly.com/catalog/perlgp/

--Errin


> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, November 24, 2004 1:41 PM
> To: [EMAIL PROTECTED]
> Subject: Perl module to compare images?
> 
> 
> Hi all,
>  
> Is there a module available to compare two images to detect 
> differences between the two, i.e. motion? Searched CPAN to no avail.
>  
> Thanks in advance!
> -Brian
> 

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




RE: How to monitor a scritp

2004-11-23 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Mauro [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, November 23, 2004 3:44 AM
> To: [EMAIL PROTECTED]
> Subject: How to monitor a scritp
> 
> 
> Hi all,
> I made a script that works fine.
> When a try to run it in crontab it seems it doesn't work.
> It makes an empty output log.
> I checked for correct path in external command I run into 
> script because I know crontab hasn't got any environment 
> variable but pheraps I forgot to check something... How do 
> you suggest me to monitor what it is happening? Does Perl 
> need any explicit environment variables to run in crontab?
> 
> 
> Thank you 
> 

Can you show us the meat of your script?  Also, the crontab line your
using?  Maybe we could help you out a little better with that info.

--Errin

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




RE: why $000 is a valid variable name?

2004-11-18 Thread Larsen, Errin M HMMA/IT

> > Zeng Nan [ZN], on Thursday, November 18, 2004 at 13:43 
> (+0800) wrote 
> > these comments:
> > 
> > ZN> As said in "Learning Perl", a perl identifier is "a letter or 
> > ZN> underscore, and then possibly more letters, or digits, or
> underscores".
> > ZN> Because of this, $123 is an invalid name, but why $000 
> or $00
> works?


  I tried the following code:
#!/usr/bin/perl

use warnings;
use strict;

my $00 = "Two Zeros";
my $000 = "Three Zeros";
my $ = "Four Zeros";

print "\$0= $0\n";
print "\$00   = $00\n";
print "\$000  = $000\n";
print "\$ = $\n";


  And I got the following output:
Can't use global $00 in "my" at ./zerotest line 6, near "my $00 "
Can't use global $000 in "my" at ./zerotest line 7, near "my $000 "
Can't use global $ in "my" at ./zerotest line 8, near "my $ "
Execution of ./zerotest aborted due to compilation errors.


  BUT ... When I removed the "warnings" and "strict", and stopped using
"my", it works:

#!/usr/bin/perl

$00 = "Two Zeros";
$000 = "Three Zeros";
$ = "Four Zeros";

print "\$0= $0\n";
print "\$00   = $00\n";
print "\$000  = $000\n";
print "\$ = $\n";


  The above code produced the following output:
$0= ./zerotest
$00   = Two Zeros
$000  = Three Zeros
$ = Four Zeros

So ... Um, weird!

--Errin

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




RE: RE : start http request and move on

2004-11-05 Thread Larsen, Errin M HMMA/IT

> -Original Message-
> From: JupiterHost.Net [mailto:[EMAIL PROTECTED] 
> Sent: Friday, November 05, 2004 3:08 PM
> To: [EMAIL PROTECTED]
> Cc: Jose Nyimi
> Subject: Re: RE : start http request and move on
> 
> 
> 
> > Nice approach, I have learned today an easy to do it :) Though care 
> > should be taken to not fork many *uncontrolled* childs.
> 
> Could you elaborate what uncontrolled children are 
> specifically and what 
> should be avoided?
> (In context of this thread of course not the little humans running 
> around like crazy ;p)
> 
> > Rgds,
> > José.
> 
> -- 

Hi,

I've been lurking on this thread for a bit, and now that you've jumped to children and 
fork and related topics I'll chime in!

I found a lot of useful information about this sort of thing in "perldoc perlipc".  
Check out the stuff about Daemons and the REAPER subroutine in that doc.  Also, in the 
cookbook (I think! I don't have it with me to check) there is some good stuff about 
daemons and children.

More specifically to the questions above, two things to be careful of:

First, always, ALWAYS check (then, check again!) that you are not going to fork-bomb 
your system.  If your script, forks (now you have 2), then both of those fork again, 
'cause you have a bug in a loop somewhere (now you have 4) and so on (now there are 8) 
and so on (16 ... Can you see where this is goin?!), you will quickly bring your box 
to a screaching halt!

Second, (I'm assuming this is a UNIX-ey OS) make sure you read up on setsid and 
sys_wait_h in the POSIX (perldoc POSIX) module, as well as the builtin 'waitpid' 
(perldoc -f waitpid).  You might have to 'man' the equivelant to get meaningful 
documentation (like, 'man setsid').

When I fork a process, I like to make sure the child at least does the following: 
- chdir /
   (it's rude for a random process to sit on a filesystem that might need to be 
unmounted)
- change the stdin, stdout and stderr
   (also rude to spurt random nonsense from a random process onto the terminal or 
somesuch)
- setsid
   (So the child can own it's own session)

Like this:

sub daemonize {
chdir '/'   or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '/dev/null'or die "Can't write to /dev/null: $!";

defined( my $pid = fork )   or die "Can't fork the child: $!";

   #The exit below can be replaced with an if-else construct if you want the 
original to keep going.
   #I believe this has been handled in this thread already!
exit if $pid;   # original process dies here
setsid  or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't duplicate STDOUT: $!";
}

I hope this helps a bit.  Let me know if ya got more questions,

--Errin

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




RE: Need help with regex

2004-11-04 Thread Larsen, Errin M HMMA/IT
> -Original Message-
> From: Kevin Old [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, November 04, 2004 9:38 AM
> To: [EMAIL PROTECTED]
> Subject: Need help with regex
> 
> 
> Hello everyone,
> 

  Hi!


> I have a line like the following:
> 
> my $line = "31232000 07/28/04 DUC000 NET 60 DAYS  RD64
>UPSGNDSVR   PREPAID";
> 
> What I'm looking for in lines like this are the customer number
> (DUC000) and a "code" that starts with UPS, or if the code 
> doesn't start with UPS idealy I'd like to have whatever was 
> in that place returned, 

  <>

  Does the information in your "line" above always have the same
information in the same fields?

  For example, is the Customer number always the 3rd string of
information in your line?

  If so, then you can grab your stuff without matching with a nice
split!

my( $customer, $code ) = ( split ' ', $line )[2, 8];


  I hope that helps,

  --Errin

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




RE: Sourcing Configuration files

2004-11-03 Thread Larsen, Errin M HMMA/IT
-Original Message-
From: Gavin Henry [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, November 03, 2004 4:15 AM
To: Perl Beginners List
Subject: Re: Sourcing Configuration files

Chris Devers said:
>> On Tue, 2 Nov 2004, Gavin Henry wrote:
>>
>>> What is the easiest way to move variable declarations out into a
file
>>> in /etc/ and requiring a perl program to read them in at startup. If
>>> they are not there, then the program must complain.
>>
>> Have you considered using Tie::File, FreezeThaw or Data::Dumper?
>>
>> 
>> 
>>


> Thanks I will try them. I think it's a bit weird I can't do this out
of
> the box. Does anyone else?


Hi Gavin,

Out-of-the-box configuration files, as you say, can be done as the
following simple code demonstrates:

--configtest.pl--
-
#!/usr/bin/perl
use warnings;
use strict;

get_config();

print "FOO_VALUE = $Config::FOO_VALUE\n";
print "BAR_VALUE = $Config::BAR_VALUE\n";

sub get_config{
package Config;
our $FOO_VALUE;
our $BAR_VALUE;
do '/etc/my_values.conf'
}


--/etc/my_values.conf--
---
$FOO_VALUE = "This is the value of foo";
$BAR_VALUE = "This is the value of bar";


I hope that helps.  That 'do' statement is the key.  If you tell perl to
'do' a filename, it will parse it, at run-time, just like any other
code.  I use that sort of thing all the time.  It is not necessary to
declare a package like I did in the above code, it just looks prettier
to me, and it makes sure that if anyone else (later) uses your config
file, they don't have to worry about colliding variable names in their
namespace.

HTH
--Errin

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