how to reuse sub

2003-09-26 Thread perl
How can I reuse a subroutine?

My environment is redhat 9, apache2, perl-5.8.0-88, mod_perl-1.99_07-5.
I've tried to put the sub in a separate file and call it from another as
below. Please modify the snipet below to make it work.

mycommon.pl
---
#!/usr/bin/perl
#return a value wrapped by single quotes
#should this be declare package something?
sub doWrap
{ my $retval;

  if(length($_[0]) == 0) { $retval = "null"; }
  else { $retval = "'" . $_[0] . "'"; }
}

tst.pl
--
#!/usr/bin/perl
require "mycommon.pl";

print &doWrap("hello"); #should output 'hello' with single quotes

thanks,
-rkl

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



RE: Silly question

2003-09-26 Thread Gupta, Sharad
Thank you.

-Sharad

-Original Message-
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]
Sent: Monday, September 22, 2003 10:20 PM
To: Gupta, Sharad
Cc: [EMAIL PROTECTED]
Subject: Re: Silly question


On Sep 22, Gupta, Sharad said:

>   package Foo;
>   use overload q("") => sub {return shift->{bar}};
>   $s = bless{bar=>"hello"}, Foo;
>   print "$s\n"
>
>prints "hello".

Because you have overloaded "" for objects of class Foo.

>   package Foo;
>   use overload q("") => sub {return shift->{bar}};
>   $s = bless{bar=>"hello"},Foo;
>   $wilma = "how r u";
>   print "$wilma\n"
>
>prints "how r u".

Because you have overloaded "" for objects of class Foo, but there is no
object of class Foo in double quotes.

If you want to intercept ALL quoted strings, you'll need to use
overload::constant, but that becomes tricky business.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



Re: Do BEGIN blocks and END blocks have priority?

