Re: Need an explanation

2005-08-26 Thread John W. Krahn
Binish A R wrote:
> I've a file, which has entries like the following ...
> 
> IDENTIFIER1=value1;
> IDENTIFIER2=value2;
> IDENTIFIER3=value3;
> 
> etc
> 
> I've got to parse the above file and am using a hash to do the same ...

Have you thought about using one of the many modules on CPAN that does this
for you?


> Here is my code ...
> 
> 
> while (<>) {
>   chomp;
> next if /^#/;
> next if /^$/;
> %CONF = split /=/;
> }
> 
> 
> But %CONF contains only the last key/value pair :-/

That is because the contents of the hash %CONF are being replaced by the
assignment to the hash.


> So I had to modify the above script to 
> 
> while (<>) {
>   chomp;
>   next if /^#/;
> next if /^$/;
> $ref = [ split /=/ ];
>   $CONF{$ref->[0]} = $ref->[1];
> }
> 
> The above is working fine.

That is because it is the correct way to ADD data to a hash.


> So my question is why isn't the first code working?
> I don't want to use too many variables in my script, 
> even with my second script I needed an extra variable viz $ref.

If you make the results from split() local to the while loop using my() then
they will not affect the number of variables outside the while loop.

my ( $key, $value ) = split /=/;
$CONF{ $key } = $value;
}
# End of loop so $key and $value are no longer in scope.


> Is it possible to get the job done using the first script?

No, however you can do it by reading the whole file into a list and processing
that:

my %CONF = map split( /=/ ), grep !/^$/, grep !/^#/, map { chomp; $_ } <>;



P.S.  Could you please not send graphics files to the list?  TIA


John
-- 
use Perl;
program
fulfillment

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




Re: System, shell question

2005-08-26 Thread John W. Krahn
steve tran wrote:
> Hello 

Hello,

> I am a beginner to perl and I have a question
> 
> if I want to use system or open or backticks to
> execute a system command, 
> like system("$cmd arg1"); ( with system for instance) 
> 
> and the $cmd which is a prog takes another program
> called in arg1 , and that program(arg1) requires
> STDIN, 
> and I need to redirect the output from $cmd into a
> file 
> how can I make both happen ? 
> 
> if I do system("$cmd arg1"); 
> I can see my program waiting for input in STDOUT,but I
> cant capture the STDOUT to proces later but when I add
> system("$cmd arg1 >> out"); 
>  
> the program waits but the output from arg1 goes to
> out. 
> Is there an easy way to accomplish both.

The same way that you would do it on the command line:

system( "$cmd < arg1 >> out" );



John
-- 
use Perl;
program
fulfillment

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




Re: the time a program runs

2005-08-26 Thread John W. Krahn
Octavian Rasnita wrote:
> Hi,

Hello,

> I have tried to find out the time a perl program runs, and I have used:
> 
> #at the start of the program:
> my $begin = (times)[0];
> my $begin_t = time();
> 
> ... The program follows
> 
> # at the end of the program:
> my $end = (times)[0] - $begin;
> my $end_t = time() - $begin_t;
> print "end: $end\nEnd_t: $end_t\n";
> 
> After running the program, it prints:
> 
> end: 4.953
> End_t: 19
> 
> Why does this difference appear?

time() is based on the time of your computers clock and it should change the
same as the clock on your wall or the watch on your wrist.  times() is based
on the amount of time that that particular process runs on a particular CPU on
your operating system while it is also running dozens of other processes.

Note that times() in scalar context returns the same value as (times)[0] so
the list slice is not really required.


John
-- 
use Perl;
program
fulfillment

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




Re: Dot and Dubble Dot directory names

2005-08-26 Thread JupiterHost.Net

In a program such as the following
-
#!/usr/local/bin/perl -w



# always always always use strict and warnings!!!
use strict;
use warnings;
use File::Spec;


$windir = "/home/users/tony";


my $windir = '/home/users/tony';


opendir(NT, $windir) || die "no $windir?: $!";


"or die" not "|| die"


while ($name = readdir(NT)) { # scalar context, one per loop


while(my $name...

or better yet:

while(readir NT) {
   my $abs = File::Spec->rel2abs($_);
   print "$abs\n";
}


   print "$name\n"; # prints ., .., system.ini, and so on
}
closedir(NT);
exit;


exit not needed


---
$name returns each of the files in the given directory. To form the 
absolute path of the files given by $name, you would simply combine the 
$windir and the $name variables like so: "$windir/$name". For example 
"/home/users/tony/x.txt".


My question relates to the two files '.' and '..'. Is there any function 
that will give the absolute path of these directories when fed $name 
values of '.' and '..' ?  Without having to chdir to these directories, 
that is.


perldoc -f File::Spec

HTH :)

Lee.M - JupiterHost.Net

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




Dot and Dubble Dot directory names

2005-08-26 Thread Tony Frasketi


In a program such as the following
-
#!/usr/local/bin/perl -w

$windir = "/home/users/tony";
opendir(NT, $windir) || die "no $windir?: $!";
while ($name = readdir(NT)) { # scalar context, one per loop
   print "$name\n"; # prints ., .., system.ini, and so on
}
closedir(NT);
exit;
---
$name returns each of the files in the given directory. To form the 
absolute path of the files given by $name, you would simply combine the 
$windir and the $name variables like so: "$windir/$name". For example 
"/home/users/tony/x.txt".


My question relates to the two files '.' and '..'. Is there any function 
that will give the absolute path of these directories when fed $name 
values of '.' and '..' ?  Without having to chdir to these directories, 
that is.


I hope I've posed my question so as to be understandable...
I've looked online and in the O'reilly books but can't find the answer.

TIA
Tony Frasketi


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




Re: Directories into arrays, again.

2005-08-26 Thread JupiterHost.Net


Then that would be the code to show :) although I bet 
strict/warnings/and checking your open's (or die $!;) will show you what


is wrong :) <<<


I see, however that is still the best practice to be in, especially when 
asking for assistance since it catches 99.9% of little issues.



No, the .bak files are there. You can see them when you look at the
directory in Windows Explorer, or do a DIR from a command prompt. They
just aren't visible to the Perl script. 


Ok, that woudl be a good thign to explain innitially, sorry if you did 
and I missed it :)


Or weren't. 


So its working  now then?

What changed?

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




RE: Need a list of files in a dir.

2005-08-26 Thread Daniel Kurtz
From: Bob Showalter [mailto:[EMAIL PROTECTED] 

>>> OK, you weren't kidding. Since you're new to Perl, you get a free
pass :~) <<<

I appreciate the indulgence .