2003-09-26 Thread Steve Grazzini
On Fri, Sep 26, 2003 at 08:13:07PM -0700, R. Joseph Newton wrote:
> Dan Anderson wrote:
> > use strict;
> > use warnings;
> >
> > END
> > {
> > print "Look ma, i'm using subroutines!";
> 
> Shouldn't lie to your mama.  That is not a subroutine.  Its more a macro.
> I gets compiled vefore anything in the main namespace, other than another
> begin block.

It's an END block.  But I don't think "macro" is quite the right way
to describe BEGIN blocks, either.  I think of it like this: perl 
compiles your script from beginning to end, one token at a time.
When it sees a BEGIN block, it stops what it was doing (the main 
compilation) and begins a sub-compilation.  This recursive step
compiles whatever is inside the BEGIN block, right up to the closing
curly brace, and when that's finished, the code inside the BEGIN block
is executed.  Then Perl resumes the main compilation.  When the main
compilation is finished, the body of the script executes.

Inside the BEGIN block, subroutines and variable declarations
"earlier" in the script have already been compiled, which means that
this works:

  sub foo { "foo!" }
  BEGIN { print foo() }

But code "earlier" in the script hasn't been executed, which means
that this *doesn't* work.

  my $foo = "foo!";
  BEGIN { print $foo }

The $foo variable is declared, but it hasn't been initialized yet.

And besides, I thought the "Look, Ma!" was referring to this, which
really is a subroutine call --->

> > foo::foo();

Anyway:

> > BEGIN
> > {
> > package foo;
> 
> It cannot be called here, before [by definition, being a BEGIN block]
> compilation has begun.

But it can be called here.  And compilation has already begun -- 
everything before this BEGIN block has already been compiled.  The
subroutine call here even works!

The problem is that the subroutine call is *compiled* before the
subroutine-with-prototype is defined, which means that perl can't
check the prototype.

> > foo();
> > BEGIN
> > {

And then when foo() is defined here with a prototype, perl remembers
that it already compiled some calls to it, and issues the warning.

> > sub foo()
> > { print "\nfoo\n"; }
> > }

As a matter of fact, none of this has anything to do with BEGIN 
blocks; and you can demonstrate the same thing without them:

  % perl -we '
  foo(42);
  sub foo () {}
  sub bar () {}
  bar(42);
  '
  main::foo() called too early to check prototype at -e line 2.
  Too many arguments for main::bar at -e line 5, near "42)"
  Execution of -e aborted due to compilation errors.

You see that bar(42) is a compilation error.  foo(42) *should* be 
a compilation error, but perl had to skip the prototype check, and 
all it can do now is issue the "called too early" warning.

-- 
Steve

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



substr parsing mask

2003-09-26 Thread perl
Does anyone have a short routine for displaying mask on some values and
displaying the value of the last four? For example, alot of site display
credit card numbers like 1234 which shows only the last four.

I know how to use the substr but what about replacing the preceding values
with ?

thanks,
-rkl

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



Re: Are package and module the same?

2003-09-26 Thread Steve Grazzini
On Fri, Sep 26, 2003 at 08:30:29PM -0700, [EMAIL PROTECTED] wrote:
> Is this a current or outdated call? main'stderr

That's old-fashioned, but it doesn't seem to have been 
deprecated (no warnings, at least).

In point of fact, it's better to use the capitalized STDERR,
since it's one of the magic names that always gets forced into
package main.  To qualify lowercase "stderr", though, you can 
use the more modern "main::stderr" and the funny-looking
"main'stderr" interchangably.

-- 
Steve

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



Re: subroutine problem

2003-09-26 Thread R. Joseph Newton
Gedi wrote:

> Hi all,
>
> I have recently started to learn perl. After reading Randal Schwartz’s
> Learning perl, I decided to give my first program a whirl.
> Upon writing it, I was checking each section of code as I went along to
> make sure everything worked.
>
> I got to one section and couldn’t get it to run as a subroutine. I don’t
> fully understand the error I am getting and am hoping somebody can point
> me in the right direction.
>
> Here is a snippet of my code (edited/reduced to about ¼ of the full
> size) which gives the same error as my full program.

What error?  It helps to paste in the error message, as well as marking the
line numbers it references.

> #usr/bin/perl -w
>
> #use strict;

Nuh-uh.  You are not ready for help from others when you refuse it from your
compiler.  Strict compilation should ALWAYS be your first line of defense
against logic errors.

>
> use warnings;
> use IO::Socket;
>
> print "1. network Listing only\n";
> print "2. Port scan only\n";
>
> print "\nPlease enter selection: ";
> chomp (my $selection = );
>
> if ($selection == 1) {
>  #bla bla, don't need this info
> }
> elsif ($selection == 2) {
>  print "\nEnter target: ";
>  chomp(my $target = );
>  print "Enter start port: ";
>  chomp(my $port = );
>  print "Enter end port: ";
>  chomp(my $end_port = );
>  &scan;

I don't know if this is the error line, but it is not good.  Only use this
when you are handing a function reference to a callback.  Under normal
circumstances, you should use scan();

>
> }
>
>
> sub scan {
>
>  print "Scanning $target | from port $port to $end_port\n\n";

Where idid these variables come from.  No such variables have been declared
within either the scope of the function or the main namespace.  I notice,
though that you do declare and assign values to, then never use,
similarly-named variables within an elsif block above.  Did you mean to
declare them in the main namespace, then assign them within the else block?

>  foreach (; $port<=$end_port; $port++) {
>if ( IO::Socket::INET->new(
> PeerAddr   => $target,
> PeerPort   => $port,
> Proto  => 'tcp',
> Timeout => 1)) {
> print "Port $port is open\n";
>}
>  }
>  print "\nPort scan complete\n";
>  exit;
> }
>
>
> Can anyone suggest why I am getting an error and how I can fix it?

We can try, once you give us a place to start.  I already had to do way to
much of your work to determine where the errors arose.  FWIW, without strict
compilation, there were no errors.
The messages you got were warnings, telling you that your code was probably
not working [ie that variables were being used without being assigned a
value.
That is a good indication that you should turn strict back on to help find
where the problems are coming from

Joseph


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



Re: Is -w deprecated?

2003-09-26 Thread R. Joseph Newton
Daniel Staal wrote:

> --On Friday, September 26, 2003 20:29 -0400 David Wall
> <[EMAIL PROTECTED]> wrote:
>
> >
> >>  In the book I bought, Programming Perl, by O'Reilly they say I
> >>  should use warnings instead of -w after the shebang and the perl
> >> path because -w is deprecated.
> >>
> >>  Is this true?  Also, will use warnings; work on versions of perl
> >>  < 5.8?
> >
> > My copy of the Camel is at work, but if it says -w is deprecated,
> > I'm inclined to believe it. :-)
>
> Just checked my copies of both the Camel and the Lama.  Both say 'use
> warnings' is preferred over '-w' because it gives finer grained
> control.  Neither, as far as I can tell, says that '-w' is actually
> deprecated.
>
> Daniel T. Staal

Good point.  I use both, and FWIW, when I turnoff warnings selctively within
a block, it works.

Could be that the first thing the warnings module does is to disable or
cancel the pragma.

Joseph


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



Re: selectdb in DBI

2003-09-26 Thread R. Joseph Newton
Ramprasad A Padmanabhan wrote:

> quick question
>
>In my script  I need to connect to two  different databases How Can I
> do this
>
>   $dbh = DBI->connect( "DBI:mysql:database=$DBNAME1;host=$DBHOST","", ""
> )  or die "Can't connect to Mysql database: $DBI::errstr\n";
>
> ...
> ...
> ...
>
> # Now change the database
> $dbh->selectdb($DBNAME2)   # This function doesnot  exist
>
> Do I need to connect to mysql again inorder to select a different database

Is this that big a burden?  One of the major benefits of RDBMS is that a given
database encapsulates its content.  The same SQL engine may manage many
different databases, each with its own set of permission, ralational sructure,
etc.  It is only sensible that when you move from one to another, each
connection should be explicit.

AFAIK, there is no bar to having two simultaneous connections to different DBs
open through the same SQL engine, and that much better promotes interactivity.

Joseph


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



Are package and module the same?

2003-09-26 Thread perl
Is this a current or outdated call? main'stderr

-rkl

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



Re: Do BEGIN blocks and END blocks have priority?

2003-09-26 Thread R. Joseph Newton
Dan Anderson wrote:

>
> I don't understand why if BEGIN blocks can have different priorities a
> warning would be put out.  I mean, require and use are basically begin
> blocks in disguise, and if you need subroutines, variables, or whatever
> from a package in a package there needs to be precedence.  But this
> doesn't really seem to make sense.  Or does it?

[Question shifted]

>
>
> What is really driving me bonkers is if I try the following code:

It's a conceptual problem, I think.

> use strict;
> use warnings;
>
> END
> {
> print "Look ma, i'm using subroutines!";

Shouldn't lie to your mama.  That is not a subroutine.  Its more a macro.
I gets compiled vefore anything in the main namespace, other than another
begin block.

>
> foo::foo();
> }
>
> BEGIN
> {
> print "\nouter\n";
> BEGIN
> { print "\ninner\n"; }
> }

foo() could be called here, during the flow of execution.

>
> print "end\n";
>
> BEGIN
> {
> package foo;

It cannot be called here, before [by definition, being a BEGIN block]
compilation has begun.

>
> foo();
> BEGIN
> {
> sub foo()
> { print "\nfoo\n"; }
> }
> }

Joseph


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



Re: Should loops return a value?

2003-09-26 Thread R. Joseph Newton
Dan Anderson wrote:

> <2 Cents>
>
> It is possible that your concept -- returning values for all language
> constructs could be revolutionary.  For instance, many, many, many
> programming constructs support code like:
>
> // apologies for the pseudocode
> while ($foo = bar())
> { /* do something */ }
>
> bar ()
> { /* return false if nothing else needs to be done */ };
>
> BUT, at the same time there are a number of languages which have tried
> to be innovative and have never been heard from again.  (When was the
> last time you heard of a commercial app being coded in LISP,

Don't know about copmmercial apps, but my brother, a CAD/CAM machinist, uses LISP
often to communicate between AutoCAD and other applications.

> Haskell,
> SmallTalk,

All I can say is C++.

I'm not fond of this particular construct, but then, I'm not fond of grep, either.
I'd rather use something called get_all_matching().

Joseph


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



Re: Should loops return a value?

2003-09-26 Thread R. Joseph Newton
Ville Jungman wrote:

> to hear everybody saying that this is a great thing! But maybe someone
> agrees with me. What did You think first time You heard about objects, perl
> or something else, what __?

The first ime I heard about "Object Oriented Programming", it sounded like a
silly-ass buzzword.  The first time I looked at an example of OO, I thought,
"What a great idea.  Have your programming constructs look like objects in  the
real world!"

> At first glance they might not feel very
> temptating but feelings might change after using them?

Hmmm, can you explain just how this construct would reduce gratuitous techiness
and lend to natural communication of intent?

Joseph

[snip  Most of us keep archives, and can consult them for the full text of past
posts.]


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



Re: Should loops return a value?

2003-09-26 Thread R. Joseph Newton
Ville Jungman wrote:

> >From: "Hanson, Rob" <[EMAIL PROTECTED]>
> >If you really want a loop to return something, you can roll your own, even
> >in Perl 5... but the syntax won't be as you gave.
>
> Ye - i'm not searching a way to solve a single problem but trying to make
> programming easier. If loops returned values it would make the whole coding
> much clearlier and better structured - at least i believe so. It'd b easy to
> just watch a code and say what it's doing in many cases. Look at what You
> write below; it is of course clever code but cleverness and hacks often
> makes code hard to debug or read particularly when programs grow.

I guess tastes differ.  Particularly as to what constitutes readability and
hacks.  I prefer natural language.  To me, while means just that--do something
while some condition pertains.  I see nothing in the word to suggest a value
being returned, or to imply which value is the "natural" one to return.  The
return value strikes me more as an unexpected side effect.  That was also why I
dumped the abbreviations.  Takes much less energy to type a few extra characters
than to squint your eyes and try to figure out which real words the
abbreviations were meant to stand for [usually an infinite variety of
possiblities.]

A well-named function, on the other hand, tells you exactly what to expect.
Ealy binding of parameters to local variables also adds clarity by providing
concrete meaning to the parameters offered.

Whatever works for you, works for you, I guess, and you will have the
opportunity to demonstrate the efficacy of this approach when Perl 6 comes
along.  Meantime, I will continue writing code that says what I mean--which
means using well-named functions and variables.

Joseph


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



Re: subroutine problem

2003-09-26 Thread Jeff 'japhy' Pinyan
On Sep 26, Gedi said:

>fully understand the error I am getting and am hoping somebody can point
>me in the right direction.

You should have shown us the error, so that we don't need to run the code.
But as it stands, the code doesn't need to be run.

>#use strict;

Why'd you do that??

>elsif ($selection == 2) {
> print "\nEnter target: ";
> chomp(my $target = );
> print "Enter start port: ";
> chomp(my $port = );
> print "Enter end port: ";
> chomp(my $end_port = );
> &scan;
>}

The three variables $target, $port, and $end_port are all declared as
lexical variables in that block.  They can't be seen OUTSIDE the block.
That means your function scan() can't see them.  If you didn't call the
function, but rather put the code of the function IN the elsif block,
you'd be ok.

You should pass the variables to the function.

# ...
scan($port, $end_port, $target);
  }

  sub scan {
my ($from, $to, $where) = @_;
print "Scanning $where | from port $from to $to\n\n";
for my $p ($from .. $to) {
  print "Port $p is open\n" if IO::Socket::INET->new(
PeerAddr => $where,
PeerPort => $p,
Proto => 'tcp',
Timeout => 1,
  );
}
print "Port scan complete\n\n";
  }

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


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



subroutine problem

2003-09-26 Thread Gedi
Hi all,
 
I have recently started to learn perl. After reading Randal Schwartz’s
Learning perl, I decided to give my first program a whirl.
Upon writing it, I was checking each section of code as I went along to
make sure everything worked.
 
I got to one section and couldn’t get it to run as a subroutine. I don’t
fully understand the error I am getting and am hoping somebody can point
me in the right direction.
 
Here is a snippet of my code (edited/reduced to about ¼ of the full
size) which gives the same error as my full program. 
 
#usr/bin/perl -w
 
#use strict;
use warnings;
use IO::Socket;
 
print "1. network Listing only\n";
print "2. Port scan only\n";
 
print "\nPlease enter selection: ";
chomp (my $selection = );
 
if ($selection == 1) {
 #bla bla, don't need this info
}
elsif ($selection == 2) {
 print "\nEnter target: ";
 chomp(my $target = );
 print "Enter start port: ";
 chomp(my $port = );
 print "Enter end port: ";
 chomp(my $end_port = );
 &scan;
}
 
 
sub scan {
 
 print "Scanning $target | from port $port to $end_port\n\n";
 foreach (; $port<=$end_port; $port++) {
   if ( IO::Socket::INET->new(
PeerAddr   => $target,
PeerPort   => $port,
Proto  => 'tcp',
Timeout => 1)) {
print "Port $port is open\n";
   }
 }
 print "\nPort scan complete\n";
 exit;
}
 
 
Can anyone suggest why I am getting an error and how I can fix it?
 
Thanks
 
Ged.


Re: Is -w deprecated?

2003-09-26 Thread Daniel Staal
--On Friday, September 26, 2003 20:29 -0400 David Wall 
<[EMAIL PROTECTED]> wrote:


In the book I bought, Programming Perl, by O'Reilly they say I
should use warnings instead of -w after the shebang and the perl
path because -w is deprecated.
Is this true?  Also, will use warnings; work on versions of perl
< 5.8?
My copy of the Camel is at work, but if it says -w is deprecated,
I'm inclined to believe it. :-)
Just checked my copies of both the Camel and the Lama.  Both say 'use 
warnings' is preferred over '-w' because it gives finer grained 
control.  Neither, as far as I can tell, says that '-w' is actually 
deprecated.

Daniel T. Staal

---
This email copyright the author.  Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes.  This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: Should loops return a value?

2003-09-26 Thread Ville Jungman
	It is possible that your concept -- returning values for all language
constructs could be revolutionary. 	BUT, at the same time there are a 
number of languages which have tried
to be innovative and have never been heard from again.
I'm not creating a new language - this wouldn't remove anything from Perl 
nor from other languages and thus would be extremaly hard to ruin the whole 
perl with this.

This only would add possibility to use those values but You could just write 
code and use old scripts like before - and maybe use these few revolutionaly 
:) commands too.

_
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus

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


Re: Should loops return a value?

2003-09-26 Thread david
Ville Jungman wrote:

>>Ville Jungman wrote:
>>Shortly, I think it might be good if loops (etc.) could return values.
> 
> Yes, You're right John! A very good example! Which one would be more
> readable?
> 
> This:
>>@bigger_than_4=
>>   foreach $value(@values) {
>>  retnext $value."a" if $value > 4;
>>   }
>>;
> 
> ...or this:
>>@bigger_than_4 = grep { ( $_ = "${_}a" ) > 4 } @values;

i see where you are going and i do understand why you think your version 
reads better especially if you (generally speaking) are new to Perl but 
after you have used Perl for a while, grep and map become second nature and 
the extra syntax becomes more annoying than useful. if you want better 
communication for your code and the programmer, you might try something 
like:

#!/usr/bin/perl -w
use strict;

sub four(&@){

my ($ref,@r) = shift;

for(@_){
my $s = eval &$ref;
push(@r,$s) if($s);
}

return @r;
}

my @r = four { q~ return $_ . "a" if($_ > 4) ~ } (2..7);

for(@r){
print "$_\n";
}

__END__

prints:

5a
6a
7a

you might find that more readable but it's really a mess. trust me: when you 
get use to grep and map, you won't need what you propose.

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

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



Re: Is -w deprecated?

2003-09-26 Thread David Wall
--On Friday, September 26, 2003 5:51 PM -0400 Dan Anderson 
<[EMAIL PROTECTED]> wrote:

In the book I bought, Programming Perl, by O'Reilly they say I should
use warnings instead of -w after the shebang and the perl path because
-w is deprecated.
	Is this true?  Also, will use warnings; work on versions of perl < 5.8?
My copy of the Camel is at work, but if it says -w is deprecated, I'm 
inclined to believe it. :-)

The warnings pragma was introduced in Perl 5.6.  perllexwarn says "The use 
warnings pragma is a replacement for both the command line flag -w and the 
equivalent Perl variable, $^W."

Removing -w and $^W from perl would break a great deal of existing code, so 
I doubt they're going away anytime soon.  (But people can always run an 
older version of Perl if necessary.  I vaguely recall hearing about people 
still using Perl v4, the "dead, flea-bitten camel carcass" that was last 
patched in 1992.)

In any case, the pragma gives you finer-grained control over warnings than 
does -w or $^W, as I suppose you already knew.



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


Re: Is -w deprecated?

2003-09-26 Thread Bob X
"Dan Anderson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> In the book I bought, Programming Perl, by O'Reilly they say I should
> use warnings instead of -w after the shebang and the perl path because
> -w is deprecated.
>
> Is this true?  Also, will use warnings; work on versions of perl < 5.8?
>
I know that "use warnings;" works in at least 5.6 since that is where I
started learning Perl from. I have never heard it was deprecated myself. I
tend to use the "use" though.



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



Re: Should loops return a value?

2003-09-26 Thread Ville Jungman
Interesting discussion, I am not convinced of the value yet, but then again 
most of my experience is in Perl and so wouldn't have really thought to do 
it, so always open to something new...
No one has mentioned 'eval' that I have seen in this little discussion, 
couldn't it be used to wrap the while loop so that it would appear to 
return a value to be caught?
my $return;
$return = eval { while ( ) { } };
Of course then you deal with compile/runtime issues, checking $@, etc.
Sounds like fine detour at least to get a fast start! Could You explain more 
- i didn't fully understand how it exactly would be done?

---
Ville Jungman
_
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. 
http://join.msn.com/?page=features/virus

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


RE: Should loops return a value?

2003-09-26 Thread Dan Anderson
<2 Cents>

It is possible that your concept -- returning values for all language
constructs could be revolutionary.  For instance, many, many, many
programming constructs support code like:

// apologies for the pseudocode
while ($foo = bar())
{ /* do something */ }

bar ()
{ /* return false if nothing else needs to be done */ };

BUT, at the same time there are a number of languages which have tried
to be innovative and have never been heard from again.  (When was the
last time you heard of a commercial app being coded in LISP, Haskell,
SmallTalk, or another innovative language that never was? (Outside of
Emacs and Sawfish for LISP)).

So I don't know what to tell you.  It could be interesting and it could
save time.  It could also needlessly obfusacate code and never catch on.



-Dan


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



RE: Should loops return a value?

2003-09-26 Thread Ville Jungman
> i'm not searching a way to solve a single
> problem but trying to make programming easier.
This is what packaging up the functionality will do.  A quick search on 
CPAN
shows several specialized loop constructs that are designed to make
programming easier:
IfLoop - mixes if and for syntax
Proc::ParallelLoop - loop in parallel
Loop - for loops with added value
i made a fast search too but didn't find anything that does this.

Why not add another loop construct module?  ...In fact, I challenge you to
do so. :)
I have thought about it but i'm not so deeply familiar with perl compiler 
that i could play with for's and if's etc because they can not be easily 
overrided.

Maybe i should first just create an fast written stupid pre-compiler module 
that recreates these loops to standard perl. Then i'd get a better picture 
how it could be used in common, what all could be done with it etc - so, 
thanks for the challenge. Do You have any better ideas or possible other 
ways?