>>> Seriously, though, not all of us run Perl on Windows. Your approach
is 
Windows-specific. <<<

True, I hadn't thought of that. I'm primarily a Visual Studio person,
and the biggest point of platform variation we need to worry about in
our discussion forums is usually "Are you using Service Pack 4 or
Service Pack 5?"

>>> In addition, the "dir" command outputs a bunch of other 
stuff besides the file name, so you'd have to do some parsing to get the

file names. <<<

Actually, use the /b switch and you do get just the file names. I agree
now that Perl gives you easier ways to do it (actually, I pretty much
assumed that was the case all along, I just hadn't had time yet to find
out what those other 'proper' ways were.)

daniel

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




Re: Need a list of files in a dir.

2005-08-26 Thread Bob Showalter

Daniel Kurtz wrote:

Bob Showalter wrote:


Please tell me you're kidding.


Why? It works. The question asked how you can do it, not the BEST way
to do it. And after a week of Perling, this was the one way I knew.
Now I know two ways. 


OK, you weren't kidding. Since you're new to Perl, you get a free pass :~)

Seriously, though, not all of us run Perl on Windows. Your approach is 
Windows-specific. In addition, the "dir" command outputs a bunch of other 
stuff besides the file name, so you'd have to do some parsing to get the 
file names.


Either a glob approach or an opendir/readdir approach is simpler and more 
portable if one just needs a list of file names. If more details are needed, 
the stat() function (or related operators) can be applied to the resulting 
list. 



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




RE: Need a list of files in a dir.