for example:
push @a,while(...){
  retnext $_."a" if $_>4;
}
would be compiled as:
@temp=();
while(...){
  push @temp,$_ if $_>4;
}
push @a,@temp;
so, it'd be very fast to write a compiler to do this but of course when 
there are more loops-inside-loops and value-waiters etc porridge gets 
thicker (oops).

> If loops returned values it would make the
> whole coding much clearlier and better
> structured - at least i believe so.
It sounds like what you are after is to modify the current loop constructs.
Right.

The issue there is that returning a value for all loops requires extra
overhead.  Since most users won't want anything returned from a loop you 
end
up just wasting some CPU time.  Sure, you might argue that a few CPU cycles
won't hurt anyone... but what about the next thing someone wants added, and
the next, and the next.
That what we perl users are already paying. You'll usually get faster 
programs if You'll create code with C and after compiling fix it with asm. 
Still it's true it could waste cpu time but i think it might be avoided - 
like i already wrote i suppose. Loops needed additional checking only if 
something was waiting it's return value; ie if their possible value would be 
needed - not every time.

For example
  while(...){...}
would be just same as before but,
  @a=while(...){...}
would get the compiler notice that @a must get a value from while. Extra 
time would only be needed if loop values are needed like @a now - and it 
would get just undef if retnext- or retlast-command weren't used in while.

This is same what for example last-command does now. It doesn't add extra 
cpu cycles if it's not used i suppose. And printf doesn't either. All (or 
usually?) good things doesn't cost.

I would expect the users that would use such a
construct would be few, especially since it isn't a common paradigm, so I
think it is better suited as an extension as opposed to a built-in.
You are right - for now. It's usual that it takes time users to get friendly 
with new languages and features. Many programmers get stuck to something and 
new things are hard to accept. But i agree often new "features" can be bad 
thing mainly if they get the compiler slow down or bloat. There are still 
good additions too and i think this one would be a major one when it'll be 
deeply understood.

Many people for example don't use perl or unix or some other good things at 
all - my mother for example. After a while they would hear that there are 
better ways to do different things although they might even be satisfied 
with their tools already. That's often the way it starts - i'm not supposing 
to hear everybody saying that this is a great thing! But maybe someone 
agrees with me. What did You think first time You heard about objects, perl 
or something else, what __? At first glance they might not feel very 
temptating but feelings might change after using them?

---
ville jungman, 2 laureston crescent, tower, blarney, cork, ireland
tel. + 353 - 21 - 451 6847, http://www.kolumbus.fi/vilmak
usko Herraan Jeesukseen, niin sinä pelastut. (apt. 16:31)





Rob

-Original Message-
From: Ville Jungman [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 25, 2003 10:18 PM
To: Hanson, Rob; [EMAIL PROTECTED]
Subject: RE: Should loops return a value?
>From: "Hanson, Rob" <[EMAIL PROTECTED]>
>If you really want a loop to return something, you can roll your own, 
even
>in Perl 5... but the syntax won't be as you gave.

Ye - i'm not searching a way to solve a single problem but trying to make
programming easier. If loops returned values it would make the whole coding
much clearlier and better structured - at least i believe so. It'd b easy 
to

just watch a code and say what it's doing in many cases. Look at what You
write below; it is of course clever code but cleverness and hacks often
makes code hard to debug or read particularly when programs grow.
Yours,
---
Ville Jungman
>How is this?  The only difference is that you need to put the array to

Re: Help needed on File manipulation

2003-09-26 Thread Dan Anderson
I'm a perl noob, so I'm not sure if this is the best way to do it, but
just a thought:

When a program is accessing the file create a file like:
filename.lock.  Delete it when you're done.  Then check to see if
filename.lock exists before trying to access the program -- if not
sleep.

Of course, I assume there has to be a better way to do this.  And
accessing file i/o will slow things down compared with something that
could be done in RAM.  (Which may or may not be a problem depending on
what kind of program you are creating).

-Dan


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



Is -w deprecated?

2003-09-26 Thread Dan Anderson
In the book I bought, Programming Perl, by O'Reilly they say I should
use warnings instead of -w after the shebang and the perl path because
-w is deprecated.

Is this true?  Also, will use warnings; work on versions of perl < 5.8?

Thanks, 

-Dan


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



What is the best way to handle errors?

2003-09-26 Thread Dan Anderson
I have a program I am trying to develop in Perl, and one of the
problems I am running into is what to do with the errors.  I was
thinking of creating a custom module that I could then use on all of my
programs to allow me to call a report_error() sub, which would
(depending on whether we were debugging, in production, or testing)
direct the error to either a log or the user of the program.  

This script will be dealing with the WWW, and located in the cgi-bin/
directory of a server.  What is the best way to deal with errors? 
Rotating log files?  E-mails back to the user? 

Thanks in advance,

-Dan


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



RE: Do BEGIN blocks and END blocks have priority?

2003-09-26 Thread david
Dan Anderson wrote:

> What is really driving me bonkers is if I try the following code:
> 
> use strict;
> use warnings;
> 
> END
> {
> print "Look ma, i'm using subroutines!";
> foo::foo();
> }
> 
> BEGIN
> {
> print "\nouter\n";
> BEGIN
> { print "\ninner\n"; }
> }
> print "end\n";
> 
> 
> BEGIN
> {
> package foo;
> foo();
> BEGIN
> {
> sub foo()
> { print "\nfoo\n"; }
> }
> }
> 
> I get:
> 
> 
> inner
> 
> outer
> foo::foo() called too early to check prototype at ./ad_module.pl line
> 21.
> 
> foo
> end
> Look ma, i'm using subroutines!
> foo
> 
> I don't understand why if BEGIN blocks can have different priorities a
> warning would be put out.  

the warning you see actually has very little to do with being inside the 
BEGIN{} block. even without the begin block:

#!/usr/bin/perl -w
use strict;

fun;
sub fun(){
print "hi\n";
}

__END__

still generate the same wanring:

main::fun() called too early to check prototype at ./tmp.pl line 14.
hi

if you don't want to see the warning, you can disable prototype:

#!/usr/bin/perl -w
use strict;

BEGIN{
&fun;
BEGIN{
sub fun(){
print "hi\n";
}
}
}

__END__ 

prints:

hi

generally speaking, '&fun' is NOT the same as 'fun()' but in this case, it 
doesn't make any difference.

BEGIN{} block are executed in FIFO (first in first out) order and END{} 
block are executed in LIFO (last in first out) order.

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

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



Help with Win32:FileSecurity

2003-09-26 Thread Carbone, Michael
Hi, 

I'm working on a script to get the permissions on a list of directories
using the following Perl script:

use warnings;
use Win32::FileSecurity qw(Get);
$file="C:\\SomePath\\dirfile";

open(DIRLIST, $file ) || die "Can't open dirfile: $!\n";
@filelist = ;

foreach $entry( @filelist ) {
   
Get( $entry, \%hash );

while( ($name) = each %hash ){
print "$name\n";
 }
}

close DIRLIST;

I get this error:

Error handling error: 123, GetFileSecurity at C:\Some.pl line 10,
 line 2.

I'm a bit stumped, can anyone give me a nudge in the right direction?

Thank you,

Michael Carbone
Open Systems Storage Management
land: 319-558-5732
cell: 319-331-7142




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



Help needed on File manipulation

2003-09-26 Thread Chinku Simon
Hi,
 I am writing a Perl program that reads files created by another independent process.
 I have to take care that the perl program does not read the files that are in the 
process of
getting created.
 I am programming in Windows NT environment. 
It is also not possible for the perl program to get triggered by the other process 
that creates
the files.

Is there any method by which it can detected whether the file is already in use by the 
another
process -- any modules or commands (already tried open command).

The file open function in perl returns an error if the file is already in use. But is 
there some
better way to do this? 
 Thanks

__
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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



RE: Reg. Perl script to read comma seperated file.

2003-09-26 Thread Paul Kraus
Perldoc Text::CSV

Easy 2 page doc that will fix you write up.

Paul

-Original Message-
From: Shashank Khanvilkar [mailto:[EMAIL PROTECTED] 
Sent: Friday, September 26, 2003 12:54 PM
To: [EMAIL PROTECTED]
Subject: Reg. Perl script to read comma seperated file.


Hi,
I do know how to read the comma seperated file using the following
script

while ($lines = ){
if ($lines =~ /^\s*$/){
last;
}
@flds = split(/,/, $lines);
($year, $paperName, $authors, $conf, $fileName, $abstract) = @flds;
print "$year $paperName, $fileName\n\n";
}

However I am facing one tiny problem..
My CSV file has data like this:
2003, Distibuted Multimedia Document Management Systems, "A. Khokhar,
and A. Ghafoor, ", IEEE... ,

Thus i want to read author's as a single field..
thus $authors should  be "A. Khokhar, and A. Ghafoor,"

Any ideas on how to modify this script will be appreciated. Shashank



-- 
Regards
Shashank
http://mia.ece.uic.edu/~papers



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



Reg. Perl script to read comma seperated file.

2003-09-26 Thread Shashank Khanvilkar
Hi,
I do know how to read the comma seperated file using the following script

while ($lines = ){
if ($lines =~ /^\s*$/){
last;
}
@flds = split(/,/, $lines);
($year, $paperName, $authors, $conf, $fileName, $abstract) = @flds;
print "$year $paperName, $fileName\n\n";
}

However I am facing one tiny problem..
My CSV file has data like this:
2003, Distibuted Multimedia Document Management Systems, "A. Khokhar, and A.
Ghafoor, ", IEEE... ,

Thus i want to read author's as a single field..
thus $authors should  be "A. Khokhar, and A. Ghafoor,"

Any ideas on how to modify this script will be appreciated.
Shashank



-- 
Regards
Shashank
http://mia.ece.uic.edu/~papers



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



Unicode Patern matching

2003-09-26 Thread Sturdevant-Contractor, Robert W
Hi,

For three days I have been totally _unsuccessful_ at matching a two word
pattern ("Windows 2000") in a unicode doc in a reasonable fashion. I am
using the ActiveState build 635 on W2k. Can someone give me an example? This
works for me but is rather ugly; there's gotta be a better way.

$_ =~ m/W\x00i\x00n\x00d\x00o\x00w\x00s\x00\x20\x002\x000\x000\x000x00/;

Much thanks
BobS

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



RE: Order of Command Line Options

2003-09-26 Thread Thomas Bätzler
Hi,

Jeff Westman [mailto:[EMAIL PROTECTED] asked:
> Why does the order of these options matter?
[...]
> $ nslookup someServer | perl -en 'print qq($_);'
> 
> $ nslookup someServer | perl -ne 'print qq($_);'

-e must be followed by the code:

$ perl --help

Usage: perl [switches] [--] [programfile] [arguments]
[...]
  -e 'command'one line of program (several -e's allowed, omit
programfile)
[...]

So what does your first example do? Try this:

$ perl -w -en 'print "Hello, World!\n"'
Unquoted string "n" may clash with future reserved word at -e line 1.
Useless use of a constant in void context at -e line 1.

HTH,
Thomas
"-w is your friend"


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



RE: Pop3 to SMTP

2003-09-26 Thread Wiggins d'Anconia


On 26 Sep 2003 14:00:29 -, Zanardi2k3 <[EMAIL PROTECTED]> wrote:

> Does anybody know of a Perl module / script that can retrieve mail from a 
> POP3 server and forward it to another SMTP system? I currently use 
> pullmail.exe ("http://www.swsoft.co.uk/index.asp?page=freesoftware";) but it 
> is not open source and i'd like to have something i can tweak, even if i 
> have to write it from scratch (hopefully using some module).
> 
> P.S. our mail server (SeattleLab Mail) only offers the choice to route all 
> of its mail to another server, but we must do this on a per-user basis.
> 
> Thank you for your time.
> 

This would be a fairly trivial task to be performed by the Mail::Box suite.  

Have you looked at 'fetchmail'?  Fetchmail should fulfill your list of requirements 
other than it is not implemented in Perl.

http://catb.org/~esr/fetchmail/

http://danconia.org

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



Pop3 to SMTP

2003-09-26 Thread Zanardi2k3
Does anybody know of a Perl module / script that can retrieve mail from a 
POP3 server and forward it to another SMTP system? I currently use 
pullmail.exe ("http://www.swsoft.co.uk/index.asp?page=freesoftware";) but it 
is not open source and i'd like to have something i can tweak, even if i 
have to write it from scratch (hopefully using some module).

P.S. our mail server (SeattleLab Mail) only offers the choice to route all 
of its mail to another server, but we must do this on a per-user basis.

Thank you for your time.

-- 
Zanardi2k3

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



RE: How to pass parameters to a module

2003-09-26 Thread Hanson, Rob
> Can someone explain how does one pass
> a parameter to a Perl Module?

There are a few ways.

#1 - On the use line

use My::Module qw(foo bar);

When you "use" a module it first loads the module and evaluates it.  Second
it runs the import() subroutine in the module (if there is one), passing the
params you specified on the use line.  BTW - this is how Exporter works, it
supplies an import() subroutine that exports variables/methods for you.  If
you need to use Exporter, and want your own import() sub, then you need to
do it a little differently.  Take a look at the perl docs for more info.

#2 - On object creation

my $obj = new My::Module('foo', 'bar');

#3 - Set via properties or methods

$obj->{key} = 'foo';
...or...
$obj->set_key('foo');

It really depends on what exactly you are trying to accomplish.

Rob



-Original Message-
From: Rajesh Dorairajan [mailto:[EMAIL PROTECTED]
Sent: Friday, September 26, 2003 1:00 AM
To: '[EMAIL PROTECTED]'
Subject: How to pass parameters to a module


Can someone explain how does one pass a parameter to a Perl Module? To
illustrate suppose I've My::Module

package My::Module;

BEGIN
{
  $scalar = $input;
}

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = ($scalar);

In the above script is there anyway to pass the $input variable to the
package My::Module from the calling script? Please excuse me if the code is
horrible, just trying to simplify what I want ;)

TIA

Rajesh

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



RE: Should loops return a value?

2003-09-26 Thread Hanson, Rob
> cleverness and hacks often makes code hard
> to debug or read particularly when programs grow.

I was really just showing that it could be done.  The next step would be to
package the subroutine up as a library so that is can be reused.  This has
been done before in module like Error that gives you constructs that look
like typical try/catch/finally.  And once it is packaged, and deemed stable,
you never have to worry about it again.  Just think how often you use a
module, and never question that it is working correctly.

> i'm not searching a way to solve a single
> problem but trying to make programming easier.

This is what packaging up the functionality will do.  A quick search on CPAN
shows several specialized loop constructs that are designed to make
programming easier:

IfLoop - mixes if and for syntax
Proc::ParallelLoop - loop in parallel
Loop - for loops with added value

Why not add another loop construct module?  ...In fact, I challenge you to
do so. :)

> If loops returned values it would make the
> whole coding much clearlier and better
> structured - at least i believe so.

It sounds like what you are after is to modify the current loop constructs.
The issue there is that returning a value for all loops requires extra
overhead.  Since most users won't want anything returned from a loop you end
up just wasting some CPU time.  Sure, you might argue that a few CPU cycles
won't hurt anyone... but what about the next thing someone wants added, and
the next, and the next.  I would expect the users that would use such a
construct would be few, especially since it isn't a common paradigm, so I
think it is better suited as an extension as opposed to a built-in.

Rob

-Original Message-
From: Ville Jungman [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 25, 2003 10:18 PM
To: Hanson, Rob; [EMAIL PROTECTED]
Subject: RE: Should loops return a value?


>From: "Hanson, Rob" <[EMAIL PROTECTED]>
>If you really want a loop to return something, you can roll your own, even
>in Perl 5... but the syntax won't be as you gave.

Ye - i'm not searching a way to solve a single problem but trying to make 
programming easier. If loops returned values it would make the whole coding 
much clearlier and better structured - at least i believe so. It'd b easy to

just watch a code and say what it's doing in many cases. Look at what You 
write below; it is of course clever code but cleverness and hacks often 
makes code hard to debug or read particularly when programs grow.

Yours,
---
Ville Jungman

>How is this?  The only difference is that you need to put the array to loop
>over after the code.  it's close though.
>
>sub loop (&@);
>
>my @x = (1,3,5,7);
>my @y = loop {
>   return $_[0]."a" if $_[0] > 4;
>} @x;
>
>print "@y";
>
>
>sub loop (&@) {
>   my $sub = shift;
>   my @ret;
>
>   foreach (@_) {
> my $val = &$sub($_);
> push @ret, $val if $val;
>   }
>
>   return @ret;
>}
>
>Rob
>
>
>-Original Message-
>From: Ville Jungman [mailto:[EMAIL PROTECTED]
>Sent: Thursday, September 25, 2003 9:36 PM
>To: Hanson, Rob; [EMAIL PROTECTED]
>Subject: RE: Should loops return a value?
>
>
>Rob, did You read my message at all :-) ?
>
>i just was wandering if there _could_ be more readable way to do this.
>Andnot only for to be readable. If loops (and maybe some other builtin
>commands, too - we are not talking only about whiles and for's) returned
>values, programming might be a way different because you could combine 
>loops
>
>with for example those greps and maps. Consider really what would be
>possible to do with this (maybe look at those examples again, too, to get
>the idea). Also You can not do everything with grep and map and on the 
>other
>
>hand they are not very fast to read or debug when comparizing with this.
>
>i've _always_ wanted to have a program language where loop-commands could
>return whatever is needed. It might be that someday i have to write such
>language or make a fast poor implementation. If this ability would be
>implemented wisely to perl (or to some other language), it wouldn't even
>affect to performance (ok, maybe little bit). But it'd bring much power
>because You could have more control, readibility and straightforwardity in
>Your code - and maybe more speed in some circumstances, too.
>
>Kindly, Ville Jungman
>
> >From: "Hanson, Rob" <[EMAIL PROTECTED]>
> >@values = (1,3,5,7);
> >@bigger_than_4 = map {$_.'a'} grep {$_>4} @values;
> >print "@bigger_than_4";
> > > You need to escape a loop with a value.
> >
> >Not sure I understand what you are trying to accomplish, but this is the
> >equivalent of your Perl version and is as short as your proposed syntax.
> >
> >while () {
> >   next unless /stop/;
> >   #somthing
> >   last;
> >}
> >
> >Rob
> >
> >
> >-Original Message-
> >From: Ville Jungman [mailto:[EMAIL PROTECTED]
> >Sent: Thursday, September 25, 2003 8:25 PM
> >To: [EMAIL PROTECTED]
> >Subject: Should loops return a value?
> >
> >
> >Shortly, I think it might

RE: unzipping a record at a time

2003-09-26 Thread Wiggins d'Anconia


On Fri, 26 Sep 2003 10:04:37 -0500, "JOHN FISHER" <[EMAIL PROTECTED]> wrote:

> I would say its a WinZip type file, but it only has one file in it. WinZip knows 
> what it is. Has an file extension of .zip, not .gz or .tar (my knowledge here is 
> minimal). I will look into Archive::Zip. I don't think Deflate compression was used 
> so I may have problems there as well. But hey I am learning and appreciate your help.
> 
> I have worked around it by using "unzip -p zipfile.zip | ./pgm.pl" and read it from 
> STDIN. As you point out it might not be efficient. However, this is a short lived 
> program (I hope). Still will persue looking for a better solution for the knowledge.
> 
> One other question, the doc for Compress::Zlib says "refer to the zlib 
> documentation" over and over. Where is that doc? Is it within cpan.org or perldoc?
> 

Sorry meant to mention about that last bit. 'zlib' is a C library included on most (if 
not all) unix systems that allows wrappers to be written around reading compressed 
files, aka Compress::Zlib wraps 'zlib' if my understanding is correct.  So it is not 
related directly to Perl which is why 'perldoc' didn't help much. You would need 'man 
zlib' assuming it is installed. I don't know the procedures for a normal Win32 setup 
would be for obtaining the info, Cygwin provides zlib.  For much more info check out:

http://www.gzip.org/zlib/

http://danconia.org


> 
> >>> "Wiggins d'Anconia" <[EMAIL PROTECTED]> 09/26/03 10:41AM >>>
> 
> Ok that may help to clear things up, but 1 question first.  Is this a 'zip archive' 
> file or is it a 'zipped file'.  In other words, is this an archive that contains 
> multiple files in the sense of WinZip, etc. (excuse my lack of Win32 knowledge), or 
> is it a single file that has been compressed in the gzip unix manner?
> 
> You can unzip PKZIP/WinZip/etc/ archives using Archive::Zip (that's what it's for) 
> as long as any compressed members are compressed using Deflate compression."
> 
> Is this where the snag lies?
> 
> 
> 
> 
> 
> 
> -- 
> 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: env vars using perl

2003-09-26 Thread Randal L. Schwartz
> "Pete" == Pete Emerson <[EMAIL PROTECTED]> writes:

Pete> It's the same way. Here's code that works for me:

Pete> #!/usr/bin/perl -w

Pete> use strict;
Pete> use CGI qw(:standard);

Pete> print header;
Pete> print start_html;
Pete> foreach my $key (sort keys %ENV) {
Pete>  print "\$ENV{$key} = $ENV{$key}\n";
Pete> }
Pete> print end_html;

This breaks if there's any HTML entities in the output.
Simpler:

print header('text/plain');
foreach my $key (sort keys %ENV) {
  print "\$ENV{$key} = $ENV{$key}\n";
}

There... html-safe now. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> 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: Do BEGIN blocks and END blocks have priority?

2003-09-26 Thread Dan Anderson
> BEGIN blocks do not take precedence over one another--they are all
> still executed.  They are, however, executed immediately after perl
> finishes compiling them.  So, if you have the following code:

Ok, so I'm guessing that the reason that 3214 is displayed is not
because of precedence, but that every begin block is pushed into a stack
of some kind, and when perl starts executing code, code on the top of
the stack ends up being run first.  Is that right?

So it's sort of like precedence but really isn't.  Which explains when
things printed off are printed off from innermost begin to outermost
begin, but a subroutine declared in a begin nested in other begins
creates an error when called before the sequence in the code.

Correct?

-Dan


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



RE: unzipping a record at a time

2003-09-26 Thread JOHN FISHER
I would say its a WinZip type file, but it only has one file in it. WinZip knows what 
it is. Has an file extension of .zip, not .gz or .tar (my knowledge here is minimal). 
I will look into Archive::Zip. I don't think Deflate compression was used so I may 
have problems there as well. But hey I am learning and appreciate your help.

I have worked around it by using "unzip -p zipfile.zip | ./pgm.pl" and read it from 
STDIN. As you point out it might not be efficient. However, this is a short lived 
program (I hope). Still will persue looking for a better solution for the knowledge.

One other question, the doc for Compress::Zlib says "refer to the zlib documentation" 
over and over. Where is that doc? Is it within cpan.org or perldoc?

John

>>> "Wiggins d'Anconia" <[EMAIL PROTECTED]> 09/26/03 10:41AM >>>

Ok that may help to clear things up, but 1 question first.  Is this a 'zip archive' 
file or is it a 'zipped file'.  In other words, is this an archive that contains 
multiple files in the sense of WinZip, etc. (excuse my lack of Win32 knowledge), or is 
it a single file that has been compressed in the gzip unix manner?

You can unzip PKZIP/WinZip/etc/ archives using Archive::Zip (that's what it's for) as 
long as any compressed members are compressed using Deflate compression."

Is this where the snag lies?






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



Order of Command Line Options

2003-09-26 Thread Jeff Westman
Hi,

Why does the order of these options matter?  In the first case, no output is
produced, but it works correctly in the second case.  I would have thought
perl would have been smart enough to parse the command line options in any
order.


$ nslookup someServer | perl -en 'print qq($_);'

$ nslookup someServer | perl -ne 'print qq($_);'
Name Server:  dns.myCompany.com
Address:  10.18.68.22

Trying DNS
Name:someServer.myCompany.com
Address:  10.193.20.200



TIA

-Jeff


__
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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



Re: Perl - HowTo operating with large file?

2003-09-26 Thread Wiggins d'Anconia


On Fri, 26 Sep 2003 20:13:21 +0530, Ramprasad A Padmanabhan <[EMAIL PROTECTED]> wrote:

> Juris wrote:
> > I have one small problem!
> > HowTo read from large text file text in binary mode?
> > 
> > if i want read all file, i use this code:
> > 
> > my (@LOG_FILE);
> > open (FL, "/var/log/maillog");
> > @LOG_FILE=;
> > close (FL);
> > #After this code execution file contents stored in array @LOG_FILE
> > @LOG_FILE=grep /$something/, @LOG_FILE
> > 
> > I not want read all file string by struing - it's so slowly, if file is 
> > large and contains more than 100 records!
> > I need read each 50 bytes, but how?
> > Please, help!
> > 
> > 
> 
> 
> When you are opening big files never do
> @array = 
> This essentially reads the entire file into an array and is very 
> expensive on memory.
> 
> you could do something like
> while(){
>   push @arr , $_ if(/$something/);
> }
> 
> But IMHO this still that may not be the best way.
> 
> What I would do is
> 
> 
> system("grep $something $filename > $tempfile");
> # *Nothing* beats gnu grep when you parse large file
> 
> open(FILE,$tempfile);
> # Now if you really want the lines in an array
> @lines = 

This is NOT production sufficient code, either please handle it fully or disclaim it 
by stating so.  You don't use a full path (or suggest to), you don't check the return 
value of system, there are all kinds of issues with respect to using '>' to write to a 
file (permissions, disk space, etc.) Security issues, naturally checking 'open' for 
success, removing core files if they are generated, "cleaning" the variables for taint 
safety, etc, etc, etc.  Shelling out in this manner is quick and dirty (and has its 
places I admit) but on a beginners list please disclaim it as so.

The OP may want to check out the read function, use it carefully

perldoc -f read

http://danconia.org

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



RE: unzipping a record at a time

2003-09-26 Thread Wiggins d'Anconia


On Fri, 26 Sep 2003 04:59:34 -0500, "JOHN FISHER" <[EMAIL PROTECTED]> wrote:

> I am in a Windows environment using cygwin. The zip file has /r/n as a carriage 
> return (so annoying). When I ran the script below it dumped out a lot of bizarre 
> chars to the screen. I guess this is some of the zip metadata. Using unzip -p 
> zipfile.zip it prints cleanly. The doc states:
>  "At this time gzreadline ignores the variable $/ ($INPUT_RECORD_SEPARATOR or $RS 
> when English is in use). The end of a line is denoted by the C character '\n'."
> Do you think this might be throwing the script or did I do something incorrect?
> The mode is just from the example. The cpan doc says check out the zlib doc for the 
> values, but I have been unable to locate it. perldoc zlib comes up empty.
> Should I use binmode? I cannot think how to set that up. It wants a filehandle, but 
> you can see I have gzopen'd the filename.
> 

Ok that may help to clear things up, but 1 question first.  Is this a 'zip archive' 
file or is it a 'zipped file'.  In other words, is this an archive that contains 
multiple files in the sense of WinZip, etc. (excuse my lack of Win32 knowledge), or is 
it a single file that has been compressed in the gzip unix manner?

Compress::Zlib is intended for the latter use, and I suggested it because you 
mentioned zgrep which generally handles the gzipp'd version rather than the former, 
someone else mentioned Archive::Zip which is used for the first.

FAQ for Archive::Zip sums it up nicely:

"Can't use Archive::Zip on gzip files

Q: Can I use Archive::Zip to extract Unix gzip files?

A: No.

There is a distinction between Unix gzip files, and Zip archives that also can use the 
gzip compression.

Depending on the format of the gzip file, you can use Compress::Zlib, or Archive::Tar 
to decompress it (and de-archive it in the case of Tar files).

You can unzip PKZIP/WinZip/etc/ archives using Archive::Zip (that's what it's for) as 
long as any compressed members are compressed using Deflate compression."

Is this where the snag lies?

http://danconia.org

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



Re: Perl - HowTo operating with large file?

2003-09-26 Thread Ramprasad A Padmanabhan
Juris wrote:
I have one small problem!
HowTo read from large text file text in binary mode?
if i want read all file, i use this code:

my (@LOG_FILE);
open (FL, "/var/log/maillog");
@LOG_FILE=;
close (FL);
#After this code execution file contents stored in array @LOG_FILE
@LOG_FILE=grep /$something/, @LOG_FILE
I not want read all file string by struing - it's so slowly, if file is 
large and contains more than 100 records!
I need read each 50 bytes, but how?
Please, help!




When you are opening big files never do
@array = 
This essentially reads the entire file into an array and is very 
expensive on memory.

you could do something like
while(){
push @arr , $_ if(/$something/);
}
But IMHO this still that may not be the best way.

What I would do is

system("grep $something $filename > $tempfile");
# *Nothing* beats gnu grep when you parse large file
open(FILE,$tempfile);
# Now if you really want the lines in an array
@lines = 
Ram



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


Re: Should loops return a value?

2003-09-26 Thread Wiggins d'Anconia


On Fri, 26 Sep 2003 16:21:56 +0300, "Ville Jungman" <[EMAIL PROTECTED]> wrote:

> >Not crap, just unnecessary.  If the loop should arrive at a value to be
> >returned, it should probably be enclosed in a function, which was designed 
> >for
> >returning values.
> 
> Everything is unnessessary because always code could be made with machine 
> language or assember - latter of course is unnessessary in that case.
> 
> If loops returned values the point was they returned them like other 
> commands or functions and it wouldn't ne necessary to enclose it in a 
> function. Like this:
> 
>@a=while(...){...}
> 
> or even like this:
> 
>$answer=
>   while(
>  foreach(@a,while{...})
>   ) + # loops return values have to be in scalar context when using 
> +, -, * etc
>   while(
>  foreach(...),while(...)
>   )
> 
> These might need a programmers to change their programming habits a little 
> bit, but i think it'd be worth of it. (Of course one can code in old way, 
> too.)
> 

Interesting discussion, I am not convinced of the value yet, but then again most of my 
experience is in Perl and so wouldn't have really thought to do it, so always open to 
something new...

No one has mentioned 'eval' that I have seen in this little discussion, couldn't it be 
used to wrap the while loop so that it would appear to return a value to be caught?

my $return;
$return = eval { while ( ) { } };

Of course then you deal with compile/runtime issues, checking $@, etc. 

http://danconia.org

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



Re: How to pass parameters to a module

2003-09-26 Thread Ramprasad A Padmanabhan
Rajesh Dorairajan wrote:
Can someone explain how does one pass a parameter to a Perl Module? To
illustrate suppose I've My::Module
package My::Module;

BEGIN
{
  $scalar = $input;
}
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = ($scalar);
In the above script is there anyway to pass the $input variable to the
package My::Module from the calling script? Please excuse me if the code is
horrible, just trying to simplify what I want ;)
TIA

Rajesh



Suppose you are calling this from a script say script.pl

in script.pl put
BEGIN {
  $GLOBAL::input="THIS IS PASSED TO THE MODULE";
  require My::Module;
}


and in the package put
$scalar=$GLOBAL::input;
I am not very sure if this a "good" way of doing it but works.
And  BTW  you can not do *use* My::Module , You will have to do 
*require* My::Module





Ram





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


Re: selectdb in DBI

2003-09-26 Thread Mike Blezien
The simplest way would be to create two separate database handles, IE. $dbh1 amd $dbh2 
when connecting. Assuming both databases are on the same machine.


MikeBlezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
http://www.thunder-rain.com
Web Hosting
http://www.justlightening.net
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Original Message - 
From: "Ramprasad A Padmanabhan" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, September 26, 2003 8:51 AM
Subject: selectdb in DBI


> quick question
> 
>In my script  I need to connect to two  different databases How Can I 
> do this
> 
>   $dbh = DBI->connect( "DBI:mysql:database=$DBNAME1;host=$DBHOST","", "" 
> )  or die "Can't connect to Mysql database: $DBI::errstr\n";
> 
> ...
> ...
> ...
> 
> # Now change the database
> $dbh->selectdb($DBNAME2)   # This function doesnot  exist
> 
> 
> Do I need to connect to mysql again inorder to select a different database
> 
> Thanks
> Ram
> 
> 
> 
> 
> 
> 
> -- 
> 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]



selectdb in DBI

2003-09-26 Thread Ramprasad A Padmanabhan
quick question

  In my script  I need to connect to two  different databases How Can I 
do this

 $dbh = DBI->connect( "DBI:mysql:database=$DBNAME1;host=$DBHOST","", "" 
)  or die "Can't connect to Mysql database: $DBI::errstr\n";

...
...
...
# Now change the database
$dbh->selectdb($DBNAME2)   # This function doesnot  exist
Do I need to connect to mysql again inorder to select a different database

Thanks
Ram




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


Re: Should loops return a value?

2003-09-26 Thread Ville Jungman
Not crap, just unnecessary.  If the loop should arrive at a value to be
returned, it should probably be enclosed in a function, which was designed 
for
returning values.
Everything is unnessessary because always code could be made with machine 
language or assember - latter of course is unnessessary in that case.

If loops returned values the point was they returned them like other 
commands or functions and it wouldn't ne necessary to enclose it in a 
function. Like this:

  @a=while(...){...}

or even like this:

  $answer=
 while(
foreach(@a,while{...})
 ) + # loops return values have to be in scalar context when using 
+, -, * etc
 while(
foreach(...),while(...)
 )

These might need a programmers to change their programming habits a little 
bit, but i think it'd be worth of it. (Of course one can code in old way, 
too.)