2005-08-26 Thread Ryan Frantz
> -Original Message-
> From: Daniel Kurtz [mailto:[EMAIL PROTECTED]
> Sent: Friday, August 26, 2005 3:49 PM
> To: beginners@perl.org
> Subject: RE: Need a list of files in a dir.
> 
> From: Randal L. Schwartz [mailto:[EMAIL PROTECTED]
> >>> Daniel> Why? It works.
> 
> Not on Unix it doesn't.  And there are far more Unix installations of
> Perl than Windows installations of Perl. <<<
> 
> Good point.
> 
> Although... while I'm certain that a higher PERCENTAGE of Unix systems
> have Perl than Windows systems do, are we certain that translates to a
> higher NUMBER of systems? Not that I'm trying to start an OS/religious
> flamewar. Now that I think about it, I just find it an interesting
> question.

I don't think that Randal was arguing that there are more UNIX vs.
Windows systems as much as he was stating that given a number of UNIX
systems vs. Windows systems, more UNIX systems have Perl
implementations.

> 
> daniel
> 
> --
> 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: map/grep and arrays

2005-08-26 Thread Tom Allison

>print join( ',', map { s/[\r\n]+//g; $_ } @row ), "\n";

bingo!

I wasn't doing the  ;$_ thingy in map.
Putting the $_ at the end of the block isn't very intuitive, but it's
"like" a 'return $_' call in a function.

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




RE: Need a list of files in a dir.

2005-08-26 Thread Daniel Kurtz
From: Randal L. Schwartz [mailto:[EMAIL PROTECTED] 
>>> Daniel> Why? It works.

Not on Unix it doesn't.  And there are far more Unix installations of
Perl than Windows installations of Perl. <<<

Good point.

Although... while I'm certain that a higher PERCENTAGE of Unix systems
have Perl than Windows systems do, are we certain that translates to a
higher NUMBER of systems? Not that I'm trying to start an OS/religious
flamewar. Now that I think about it, I just find it an interesting
question.

daniel

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




RE: Directories into arrays, again.

2005-08-26 Thread Daniel Kurtz
From: JupiterHost.Net [mailto:[EMAIL PROTECTED] 
> # Execute a command that backs up every file in the directory # with a

> .bak extension.

>>> Um, ok so you're wondering why there are no .bak files?

Then that would be the code to show :) although I bet 
strict/warnings/and checking your open's (or die $!;) will show you what

is wrong :) <<<

No, the .bak files are there. You can see them when you look at the
directory in Windows Explorer, or do a DIR from a command prompt. They
just aren't visible to the Perl script. Or weren't. 

daniel

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




RE: Directories into arrays, again.

2005-08-26 Thread Daniel Kurtz
Excellent points, all

From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED] 
>>> And a reason why using a shell command to copy a file is a bad idea.
<<<

Well, the 'shell' and 'copy' association in this case is actually
something of a red herring. The point of this particular script is not
to copy files, it is to illustrate to a non-programmer an easy and dirty
way of executing a program and then capturing some 'results' for
analysis. In this case, the file copying batch program was used simply
because it produces readily verifiable results (either the .bak files
are there, one for each original file specified, or they're not.)

>>>opendir DIR2, '.' or die "Can't open directory for reading a second
time: $!";
my @after = readdir DIR2;
closedir DIR2; <<<

Well, I boiled the script down to essentials before posting it,
including taking out some error handling (not on these particular
commands though.) However, in this particular instance, there were no
errors to trap. The *dirs worked correctly, they just weren't picking up
the changes made to the directory because those changes were not visible
to Perl until after the file handle to the shell was closed or otherwise
reset.

>>> As an aside, if you must use a shell command you are better off
sticking with system, until you need the output which is provided by
backticks, only use the piped open form when you need to communicate
with the command you are running. <<<

I'm certain you're right. As it happens, in this particular instance the
script IS piping additional messages to the shell. Is open() the only
means of doing that?

daniel

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




Re: Need a list of files in a dir.

2005-08-26 Thread Randal L. Schwartz
> "Daniel" == Daniel Kurtz <[EMAIL PROTECTED]> writes:

Daniel> Why? It works.

Not on Unix it doesn't.  And there are far more Unix installations of
Perl than Windows installations of Perl.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
 http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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




Re: Access denied

2005-08-26 Thread Kris Van Bruwaene
On 8/25/05, Karyn Williams <[EMAIL PROTECTED]> wrote:
> At 11:29 AM 8/25/05 +0200, you wrote:
> >On 8/24/05, Karyn Williams <[EMAIL PROTECTED]> wrote:
> Try changing your shell to /bin/sh and then try running the script from the
> command line.
That won't help, my sh is a symlink to bash.
> >> I can not run shell.
> >>
> >> "bash: ./configure: /bin/sh: bad interpreter: Permission denied"
> >>
> >> Zeus
> 
> >That typically comes when trying to execute on non Linux filesystems.
> >Don't waste time and try executing on FAT partitions for example. Always
> >use *NIX filesystems like ext2/3, reiserfs, xfs.
Not entirely true really, it's a mount option, I just tried it like:
mount -o exec,uid=kris -t vfat /dev/fd0 /floppy
and the script executes all right (with another PC, I didn't try yet
on the one with the problem).
Anyway, the partition with the "Access denied problem" is reiserfs
(v3), so that doesn't explain anything. I think I'll give up and just
reinstall Knoppix on that PC next monday, something 's definitely
screwed up. Unless you have an idea to debug it further (strace, delve
into the source of bash or anything?). I'm not a C programmer tho.

Thanks for your time.

Kris

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




Re: Directories into arrays, again.

2005-08-26 Thread JupiterHost.Net



Daniel Kurtz wrote:


Sorry, obviously the code is supposed to read:


Actually it should read:

use strict;
use warnings;


opendir( DIR1, ".");


opendir DIR1, '.' or die "Could not open .: $!";


@before = readdir(DIR1);


my @before = readdir DIR1;


closedir(DIR1);

# Execute a command that backs up every file in the directory
# with a .bak extension.


Um, ok so you're wondering why there are no .bak files?

Then that would be the code to show :) although I bet 
strict/warnings/and checking your open's (or die $!;) will show you what 
is wrong :)


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




Re: odd benchmark result, map vs foreach

2005-08-26 Thread Wiggins d'Anconia
Scott R. Godin wrote:
> Wiggins d'Anconia wrote:
> 
>> Your benchmark isn't controlled. In the first instance you are doing a
>> ++ on what amounts to a scalar getting autovivified, in the second
>> instance you are assigning a pair of values to a list then autovivifying
>> it. It isn't necessarily the map vs. foreach that is causing your
>> difference. I think for your benchmark to be accurate you are going to
>> have to control the actions taken in the loop, otherwise they affect the
>> accuracy of your benchmark.
> 
> 
> mm this may be true, but realistically, that is how I'd write them in
> code I actually use -- so it behooves me to test them the way I'd
> normally be using them.. not under artificial circumstances in ways I
> normally wouldn't write code.
> 
> I see what you're saying and it makes sense, but in this instance I'm
> more interested in seeing the results of the idiomatic expression of the
> end result. :)
> 

Right, I was just being strict about your wording. You declared that the
foreach was faster than map, but strictly speaking it should have been
"the way I used foreach was faster than the way I used map", which I
wouldn't have argued with. And I am only being that strict because this
is an open, archived forum where there are plenty of people that
wouldn't have recognized the difference and may have taken you as
strictly as I did on purpose, just because they didn't know better.

As far as I am concerned write your code however you wish, make it the
fastest to read...

http://danconia.org

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




Re: Directories into arrays, again.

2005-08-26 Thread Wiggins d'Anconia
Daniel Kurtz wrote:
> From: Daniel Kurtz [mailto:[EMAIL PROTECTED] 
> 
Why does @after end up looking exactly like @before? Is there some
> 
> buffering going on here? And if so, how do I clear it? <<<
> 
> Never mind, I figured it out. The file copying operation is another
> shell operation (c'mon, I'm a newby, and I've only read as far as
> perlopentut!) and the file handle needs to be closed before the second
> opendir().
> 
> daniel
> 

Ok, but that is why you are here to learn. And a reason why using a
shell command to copy a file is a bad idea. You didn't do any error
checking, so you didn't know where your problem was.

I assume you are using 'strict' and 'warnings'. If you aren't, you need
to be, and want to be. If you are reading a book, and it hasn't advised
you to do so, get a new book, most likely the Llama. If you are reading
the perldocs directly, then I would suggest getting the Llama as it will
make for much better use of your time.

opendir DIR1, '.' or die "Can't open directory for reading: $!";

In the above, you don't need the extra parens, there is no need to use
double quotes unless you are using interpolation and you weren't, so I
switched those to singles. And any operation that could fail should be
checked for failure, in the above case opendir will return nothing if it
fails, so we tack on a message, we include $! to learn *why* it failed.

my @before = readdir DIR1;

Declare our variables with the proper scope.

closedir DIR1;

# Execute a command that backs up every file in the directory
# with a .bak extension.

Here I would suggest using the File::Copy module, as it provides a
convenient 'copy' function which is all you are really doing. It is also
designed to be as portable as possible, and happens to be standard on
newer Perl versions.

use File::Copy;
foreach my $file (@before) {
  if (-f $file) {
copy($file, "$file\.bak") or die "Can't backup file: $!";
  }
}

opendir DIR2, '.' or die "Can't open directory for reading a second
time: $!";
my @after = readdir DIR2;
closedir DIR2;

No need for shell commands...

perldoc -f opendir
perldoc -f readdir
perldoc -f closedir
perldoc File::Copy
perldoc -f -e
perldoc strict
perldoc warnings

As an aside, if you must use a shell command you are better off sticking
with system, until you need the output which is provided by backticks,
only use the piped open form when you need to communicate with the
command you are running.

http://danconia.org

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




PLEASE HELP

2005-08-26 Thread Angie
I am sorry to bother the list folks with this, but I
am trying to get off this list.  I have sent SEVERAL
unsubscribe from this list to
[EMAIL PROTECTED] and every other alias
provided in the welcome e-mail.  No dice!  Nothing!  I
even wrote an e-mail to the supposed "human" e-mail. 
Any help would be appreciated, as obviously the
unsubscribe is NOT working!

Thanks in advance,
AW

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




System, open, SHELL redirect

2005-08-26 Thread steve tran
Hello, 

I guess I wasnt clear enough in my earlier mail.

what I meant was lets say I have 2 programs,
exe1 and exe2 

exe2 is input to exe1 

so I want to call this from my perl wrapper as 

system ("exe1 exe2"); or `exe1 exe2`;

If I  want to capture STDOUT from exe1 
thats simple if I redirect to a file

system("exe1 exe2 >> out"); or `exe1 exe2 1>out`;

Now the problem in this case is exe2's STDOUT will be
redirected to out file as well, what if I want output
of exe2 to go to STDOUT ? 

--- Binish A R <[EMAIL PROTECTED]> wrote:

> steve tran wrote:
> 
> >Hello 
> >
> >I am a beginner to perl and I have a question
> >
> >if I want to use system or open or backticks to
> >execute a system command, 
> >like system("$cmd arg1"); ( with system for
> instance) 
> >
> >and the $cmd which is a prog takes another program
> >called in arg1 , and that program(arg1) requires
> >STDIN, 
> >and I need to redirect the output from $cmd into a
> >file 
> >how can I make both happen ? 
> >
> >if I do system("$cmd arg1"); 
> >I can see my program waiting for input in
> STDOUT,but I
> >cant capture the STDOUT to proces later but when I
> add
> >system("$cmd arg1 >> out"); 
> > 
> >the program waits but the output from arg1 goes to
> >out. 
> >Is there an easy way to accomplish both.
> >
> >I would really appreciate any help 
> >steve
> >
> >
> >
> >
> > 
> >__ 
> >Yahoo! Mail for Mobile 
> >Take Yahoo! Mail with you! Check email on your
> mobile phone. 
> >http://mobile.yahoo.com/learn/mail 
> >
> >  
> >
> Although the question isn't clear to me, I think you
> are looking for pipes ...
> 
> open (FD, "|command arg1");
> 
> print FD "text from STDIN goes here";
> 
> 
> and if you want to capture the output from this
> program, you can use ...
> 
> open(FD, "command arg1|");
> @buf = ;
> 
> 
> 
> -- 
> *//binish//*
> 
> Get Thunderbird
> 
> 
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




Re: Need an explanation

2005-08-26 Thread Binish A R




[EMAIL PROTECTED] wrote:
while (<>) {
  
  chomp;
  
  next if /^#/;
  
  next if /^$/;
  
  
  $CONF{(split/=/)[0]}=(split/=/)[1];
  
}
  
  
  
  
  
  
-Original Message-
  
From: Binish A R <[EMAIL PROTECTED]>
  
To: Perl Beginners 
  
Cc: [EMAIL PROTECTED]
  
Sent: Fri, 26 Aug 2005 23:04:58 +0530
  
Subject: Re: Need an explanation
  
  
[EMAIL PROTECTED] wrote: maybe this is brief:
  
  
while (<>) {
  
   chomp;
  
   next if /^#/;
  
   next if /^$/;
  
   my ($var1,$var2)=split /=/;
  
   $CONF{$var1}=$var2;
  
}
  
  
  
  
-Original Message-
  
From: Binish A R <[EMAIL PROTECTED]>
  
To: Perl Beginners 
  
Sent: Fri, 26 Aug 2005 22:28:22 +0530
  
Subject: Need an explanation
  
  
I've a file, which has entries like the following ...
  
  
IDENTIFIER1=value1;
  
IDENTIFIER2=value2;
  
IDENTIFIER3=value3;
  
  
etc
  
  
I've got to parse the above file and am using a hash to do the same ...
  
Here is my code ...
  
  
  
while (<>) {
  
   chomp;
  
   next if /^#/;
  
   next if /^$/;
  
   %CONF = split /=/;
  
}
  
  
  
But %CONF contains only the last key/value pair :-/
  
  
So I had to modify the above script to
  
  
while (<>) {
  
   chomp;
  
   next if /^#/;
  
   next if /^$/;
  
   $ref = [ split /=/ ];
  
   $CONF{$ref->[0]} = $ref->[1];
  
}
  
  
The above is working fine.
  
  
So my question is why isn't the first code working?
  
I don't want to use too many variables in my script,
  
even with my second script I needed an extra variable viz $ref.
  
  
Is it possible to get the job done using the first script?
  
  
  
  
--
  
/binish/  [Image removed]
  
  
  
  
  

  
Check Out the new free AIM(R) Mail -- 2 GB of storage and
industry-leading spam and email virus protection.
  
  
  
  
but that requires two xtra variable, $var1, $var2 :(
  
--
  
/binish/  [Image removed]
  
  
  
  
  

  
Check Out the new free AIM(R) Mail -- 2 GB of storage and
industry-leading spam and email virus protection.
  
  
  

Thatz cool ... I appreciate that :D


-- 
/binish/







Re: Need an explanation

2005-08-26 Thread angelflowercn

while (<>) {
  chomp;
  next if /^#/;
  next if /^$/;

  $CONF{(split/=/)[0]}=(split/=/)[1];
}





-Original Message-
From: Binish A R <[EMAIL PROTECTED]>
To: Perl Beginners 
Cc: [EMAIL PROTECTED]
Sent: Fri, 26 Aug 2005 23:04:58 +0530
Subject: Re: Need an explanation

[EMAIL PROTECTED] wrote: maybe this is brief:

while (<>) {
   chomp;
   next if /^#/;
   next if /^$/;
   my ($var1,$var2)=split /=/;
   $CONF{$var1}=$var2;
}



-Original Message-
From: Binish A R <[EMAIL PROTECTED]>
To: Perl Beginners 
Sent: Fri, 26 Aug 2005 22:28:22 +0530
Subject: Need an explanation

I've a file, which has entries like the following ...

IDENTIFIER1=value1;
IDENTIFIER2=value2;
IDENTIFIER3=value3;

etc

I've got to parse the above file and am using a hash to do the same ...
Here is my code ...


while (<>) {
   chomp;
   next if /^#/;
   next if /^$/;
   %CONF = split /=/;
}


But %CONF contains only the last key/value pair :-/

So I had to modify the above script to

while (<>) {
   chomp;
   next if /^#/;
   next if /^$/;
   $ref = [ split /=/ ];
   $CONF{$ref->[0]} = $ref->[1];
}

The above is working fine.

So my question is why isn't the first code working?
I don't want to use too many variables in my script,
even with my second script I needed an extra variable viz $ref.

Is it possible to get the job done using the first script?



--
/binish/  [Image removed]





Check Out the new free AIM(R) Mail -- 2 GB of storage and 
industry-leading spam and email virus protection.




but that requires two xtra variable, $var1, $var2 :(
--
/binish/  [Image removed]





Check Out the new free AIM(R) Mail -- 2 GB of storage and 
industry-leading spam and email virus protection.



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




RE: Directories into arrays, again.

2005-08-26 Thread Daniel Kurtz
From: Daniel Kurtz [mailto:[EMAIL PROTECTED] 
>>> Why does @after end up looking exactly like @before? Is there some
buffering going on here? And if so, how do I clear it? <<<

Never mind, I figured it out. The file copying operation is another
shell operation (c'mon, I'm a newby, and I've only read as far as
perlopentut!) and the file handle needs to be closed before the second
opendir().

daniel

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




Re: Need an explanation

2005-08-26 Thread Binish A R




[EMAIL PROTECTED] wrote:
maybe this is brief:
  
  
while (<>) {
  
   chomp;
  
   next if /^#/;
  
   next if /^$/;
  
   my ($var1,$var2)=split /=/;
  
   $CONF{$var1}=$var2;
  
}
  
  
  
  
-Original Message-
  
From: Binish A R <[EMAIL PROTECTED]>
  
To: Perl Beginners 
  
Sent: Fri, 26 Aug 2005 22:28:22 +0530
  
Subject: Need an explanation
  
  
I've a file, which has entries like the following ...
  
  
IDENTIFIER1=value1;
  
IDENTIFIER2=value2;
  
IDENTIFIER3=value3;
  
  
etc
  
  
I've got to parse the above file and am using a hash to do the same ...
  
Here is my code ...
  
  
  
while (<>) {
  
   chomp;
  
   next if /^#/;
  
   next if /^$/;
  
   %CONF = split /=/;
  
}
  
  
  
But %CONF contains only the last key/value pair :-/
  
  
So I had to modify the above script to
  
  
while (<>) {
  
   chomp;
  
   next if /^#/;
  
   next if /^$/;
  
   $ref = [ split /=/ ];
  
   $CONF{$ref->[0]} = $ref->[1];
  
}
  
  
The above is working fine.
  
  
So my question is why isn't the first code working?
  
I don't want to use too many variables in my script,
  
even with my second script I needed an extra variable viz $ref.
  
  
Is it possible to get the job done using the first script?
  
  
  
  
--
  
/binish/  [Image removed]
  
  
  
  
  

  
Check Out the new free AIM(R) Mail -- 2 GB of storage and
industry-leading spam and email virus protection.
  
  
  
  

but that requires two xtra variable, $var1, $var2 :(


-- 
/binish/







Re: odd benchmark result, map vs foreach

2005-08-26 Thread Scott R. Godin

Wiggins d'Anconia wrote:


Your benchmark isn't controlled. In the first instance you are doing a
++ on what amounts to a scalar getting autovivified, in the second
instance you are assigning a pair of values to a list then autovivifying
it. It isn't necessarily the map vs. foreach that is causing your
difference. I think for your benchmark to be accurate you are going to
have to control the actions taken in the loop, otherwise they affect the
accuracy of your benchmark.


mm this may be true, but realistically, that is how I'd write them in code I 
actually use -- so it behooves me to test them the way I'd normally be using 
them.. not under artificial circumstances in ways I normally wouldn't write code.


I see what you're saying and it makes sense, but in this instance I'm more 
interested in seeing the results of the idiomatic expression of the end result. :)


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




Re: odd benchmark result, map vs foreach

2005-08-26 Thread Scott R. Godin

John W. Krahn wrote:

Scott R. Godin wrote:


Interesting .. I would have thought that map would be faster, but it
appears that foreach is, in this instance. curious.. :)




[snip]


To be equivalent the foreach sub should use assignment instead of
postincrement (which is also faster.)


Interestingly, not here.


Foreach => sub {
my %nr;
$nr{ $_ } = 1 foreach
 qw{ shipto_company shipto_email billto_company
 billto_email same_bill_ship rkh_quantity rkp_quantity rmh_quantity
 rmp_quantity rbh_quantity rbp_quantity ship_on_account
 shipping_id_number order_number };
 },


Also in my test using a hash slice is faster still.


This, however, is true.


Foreach => sub {
my %nr;
$_ = 1 foreach
 @nr{ qw{ shipto_company shipto_email billto_company
 billto_email same_bill_ship rkh_quantity rkp_quantity rmh_quantity
 rmp_quantity rbh_quantity rbp_quantity ship_on_account
 shipping_id_number order_number } };
 },


1:17pm {48} localhost:/home/webadmin>$ perl bench.pl
Benchmark: running Foreach, Foreach2, HashSlice, Map for at least 5 CPU 
seconds...
   Foreach: 13 wallclock secs
( 5.32 usr +  0.02 sys =  5.34 CPU) @ 35233.90/s (n=188149)
  Foreach2: 11 wallclock secs
( 5.19 usr +  0.01 sys =  5.20 CPU) @ 34134.42/s (n=177499)
 HashSlice: 11 wallclock secs
( 5.18 usr +  0.00 sys =  5.18 CPU) @ 37383.20/s (n=193645)
   Map: 11 wallclock secs
( 5.14 usr +  0.01 sys =  5.15 CPU) @ 23419.03/s (n=120608)
1:21pm {49} localhost:/home/webadmin>$ perl bench.pl
Benchmark: running Foreach, Foreach2, HashSlice, Map for at least 5 CPU 
seconds...
   Foreach: 11 wallclock secs
( 5.20 usr +  0.01 sys =  5.21 CPU) @ 35404.99/s (n=184460)
  Foreach2: 11 wallclock secs
( 5.20 usr +  0.01 sys =  5.21 CPU) @ 34068.91/s (n=177499)
 HashSlice: 11 wallclock secs
( 5.32 usr +  0.00 sys =  5.32 CPU) @ 37099.44/s (n=197369)
   Map: 11 wallclock secs
( 5.17 usr +  0.00 sys =  5.17 CPU) @ 23328.43/s (n=120608)

Maybe I'm just seeing things, but it certainly *appears* that way I originally 
wrote it, is somwhat faster than the alternate 'equivalent' you offered.



1:22pm {50} localhost:/home/webadmin>$ cat bench.pl
#!/usr/bin/perl

use warnings;
use strict;

use Benchmark qw(timethese);

timethese( -5, {
"Foreach" => sub {
my %nr;
$nr{$_}++ foreach
qw{ shipto_company shipto_email billto_company billto_email 
same_bill_ship rkh_quantity rkp_quantity rmh_quantity rmp_quantity rbh_quantity 
rbp_quantity ship_on_account shipping_id_number order_number };

},
"Map" => sub {
my %nr = map {$_ => 1}
qw{ shipto_company shipto_email billto_company billto_email 
same_bill_ship rkh_quantity rkp_quantity rmh_quantity rmp_quantity rbh_quantity 
rbp_quantity ship_on_account shipping_id_number order_number };

},
"Foreach2" => sub{
my %nr;
$nr{ $_ } = 1 foreach
qw{ shipto_company shipto_email billto_company billto_email 
same_bill_ship rkh_quantity rkp_quantity rmh_quantity rmp_quantity rbh_quantity 
rbp_quantity ship_on_account shipping_id_number order_number };

},
"HashSlice" => sub{
my %nr;
$_ = 1 foreach
@nr{ qw{ shipto_company shipto_email billto_company billto_email 
same_bill_ship rkh_quantity rkp_quantity rmh_quantity rmp_quantity rbh_quantity 
rbp_quantity ship_on_account shipping_id_number order_number } };

},
});

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




RE: Directories into arrays, again.

2005-08-26 Thread Daniel Kurtz
Sorry, obviously the code is supposed to read:

opendir( DIR1, ".");
@before = readdir(DIR1);
closedir(DIR1);

# Execute a command that backs up every file in the directory
# with a .bak extension.

opendir( DIR2, ".");
@after = readdir(DIR2);
closedir(DIR2);

It still doesn't work.

daniel

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




Directories into arrays, again.

2005-08-26 Thread Daniel Kurtz
OK, since everyone's making fun of my suggestion of using the output of
a shell command as a means of getting a directory listing into an array
, please explain to me what's wrong with this code I'm trying, using
opendir and readdir:

opendir( DIR1, ".");
@before = readdir(DIR);
closedir(DIR1);

# Execute a command that backs up every file in the directory
# with a .bak extension.

opendir( DIR2, ".");
@after = readdir(DIR2);
closedir(DIR2);

Why does @after end up looking exactly like @before? Is there some
buffering going on here? And if so, how do I clear it?

daniel

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




Re: Need an explanation

2005-08-26 Thread angelflowercn

maybe this is brief:

while (<>) {
   chomp;
   next if /^#/;
   next if /^$/;
   my ($var1,$var2)=split /=/;
   $CONF{$var1}=$var2;
}



-Original Message-
From: Binish A R <[EMAIL PROTECTED]>
To: Perl Beginners 
Sent: Fri, 26 Aug 2005 22:28:22 +0530
Subject: Need an explanation

I've a file, which has entries like the following ...

IDENTIFIER1=value1;
IDENTIFIER2=value2;
IDENTIFIER3=value3;

etc

I've got to parse the above file and am using a hash to do the same ...
Here is my code ...


while (<>) {
   chomp;
   next if /^#/;
   next if /^$/;
   %CONF = split /=/;
}


But %CONF contains only the last key/value pair :-/

So I had to modify the above script to

while (<>) {
   chomp;
   next if /^#/;
   next if /^$/;
   $ref = [ split /=/ ];
   $CONF{$ref->[0]} = $ref->[1];
}

The above is working fine.

So my question is why isn't the first code working?
I don't want to use too many variables in my script,
even with my second script I needed an extra variable viz $ref.

Is it possible to get the job done using the first script?



--
/binish/  [Image removed]





Check Out the new free AIM(R) Mail -- 2 GB of storage and 
industry-leading spam and email virus protection.



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




Re: System, shell question

2005-08-26 Thread Binish A R




steve tran wrote:

  Hello 

I am a beginner to perl and I have a question

if I want to use system or open or backticks to
execute a system command, 
like system("$cmd arg1"); ( with system for instance) 

and the $cmd which is a prog takes another program
called in arg1 , and that program(arg1) requires
STDIN, 
and I need to redirect the output from $cmd into a
file 
how can I make both happen ? 

if I do system("$cmd arg1"); 
I can see my program waiting for input in STDOUT,but I
cant capture the STDOUT to proces later but when I add
system("$cmd arg1 >> out"); 
 
the program waits but the output from arg1 goes to
out. 
Is there an easy way to accomplish both.

I would really appreciate any help 
steve




		
__ 
Yahoo! Mail for Mobile 
Take Yahoo! Mail with you! Check email on your mobile phone. 
http://mobile.yahoo.com/learn/mail 

  

Although the question isn't clear to me, I think you are looking for pipes ...

open (FD, "|command arg1");

print FD "text from STDIN goes here";


and if you want to capture the output from this program, you can use ...

open(FD, "command arg1|");
@buf = ;



-- 
/binish/







RE: Need a list of files in a dir.

2005-08-26 Thread Daniel Kurtz
Bob Showalter wrote:

> Please tell me you're kidding.

Why? It works. The question asked how you can do it, not the BEST way to
do it. And after a week of Perling, this was the one way I knew. Now I
know two ways. 

daniel

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




Need an explanation

2005-08-26 Thread Binish A R




I've a file, which has entries like the following ...

IDENTIFIER1=value1;
IDENTIFIER2=value2;
IDENTIFIER3=value3;

etc

I've got to parse the above file and am using a hash to do the same ...
Here is my code ...


while (<>) {
	chomp;
next if /^#/;
next if /^$/;
%CONF = split /=/;
}


But %CONF contains only the last key/value pair :-/

So I had to modify the above script to 

while (<>) {
	chomp;
	next if /^#/;
next if /^$/;
$ref = [ split /=/ ];
	$CONF{$ref->[0]} = $ref->[1];
}

The above is working fine.

So my question is why isn't the first code working?
I don't want to use too many variables in my script, 
even with my second script I needed an extra variable viz $ref.

Is it possible to get the job done using the first script?




-- 
/binish/







RE: Need a list of files in a dir.

2005-08-26 Thread Chance Ervin


-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED] 
Sent: Friday, August 26, 2005 9:40 AM
To: Daniel Kurtz; beginners@perl.org
Subject: Re: Need a list of files in a dir.

Daniel Kurtz wrote:
> Ooh ooh ooh! One I know!
> 
> open(COMMAND, "dir |");
> @files = ;

Please tell me you're kidding.

-- 

This may be closer to what you want:

opendir(DIR, $somedir) 
   or die "cannot opendir $somedir: $!";
while (defined($somefile = readdir(DIR))) {
# process "$somedir/$somefile"
}
closedir(DIR);

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




Re: Need a list of files in a dir.

2005-08-26 Thread Bob Showalter

Daniel Kurtz wrote:

Ooh ooh ooh! One I know!

open(COMMAND, "dir |");
@files = ;


Please tell me you're kidding.

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




Re: Need a list of files in a dir.

2005-08-26 Thread Luinrandir
Thanks Daniel.. and keep on TOP POSTING!

Gawd I hate bottom posting!

Lou Hernsen

(.. and so my children the war between the topposters and bottomposters
began
The topposters, light happy and joyous beings ; the bottomposters, smelly
and scragly
and living below the earth in tunnels and caves in the land of Scorch...
shaking there fists at the top posters.
"Oh.. they thought quietly to themselves, if WE could as free as you. But
since we
don't wanna, we will taunt you, and scold you and try to make you like us".
And the children of the top posters went on top posting. One day some of the
newer
top posters were throwing bread crumbs into the holes in the earth that lead
to the caverns
of the bottom posters when suddenly... a butterfly flew by and the children
of the
topposters followed it, happy and dancing and holding hands and not
listening to the grumblings,
coming from the holes in the ground, that lead to the caverns of the
bottomposters in the land of Scorch...
who lived unhappily ever after..)


- Original Message - 
From: "Daniel Kurtz" <[EMAIL PROTECTED]>
To: 
Sent: Friday, August 26, 2005 8:12 AM
Subject: RE: Need a list of files in a dir.


Ooh ooh ooh! One I know!

open(COMMAND, "dir |");
@files = ;

Daniel

-Original Message-
From: Luinrandir [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 25, 2005 21:33
To: beginners@perl.org
Subject: Need a list of files in a dir.


How do I get the list of files in a DIR and put in an array?

I'm drawing a blank on my search for this.

thanks
Lou






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




RE: Need a list of files in a dir.

2005-08-26 Thread Ryan Frantz


> -Original Message-
> From: Luinrandir [mailto:[EMAIL PROTECTED]
> Sent: Friday, August 26, 2005 2:53 PM
> To: Daniel Kurtz
> Cc: beginners@perl.org
> Subject: Re: Need a list of files in a dir.
> 
> Thanks Daniel.. and keep on TOP POSTING!
> 

Not to start a flame war, but it has been established on this list that
in-line (not bottom-) posting is preferred.

I was once instructed (not forced) to do so on this list as I primarily
top-post most emails (as many people do).  There are several arguments
in both camps but that discussion is outside the scope of this list.

To sum things up: be adaptable, man!  A protocol has been established on
this list.  Let it be known here and now (maybe it should be in the list
FAQ, if it isn't already).  Don't waste time (speaking from experience)
with top post/in-line post/bottom post propaganda.

ry

"It is best to remain quiet and thought a fool, than to open one's mouth
and remove all doubt."
-Grandpap

> Gawd I hate bottom posting!
> 
> Lou Hernsen


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




System, shell question

2005-08-26 Thread steve tran
Hello 

I am a beginner to perl and I have a question

if I want to use system or open or backticks to
execute a system command, 
like system("$cmd arg1"); ( with system for instance) 

and the $cmd which is a prog takes another program
called in arg1 , and that program(arg1) requires
STDIN, 
and I need to redirect the output from $cmd into a
file 
how can I make both happen ? 

if I do system("$cmd arg1"); 
I can see my program waiting for input in STDOUT,but I
cant capture the STDOUT to proces later but when I add
system("$cmd arg1 >> out"); 
 
the program waits but the output from arg1 goes to
out. 
Is there an easy way to accomplish both.

I would really appreciate any help 
steve





__ 
Yahoo! Mail for Mobile 
Take Yahoo! Mail with you! Check email on your mobile phone. 
http://mobile.yahoo.com/learn/mail 

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




RE: Need a list of files in a dir.

2005-08-26 Thread Ryan Frantz


> -Original Message-
> From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED]
> Sent: Friday, August 26, 2005 11:27 AM
> To: Daniel Kurtz
> Cc: beginners@perl.org
> Subject: Re: Need a list of files in a dir.
> 
> Please bottom post...
> 
> Daniel Kurtz wrote:
> > Ooh ooh ooh! One I know!
> >
> > open(COMMAND, "dir |");
> > @files = ;
> >
> 
> Sort of, while that *may* work it doesn't have proper error checking,
is
> less secure, less efficient, and less portable than many other ways,
> especially those already provided.
> 

I agree.  Though I'm still a novice, I have already applied Perl scripts
to several tedious tasks that I have.  These include searching file
systems for this, that, and the other file.  A good resource is
O'Reilly's Perl for System Administration, Ch. 2 Filesystems.  In
demonstrates modules such as Cwd and File::Find that work great in
scouring your filesystem.

And of course, don't forget to search CPAN; lots of good stuff.

> This is my same rant as in the past, check the archives if you want
the
> real details, but shelling out is an *absolute* last resort. Perl
> provides built-in functions to handle this, in cases where it does it
is
> never faster, safer, or more portable to shell out.
> 
> http://danconia.org
> 
> 
> > Daniel
> >
> > -Original Message-
> > From: Luinrandir [mailto:[EMAIL PROTECTED]
> > Sent: Thursday, August 25, 2005 21:33
> > To: beginners@perl.org
> > Subject: Need a list of files in a dir.
> >
> >
> > How do I get the list of files in a DIR and put in an array?
> >
> > I'm drawing a blank on my search for this.
> >
> > thanks
> > Lou
> >
> >
> >
> 
> --
> 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]
 




the time a program runs

2005-08-26 Thread Octavian Rasnita
Hi,

I have tried to find out the time a perl program runs, and I have used:

#at the start of the program:
my $begin = (times)[0];
my $begin_t = time();

... The program follows

# at the end of the program:
my $end = (times)[0] - $begin;
my $end_t = time() - $begin_t;
print "end: $end\nEnd_t: $end_t\n";

After running the program, it prints:

end: 4.953
End_t: 19

Why does this difference appear?


The program is a very short one, for testing the speed of Storable module.

Thank you.

Teddy



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




Re: Need a list of files in a dir.

2005-08-26 Thread Wiggins d'Anconia
Please bottom post...

Daniel Kurtz wrote:
> Ooh ooh ooh! One I know!
> 
> open(COMMAND, "dir |");
> @files = ;
> 

Sort of, while that *may* work it doesn't have proper error checking, is
less secure, less efficient, and less portable than many other ways,
especially those already provided.

This is my same rant as in the past, check the archives if you want the
real details, but shelling out is an *absolute* last resort. Perl
provides built-in functions to handle this, in cases where it does it is
never faster, safer, or more portable to shell out.

http://danconia.org


> Daniel
> 
> -Original Message-
> From: Luinrandir [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, August 25, 2005 21:33
> To: beginners@perl.org
> Subject: Need a list of files in a dir.
> 
> 
> How do I get the list of files in a DIR and put in an array?
> 
> I'm drawing a blank on my search for this.
> 
> thanks
> Lou
> 
> 
> 

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




RE: Need a list of files in a dir.

2005-08-26 Thread Daniel Kurtz
Ooh ooh ooh! One I know!

open(COMMAND, "dir |");
@files = ;

Daniel

-Original Message-
From: Luinrandir [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 25, 2005 21:33
To: beginners@perl.org
Subject: Need a list of files in a dir.


How do I get the list of files in a DIR and put in an array?

I'm drawing a blank on my search for this.

thanks
Lou



-- 
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: use Module VERSION; ignoring version???

2005-08-26 Thread JupiterHost.Net

You'll see that the author of this module has incorrectly implemented the
VERSION method as:

sub VERSION { $VERSION }


From perldoc UNIVERSAL:


   "VERSION ( [ REQUIRE ] )"
   "VERSION" will return the value of the variable $VERSION in the
   package the object is blessed into. If "REQUIRE" is given then it
   will do a comparison and die if the package version is not
greater
   than or equal to "REQUIRE".

   "VERSION" can be called as either a class (static) method, an
   object method or a function.

You should notify the module author to fix this (by removing his method)


Whoa, thats new :) I was always under the impression that sub VERSION 
could be specified in modules and was in fact encouraged (Don't ask me 
where I got that idea though ;p), but UNIVERSAL takes care of it for you 
in the same way import() is handled (IE I know I know *not* by 
UNIVERSAL, its the principle I'm comparing it to) if there is no 
explicit import() function?


I happen to know the author and he's pretty awesome about this sort of 
thing and will let him know so he can fix it :)


Thx :)

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




Cvs.pm

2005-08-26 Thread Chris Knipe

Just a quickie...

I'm trying to get to use Cvs.pm... I'm BATTLING.

For one, the documentation, indicates that errors is reported by 
$Cvs::ERROR.


$Cvs::ERROR, does not even exist in the module

Does anyone have some info on how I could get some DECENT code working with 
this module??  After creating my I can't even detect a incorrect login...



my $cvs = new Cvs ('/tmp', cvsroot="blah", password='blah') or die 
$Cvs::ERROR; ## Which is COMPLETELY wrong, but anyways.


I give it a incorrect login, and well yeah, it does not even pick this 
up


Is this module working at all?  Anything else that can be suggested for CVS 
access / manupilation in perl?


Thanks,
Chris. 




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




Cvs.pm

2005-08-26 Thread Chris Knipe
Just a quickie...

I'm trying to get to use Cvs.pm... I'm BATTLING.

For one, the documentation, indicates that errors is reported by 
$Cvs::ERROR.

$Cvs::ERROR, does not even exist in the module

Does anyone have some info on how I could get some DECENT code working with 
this module??  After creating my I can't even detect a incorrect login...


my $cvs = new Cvs ('/tmp', cvsroot="blah", password='blah') or die 
$Cvs::ERROR; ## Which is COMPLETELY wrong, but anyways.

I give it a incorrect login, and well yeah, it does not even pick this 
up

Is this module working at all?  Anything else that can be suggested for CVS 
access / manupilation in perl?

Thanks,
Chris. 


Re: map/grep and arrays

2005-08-26 Thread Xavier Noria

On Aug 26, 2005, at 13:19, Tom Allison wrote:


I keep getting hung up on what I think would be a simple problem.

I was to do approximately this:

Given an array of "stuff" print them out
print join(",", @row),"\n";

works great until your array elements contain dumb characters like  
\n\r somewhere in the string (not always at the end!).


Initially I thought I could do:

print join(",", map(s/[\r\n]+//g, @row)), "\n";
print join(",", map{s/[\r\n]+//g} @row), "\n";

but I get output like:
,,2,,



This is a common error: there map builds a list whose elements are  
respectively the _result_ of applying s/// to each element of @row.  
Now, it is natural (until you think twice) to associate the result of  
s/// with the new $_ and expect map to build a list with those  
resulting $_s. But s/// does _not_ evaluate to the new $_, the  
modified $_ is a side-effect of s///. On the contrary, s/// itself  
returns something:


Searches a string for a pattern, and if found, replaces that
pattern with the replacement text and returns the number of
substitutions made.  Otherwise it returns false (specifically,
the empty string).

And that's what you are getting, see what happens?

A possible fix is:

s/[\r\n]+//g for @row; # sanitizes @row in place
join(",", @row);

-- fxn

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




Re: map/grep and arrays

2005-08-26 Thread John W. Krahn
Tom Allison wrote:
> I keep getting hung up on what I think would be a simple problem.
> 
> I was to do approximately this:
> 
> Given an array of "stuff" print them out
> print join(",", @row),"\n";
> 
> works great until your array elements contain dumb characters like \n\r
> somewhere in the string (not always at the end!).
> 
> Initially I thought I could do:
> 
> print join(",", map(s/[\r\n]+//g, @row)), "\n";
> print join(",", map{s/[\r\n]+//g} @row), "\n";
> 
> but I get output like:
> ,,2,,
> 
> telling me how many of these I actually matched...
> 
> I can't be that far off, can I?

Not too far off.

print join( ',', map { s/[\r\n]+//g; $_ } @row ), "\n";



John
-- 
use Perl;
program
fulfillment

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




Re: map/grep and arrays

2005-08-26 Thread Manav Mathur

- Original Message - 
From: "Tom Allison" <[EMAIL PROTECTED]>
To: "beginners perl" 
Sent: Friday, August 26, 2005 4:49 PM
Subject: map/grep and arrays


> I keep getting hung up on what I think would be a simple problem.
> 
> I was to do approximately this:
> 
> Given an array of "stuff" print them out
> print join(",", @row),"\n";
> 
> works great until your array elements contain dumb characters like \n\r 
> somewhere in the string (not always at the end!).
> 
> Initially I thought I could do:
> 
> print join(",", map(s/[\r\n]+//g, @row)), "\n";
> print join(",", map{s/[\r\n]+//g} @row), "\n";
> 
> but I get output like:
> ,,2,,
> 
> telling me how many of these I actually matched...
> 
> I can't be that far off, can I?
> 


Try

print join(",", map{$_=s/[\r\n]+//g} @row), "\n";

Manav



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




Re: Program is exiting on a failure

2005-08-26 Thread Tom Allison

Wiggins d'Anconia wrote:

Chris Lyon wrote:


I seem to be erroring out @ the $session->login portion of my program
because the module that I am call is saying the password/username is
bad. How do I trap the error and exit cleanly without just dumping
from the application:


login failed: access denied or bad username at ./cisco.pl line 47

$session = Net::Telnet::Cisco->new(Host => $ip);
$session->login($login, $password);




You may want to check the 'errmode' method of Net::Telnet. Otherwise you
can catch the die in the normal Perl exception handling way,

perldoc -f eval

my $return = eval { # some code that might die };
if ($@) {
  print "Uh oh, croaked leaving only: $@";
}

# code after not croaking

http://danconia.org



Personal experience says when you are doing the eval{} thing to make 
sure you still have a way to die rather to the just keep plowing ahead.


As a practice, evaluate your $@ for errors you expect and then die on 
all the others.  This will give you a method for managing known errors 
without ignoring the unexpected.


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




map/grep and arrays

2005-08-26 Thread Tom Allison

I keep getting hung up on what I think would be a simple problem.

I was to do approximately this:

Given an array of "stuff" print them out
print join(",", @row),"\n";

works great until your array elements contain dumb characters like \n\r 
somewhere in the string (not always at the end!).


Initially I thought I could do:

print join(",", map(s/[\r\n]+//g, @row)), "\n";
print join(",", map{s/[\r\n]+//g} @row), "\n";

but I get output like:
,,2,,

telling me how many of these I actually matched...

I can't be that far off, can I?

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