_
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* 
http://join.msn.com/?page=features/junkmail

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


RE: How to autouse Statistics::Descriptive?

2003-09-26 Thread Bob Showalter
Hoenie Luk wrote:
> Has anyone try to delay loading the Statistics module by
> using autouse? I
> can't get it to work.
> 
> The usual usage without autouse is this:
> use Statistics::Descriptive;
> $stat = Statistics::Descriptive::Sparse->new();
> 
> But if I autouse with this syntax:
> use autouse 'Statistics::Descriptive';
> $stat = Statistics::Descriptive::Sparse->new();
> 
> I get this error:
> Can't locate object method "new" via package
> "Statistics::Descriptive::Sparse" (
> perhaps you forgot to load "Statistics::Descriptive::Sparse"?) at
> C:\Perl\prog\b io1a\test.pl line 2.

I'm not that familiar with autouse, but it appears to be designed for those
modules that export functions, and not modules that are used OOP-style.

If you want to delay loading until you're ready to call new, you can do
this:

   eval "use Statistics::Descriptive";
   die $@ if $@;
   $stat = Statistics::Descriptive::Sparse->new();

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



RE: unzipping a record at a time

2003-09-26 Thread JOHN FISHER
I am in a Windows environment using cygwin. The zip file has /r/n as a carriage return 
(so annoying). When I ran the script below it dumped out a lot of bizarre chars to the 
screen. I guess this is some of the zip metadata. Using unzip -p zipfile.zip it prints 
cleanly. The doc states:
 "At this time gzreadline ignores the variable $/ ($INPUT_RECORD_SEPARATOR or $RS when 
English is in use). The end of a line is denoted by the C character '\n'."
Do you think this might be throwing the script or did I do something incorrect?
The mode is just from the example. The cpan doc says check out the zlib doc for the 
values, but I have been unable to locate it. perldoc zlib comes up empty.
Should I use binmode? I cannot think how to set that up. It wants a filehandle, but 
you can see I have gzopen'd the filename.

#!/usr/bin/perl -w
use Compress::Zlib;
@ARGV == 1 or die "Usage: $0 input_zip_file\n";
my $gz = gzopen ($ARGV[0], "rb") or die "Cannot open $ARGV[0]: $gzerrno\n";
print $buff while $gz->gzreadline($buff) > 0;
die "Error reading from $ARGV[0]: $gzerrno" . ($gzerrno+0) . "\n"
   if $gzerrno != Z_STREAM_END ;
$gz->gzclose();

Thanks,
John



>>> "Wiggins d'Anconia" <[EMAIL PROTECTED]> 09/25/03 05:45PM >>>

Check out the 'Compress::Zlib' module. In particular the 'gzreadline' method. Much 
better than shelling out to do this sort of thing...

http://danconia.org 



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



Re: Perl - HowTo operating with large file?

2003-09-26 Thread Paul William

why not read the file line by line and then simply match each line with
/$something/, disgarding any lines which do not match /$something/.

If you wanted to could push all matching lines into and array.

Cheers

Paul


On Fri, 2003-09-26 at 18:52, Juris wrote:
> I have one small problem!
> HowTo read from large text file text in binary mode?
> 
> if i want read all file, i use this code:
> 
> my (@LOG_FILE);
> open (FL, "/var/log/maillog");
> @LOG_FILE=;
> close (FL);
> #After this code execution file contents stored in array @LOG_FILE
> @LOG_FILE=grep /$something/, @LOG_FILE
> 
> I not want read all file string by struing - it's so slowly, if file is 
> large and contains more than 100 records!
> I need read each 50 bytes, but how?
> Please, help!
> 
> 
> -- 
> With best regards,
> Juris


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



Perl - HowTo operating with large file?

2003-09-26 Thread Juris
I have one small problem!
HowTo read from large text file text in binary mode?
if i want read all file, i use this code:

my (@LOG_FILE);
open (FL, "/var/log/maillog");
@LOG_FILE=;
close (FL);
#After this code execution file contents stored in array @LOG_FILE
@LOG_FILE=grep /$something/, @LOG_FILE
I not want read all file string by struing - it's so slowly, if file is 
large and contains more than 100 records!
I need read each 50 bytes, but how?
Please, help!

--
With best regards,
Juris
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


How to pass parameters to a module

2003-09-26 Thread Rajesh Dorairajan
Can someone explain how does one pass a parameter to a Perl Module? To
illustrate suppose I've My::Module

package My::Module;

BEGIN
{
  $scalar = $input;
}

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = ($scalar);

In the above script is there anyway to pass the $input variable to the
package My::Module from the calling script? Please excuse me if the code is
horrible, just trying to simplify what I want ;)

TIA

Rajesh


Re: Problems with opening a word doc on similar systems

2003-09-26 Thread R. Joseph Newton
[EMAIL PROTECTED] wrote:

> I am facing a very strange problem while i try opening a word document through
> my perl program.
>
> So far, i use the system() function.
>
> system(1, $winword_pathm $word_document);
>
> That works well on a win98 system, but it doesn't on another win98 system.
>
> Those machines have Office 97.
>
> What is the real problem because it drives me mad. Have you ever face such a
> annoying problem?
>
> Please help!!!

Is the path actually the correct path on both machines?  Default installations
locations are not always where a program is found.  On Windows, it should not be
necessary to hard-code the path anyway.  The system executable search path
shouldinclude the folder containing Winword.exe if Office has been properly
installed.  All you should need then is:
system("winword $word_document");

On Win2K, its even easier.  The command environment in NT integrates much better
with file associations, so I just need to:
system($word_document);

Joseph


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