[24542] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6720 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 23 21:08:01 2004

Date: Wed, 23 Jun 2004 18:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 23 Jun 2004     Volume: 10 Number: 6720

Today's topics:
        DBD::Sybase, FreeTDS problem (Daniel Berger)
        Find length of files <michaelp@hio.no>
    Re: Find length of files <postmaster@castleamber.com>
    Re: Find length of files <rustyp@freeshell.org>
    Re: Find length of files <1usa@llenroc.ude>
    Re: Find length of files <postmaster@castleamber.com>
        help with perl dbi and update without locks <jcharth@hotmail.com>
    Re: help with perl dbi and update without locks <1usa@llenroc.ude>
    Re: Help! - Need a CGI redirect which passes a querystr <noreply@gunnar.cc>
    Re: Help! - Need a CGI redirect which passes a querystr <flavell@ph.gla.ac.uk>
    Re: Help! - Need a CGI redirect which passes a querystr (Damon)
    Re: Idiom for partitioning array? (Anno Siegel)
    Re: Idiom for partitioning array? <bmb@ginger.libs.uga.edu>
    Re: if statement w/hash and subroutines <gnari@simnet.is>
    Re: Perl editors and debuggers <filipe@nospam.thanks>
        REGEX Negation <rustyp@freeshell.org>
    Re: split xml file between two processing instructions <tadmc@augustmail.com>
    Re: Unable to send email from perl script (David Efflandt)
        Using closures with "use strict" <this.is@invalid>
    Re: Using closures with "use strict" (Malcolm Dew-Jones)
    Re: Using closures with "use strict" <usenet@morrow.me.uk>
    Re: Using closures with "use strict" <dwall@fastmail.fm>
    Re: WWW::RobotRules <postmaster@castleamber.com>
    Re: WWW::RobotRules <usenet@morrow.me.uk>
    Re: WWW::RobotRules <boatingN.O.S.P.A.M@cox.net>
    Re: WWW::RobotRules <jayNOSPAM@tequila-stuff.com>
    Re: WWW::RobotRules <usenet@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 23 Jun 2004 14:15:34 -0700
From: djberg96@hotmail.com (Daniel Berger)
Subject: DBD::Sybase, FreeTDS problem
Message-Id: <6e613a32.0406231315.3754c2db@posting.google.com>

Hi all,

Perl 5.8.2
Solaris 9
FreeTDS 0.63 (20040622)
DBI 1.42
DBD::Sybase 1.04

I installed FreeTDS 0.63 successfully.  I can definitely connect using
tsql.  However, when I try this simple script I get an error.

use strict;
use warnings;
use DBI;
use DBD::Sybase;

my $login  = "user";
my $passwd = "xxxx";
my $server = "SOME-SERVER";
my $host   = "somehost";
my $port   = 1030;

$ENV{SYBASE} = "/usr/local";
my $dbh = DBI->connect("dbi:Sybase:server=$server;host=$host;port=$port",$login,
$passwd);

$dbh->disconnect();

The error is: This version of OpenClient doesn't support CS_SERVERADDR
at /opt/csw/lib/perl/site_perl/DBD/Sybase.pm line 87.

I saw nothing in the archives or on Google about this.  Any ideas?

Regards,

Dan


------------------------------

Date: Wed, 23 Jun 2004 22:17:17 +0200
From: Michael Preminger <michaelp@hio.no>
Subject: Find length of files
Message-Id: <cbcogf$132$1@dolly.uninett.no>

Hello!

I have a huge directory, for which I need the word-count of all files 
(like wc -w * , and then put all length into a database)

Is there a smart way to do it in perl? (apart from wc * > file and then 
open the file..)

Thanks

Michael


------------------------------

Date: Wed, 23 Jun 2004 15:21:04 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Find length of files
Message-Id: <40d9e633$0$870$58c7af7e@news.kabelfoon.nl>

Michael Preminger wrote:

> Hello!
> 
> I have a huge directory, for which I need the word-count of all files 
> (like wc -w * , and then put all length into a database)
> 
> Is there a smart way to do it in perl? (apart from wc * > file and then 
> open the file..)

foreach file
open file,
put each word in a hash, like $words{$word}++;
this way you get the count of each word.

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


------------------------------

Date: Wed, 23 Jun 2004 17:44:00 -0500
From: Rusty Phillips <rustyp@freeshell.org>
Subject: Re: Find length of files
Message-Id: <pan.2004.06.23.22.43.58.727583@freeshell.org>

The wordcount is not the number of times a word occurs, John.  
It's the number of words in a file.

Using wc will probably be faster than the perl way (unless there's a 
perl command for doing wordcounts I'm not aware of), but you don't 
need to open a file.
Just use a pipe.

http://www.devdaily.com/perl/edu/articles/pl010004.shtml


------------------------------

Date: 23 Jun 2004 23:19:51 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: Find length of files
Message-Id: <Xns9511C4A634644asu1cornelledu@132.236.56.8>

Rusty Phillips <rustyp@freeshell.org> wrote in 
news:pan.2004.06.23.22.43.58.727583@freeshell.org:

> The wordcount is not the number of times a word occurs, John.  
> It's the number of words in a file.

Just a minor point: You _can_ obtain the number of words in a file by 
adding the number of occurances of each word in the file.

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


------------------------------

Date: Wed, 23 Jun 2004 18:21:19 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Find length of files
Message-Id: <40da1074$0$866$58c7af7e@news.kabelfoon.nl>

Rusty Phillips wrote:

> The wordcount is not the number of times a word occurs, John.  
> It's the number of words in a file.

Ah, ok, then do something like:

$words_in_file{$filename}++;

for every word you find :-D.

> Using wc will probably be faster than the perl way (unless there's a 
> perl command for doing wordcounts I'm not aware of),

Uhm, I guess for every file you have to fork, so I doubt it.

Hard to say, without benchmarking.

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


------------------------------

Date: Wed, 23 Jun 2004 23:12:17 GMT
From: joe <jcharth@hotmail.com>
Subject: help with perl dbi and update without locks
Message-Id: <Xns9511C2C8B6106josephthecianet@207.69.154.202>

i have a table with the fields pk and user; i am trying to avoid conflics 
when 2 or more users do a select max(pk) and then insert pkmaxvalue, user.
i created a function that assigns max(pk) to a variable and then uses this 
variable to create a record with the variable and another field for the 
user that created this record.  I am trying to avoid conflicts by retriving 
the user from the record after the update. if the user does not match i 
created a loop to create a record until the user in the record is equal to 
the user that created the record. would this work?
 i am trying to make the script work with mysql, unixodbc and iodbc and i 
am trying to avoid locks because i dont know if i can do 
$dbh->do("lock table my table in exclusive mode");
 for all dbi and dbiodbc implementations. 


------------------------------

Date: 23 Jun 2004 23:35:33 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: help with perl dbi and update without locks
Message-Id: <Xns9511C74EEE14Dasu1cornelledu@132.236.56.8>

joe <jcharth@hotmail.com> wrote in
news:Xns9511C2C8B6106josephthecianet@207.69.154.202: 

> i have a table with the fields pk and user; i am trying to avoid
> conflics when 2 or more users do a select max(pk) and then insert
> pkmaxvalue, user. i created a function that assigns max(pk) to a
> variable and then uses this variable to create a record with the
> variable and another field for the user that created this record.  I
> am trying to avoid conflicts by retriving the user from the record
> after the update. if the user does not match i created a loop to
> create a record until the user in the record is equal to the user that
> created the record. would this work? 

I am not sure exactly what you are doing based on the description above. 
It is preferable to post a short example in Perl of what you are doing so 
as to remove any ambiguity.

> i am trying to make the script work with mysql, unixodbc and iodbc
> and i am trying to avoid locks because i dont know if i can do 
> $dbh->do("lock table my table in exclusive mode");
> for all dbi and dbiodbc implementations. 

I might be missing something but just using a lock file is probably going 
to be much simpler:

use Fcntl qw(:flock);

sub update {

   # ...

   open my $lock, '>', 'lockfile' 
    	or die "Cannot open lockfile: $!";
   flock $lock, LOCK_EX 
    	or die "Cannot obtain exclusive lock on lockfile: $!";

   # Do the updating etc.

}

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


------------------------------

Date: Wed, 23 Jun 2004 22:52:47 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Help! - Need a CGI redirect which passes a querystring  value
Message-Id: <2ju8uvF15tn5fU1@uni-berlin.de>

Alan J. Flavell wrote:
> As for the history of Gunnar recommending solutions which bypass 
> CGI.pm, the relevant discussions from past exchanges can be 
> consulted at the usual news archives.

Even if I disagree on your using of the word "bypass", I won't argue
this time.

> I don't see any benefit in going over them yet again.

That's fair. So, why did you refer to them in the first place? If you
disapprove of the suggestion I posted in this thread, wouldn't it have
been more suitable to say so?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Wed, 23 Jun 2004 22:05:45 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: Help! - Need a CGI redirect which passes a querystring  value
Message-Id: <Pine.LNX.4.53.0406232200430.32436@ppepc56.ph.gla.ac.uk>

On Wed, 23 Jun 2004, Gunnar Hjalmarsson wrote:

> > I don't see any benefit in going over them yet again.
>
> That's fair. So, why did you refer to them in the first place?

To encourage anyone who's seriously interested to go and check a news
archive, if they haven't already done so.

> If you disapprove of the suggestion

My views are more complex than that, as I thought I had made clear
before.  But I don't want to get involved in troll-feeding again:
you are sufficiently acquainted with the climate around here to work
out for yourself whether you want to get dragged-in or not, and I've
really said all that I thought it worthwhile to say.

good luck.

-- 
    I'm afraid you have fallen into a well-known trap of understanding
    what is going on here.  -Phil Hazel on exim-users


------------------------------

Date: 23 Jun 2004 14:47:07 -0700
From: damon@tribbledesigns.com (Damon)
Subject: Re: Help! - Need a CGI redirect which passes a querystring  value
Message-Id: <fcd2095.0406231347.6183658c@posting.google.com>

"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message news:<fb4Cc.25944$Nz.1083541@news20.bellglobal.com>...
> "Damon" <damon@tribbledesigns.com> wrote in message
> news:fcd2095.0406221552.429e37ea@posting.google.com...
> > Thanks Matt,
> >
> > My only problem now is that this is all happening on a windows server.
> > The code you provided doesn't seem to work in this environment. I just
> > get the following error: "%1 is not a valid Win32 application."
> >
> > You see, we have several links hard-coded into our desktop application
> > like "http://www.mydomain.com/myPage.cgi?cnt=50" which now need to
> > point to "http://www.mydomain.com/myPage.aspx?cnt=50".
> >
> > Is there any way to make myPage.cgi?cnt=50 redirect and preserve the
> > querystring on a windows server?
> >
> 
> <please don't top-post>
> 
> The script I provided should work regardless of what value of cnt is passed
> to it. The error you note sounds like it's in the way that you have the .cgi
> extension mapped. Do any .cgi scripts work on your server?
> 
> Matt

Hi all,

Sorry about the top-posting.

Thanks to all for your help. Both Matt and Gunnar's solutions solved
my problem. I found out that the Windows server I am hosted on was not
correctly setup to process CGI. Once this was fixed, both scripts
worked.

Thanks for all your help! I really appreciate it...

-Damon


------------------------------

Date: 23 Jun 2004 22:35:22 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Idiom for partitioning array?
Message-Id: <cbd0ja$i99$1@mamenchi.zrz.TU-Berlin.DE>

Brad Baxter  <bmb@ginger.libs.uga.edu> wrote in comp.lang.perl.misc:
> On Wed, 23 Jun 2004, Anno Siegel wrote:
> 
> > J Krugman  <jkrugman345@yahbitoo.com> wrote in comp.lang.perl.misc:
> > >
> > >
> > > Is there a nice Perl idiom for partitioning an array of length N
> > > into "subarrays of length n" (i.e. array refs pointing to arrays
> > > of length n)?  (If N is not a multiple of n, the last entry of the
> > > partitioned array should be a subarray of length N mod n.) For
> > > example, if n = 3, and the input array is
> > >
> > >   ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
> > >
> > > the partitioned array should be:
> > >
> > >   (['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h'])
> >
> >     map [ splice @a, 0, $n], 0 .. $#a/$n;
> 
> Can't beat that.

It isn't entirely correct, however.  If @a is empty (and $n > 1), it
returns one empty slice, but should return none.  That's because
the implicit int( $#a/$n) is 0, not something negative, when $#a = -1.

>                  OP's "input array" vs. "partitioned array" makes me
> think that he might want to keep the original array.  Perhaps

Yes, we were all very destructive in this thread, weren't we.  Time
for a conservative solution.

> sub min{$_[0]<$_[1]?$_[0]:$_[1]}

   sub min { $_[ $_[ 1] < $_[ 0] ] } # :)

> my @r = map [ @a[ $_*$n .. min ++$_*$n-1, $#a ] ], 0 .. $#a/$n;

Yup.

My spelling checker suggests "TNT" for "int" and "mine" for "min".
It must be in a destructive mood, too.

Anno


------------------------------

Date: Wed, 23 Jun 2004 20:50:14 -0400
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Idiom for partitioning array?
Message-Id: <Pine.A41.4.58.0406232029510.13442@ginger.libs.uga.edu>

On Wed, 23 Jun 2004, Anno Siegel wrote:

> Brad Baxter  <bmb@ginger.libs.uga.edu> wrote in comp.lang.perl.misc:

> > On Wed, 23 Jun 2004, Anno Siegel wrote:

> > > J Krugman  <jkrugman345@yahbitoo.com> wrote in comp.lang.perl.misc:

> > > > Is there a nice Perl idiom for partitioning an array of length N
> > > > into "subarrays of length n" (i.e. array refs pointing to arrays
> > > > of length n)?  (If N is not a multiple of n, the last entry of the
> > > > partitioned array should be a subarray of length N mod n.) For
> > > > example, if n = 3, and the input array is
> > > >
> > > >   ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
> > > >
> > > > the partitioned array should be:
> > > >
> > > >   (['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h'])
> > >
> > >     map [ splice @a, 0, $n], 0 .. $#a/$n;
> >
> > Can't beat that.
>
> It isn't entirely correct, however.  If @a is empty (and $n > 1), it
> returns one empty slice, but should return none.  That's because
> the implicit int( $#a/$n) is 0, not something negative, when $#a = -1.

Which behavior the non-destructive version also exhibits.

 ... if @a;

perhaps?


> >                  OP's "input array" vs. "partitioned array" makes me
> > think that he might want to keep the original array.  Perhaps
>
> Yes, we were all very destructive in this thread, weren't we.  Time
> for a conservative solution.
>
> > sub min{$_[0]<$_[1]?$_[0]:$_[1]}
>
>    sub min { $_[ $_[ 1] < $_[ 0] ] } # :)

I wish I'd said that.  :-)


> > my @r = map [ @a[ $_*$n .. min ++$_*$n-1, $#a ] ], 0 .. $#a/$n;
>
> Yup.

my @r;
@r = map [ @a[ $_*$n .. min ++$_*$n-1, $#a ] ], 0 .. $#a/$n if @a;


> My spelling checker suggests "TNT" for "int" and "mine" for "min".
> It must be in a destructive mood, too.

If computers were smarter than us, THEY'D be earning the big bucks.

Regards,

Brad



------------------------------

Date: Wed, 23 Jun 2004 23:04:25 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: if statement w/hash and subroutines
Message-Id: <cbd264$o0g$1@news.simnet.is>

"Mark Connell" <mhc5@duke.edu> wrote in message
news:64211cc6.0406220613.56a07862@posting.google.com...
>
> sub MICe {
>
> %Seq=@_;
> lots of calculations...then I print to a file.
> I tried to include the printing by doing this:
> if ( @_ == %Newsq) {
> print DAA so on to a file.
> }
> else {print something else}
> }
>
>
> I later cued the subroutine by this line:
>
> MICe(%Newsq);
> or
> MICe(%Seqac);
>
> I think that includes all the relevant code.  Would references be
> helpful?  I haven't learned much about them yet so I'm not sure if
> they would be.

if you hardcode the 2 possible hashes (for example by using refs),
you are just making this more difficult to maintain.

rather, add an argument to specify the different types.

    MICe(1,%Newsq);
    MICe(0,%Seqac);

    sub MICe {
        my ($action,%Seq)=@_;
        ...
        if ($action) {
            ...
        } else {
            ...
        }
    }

gnari






------------------------------

Date: Wed, 23 Jun 2004 20:20:06 +0200
From: Filipe <filipe@nospam.thanks>
Subject: Re: Perl editors and debuggers
Message-Id: <pan.2004.06.23.18.20.06.782811@nospam.thanks>

Hi,

> Just looking for recommendations.

On Windows, I like PSPad (http://www.pspad.com/).

Filipe


------------------------------

Date: Wed, 23 Jun 2004 17:29:26 -0500
From: Rusty Phillips <rustyp@freeshell.org>
Subject: REGEX Negation
Message-Id: <pan.2004.06.23.22.29.25.534688@freeshell.org>

I know about negative lookahead and negative character closures, 
but I can't find any good way to do actual negation.

One thing I'd like to use this for is to match quotes while 
guaranteeing that I'm not matching backslashed quotes (that is, if I
find a backslash in the string, the quote in front of it should not
be matched).
This string:
String = q{She said, 
Welcome to the party\\""}

Should match 
He said, \\"Welcome to the party\\"
as the part within the quotes, and not
He said, \\

There are many more places where I'd like to use a negation 
technique - especially I'd like to match things of the form:
"match the largest string that doesn't contain the character sequence 
'blah.'"

Are there any ways to do either of these types of negation?


------------------------------

Date: Wed, 23 Jun 2004 13:40:15 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: split xml file between two processing instructions
Message-Id: <slrncdjjkf.avm.tadmc@magna.augustmail.com>

kcwolle <kcwolle@freenet.de> wrote:

> I want to split an xml file on processing instructions into different
> files.


Does it have to work on arbitrary XML or only on "your" XML?

Might you have PIs like this?

   <?split             ?>
or
   <?split
   ?>

If so, you're on your own. If not, see below.


> All content between the two PIs should be included in the new file.
> The file name should contain the content of first and the last <no>
> elements.


There are no <no> elements...


> example:
><?split ?>
><h1>... text ...</h1>
><start-element/>
><text>
> ...text text text...
><nr>4</nr>
></text>
> text text text
><nr>18</nr>
><end-element/>
><h6> ... text ...</h6>
><?split ?>
> 
> In this case the file name should be: test-no4to18.xml and everything
> from <h1> to </h6> should be included.


> I would like to use an XML module 


Since you don't need to make use of the XML structuring, I would
treat them as plain ol' text files.


> Can anybody help me?


What have you tried so far?

We generally prefer to help those who have attempted to help
themselves first...


This should get you started:

   foreach my $section ( split /\Q<?split ?>/ ) {
      my( $num1, $num2) = ($section =~ /<nr>(\d+)/g)[0, -1];
      next unless defined $num1;
      my $fname = "text-no${num1}to$num2.xml";
      print "$fname\n";
   }


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Thu, 24 Jun 2004 00:02:08 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Unable to send email from perl script
Message-Id: <slrncdk6fv.o1p.efflandt@typhoon.xnet.com>

On 23 Jun 2004 00:27:59 -0700, da_sotong <bobo_shooter85@hotmail.com> wrote:
> Hi all, 
> 
> I have a perl script that does the sending of email. However i keep
> getting the same error when i run the script:
> 
> Failed to connect to mail server: Unknown error
> 
> I'm quite sure that the codes are correct (as the script COULD work on
> my colleagues' machine)..i'm suspecting that it's my configuration of
> my machine...I'm using win2k for now...installed all the necessary
> modules and IIS ....wat could be the error?

What Perl method are you using (module?) and what smtp server are you 
attempting to connect to?  Perhaps it is attempting to relay through an 
smtp server on localhost, and you are not running one, or it is not 
properly configured.  Or maybe you have a firewall that is blocking it.

The open relay(s) that some modules may have used in the past may no
longer be open, due to spam abuse.  Although, if you use a module like
Mail::Sendmail, you may be able to configure it for a specific destination
smtp server (one that is MX for destination address).

-- 
David Efflandt - All spam ignored  http://www.de-srv.com/


------------------------------

Date: Wed, 23 Jun 2004 22:23:39 +0200
From: ddtl <this.is@invalid>
Subject: Using closures with "use strict"
Message-Id: <nkpjd01jquhqinj084vb9ljjeuspr5vq5p@4ax.com>


Hello,

If I define a "use strict" and want to create a closure,
how can I refer to the anonymous functions defined in that 
closure? Consider the following example:

-------------------------------------------
use strict;

{
	our $f = sub {print "Hi\n";};
}

&$f;
-------------------------------------------

Here compiler complains, and reports an error:

"Variable "$f" is not imported at ~/test line 7."

Obviously, though 'f' is declared using 'our' modifier, as there
is no such a global variable called 'f', using 'our $f;' creates
some kind of restricted global variable (don't know even how to
call it - that variable works like a global one, but it's "globalness"
is restricted to the enclosing block).

In order to rectify it (and still have the benefits of "use strict", 
there are two possibilities that I can see:

1) Enclose closure and every call to anonymous function defined in 
the closure in "no strict":

------------------------------------
use strict;

{
	no strict;
	$f = sub {print "Hi\n";};
	use strict;

}

#...
#...

no strict;
&$f;
use strict;
#...
#...
------------------------------------

2) Define all the references to anonymous functions in closure as
"our $REF_NAME" somewhere outside the block:


------------------------------------
use strict;

{
	our $f = sub {print "Hi\n";};
}
our $f;

&$f;
------------------------------------


Another solution can maybe involve some tricks with package declarations,
but I am only in the "Chapter 8" of "Programming Perl", so I can't
be sure.


The problem is, that all those solutions are looking very contrived
and ugly. 

Is there is a better way to do that?


ddtl.




------------------------------

Date: 23 Jun 2004 12:37:26 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Using closures with "use strict"
Message-Id: <40d9dbf6@news.victoria.tc.ca>

ddtl (this.is@invalid) wrote:

: Hello,

: If I define a "use strict" and want to create a closure,
: how can I refer to the anonymous functions defined in that 
: closure? Consider the following example:

: -------------------------------------------
: use strict;

: {
: 	our $f = sub {print "Hi\n";};
: }

: &$f;
: -------------------------------------------

: Here compiler complains, and reports an error:

: "Variable "$f" is not imported at ~/test line 7."

: Obviously, though 'f' is declared using 'our' modifier, as there
: is no such a global variable called 'f', using 'our $f;' creates
: some kind of restricted global variable (don't know even how to
: call it - that variable works like a global one, but it's "globalness"
: is restricted to the enclosing block).
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Yes (sort of), that's what "our" does.  It tells the compiler you intend 
to access the global variable $f, but only within the current scope.

If you intend to access the same global variable within a different scope 
then it must also be defined within that different scope as well.

There are various things you could do.  (most examples untested)

#1
	use strict;
	my $f;
	{
	   $f = sub {print "Hi\n";};
	}
	&$f;

#2
	use strict;
	our $f;
	{
	   $f = sub {print "Hi\n";};
	}
	&$f;

#3
	use strict;
	{
	   our $f = sub {print "Hi\n";};
	}
	our $f;
	&$f;
#4
	use strict;
	{
	   our $f = sub {print "Hi\n";};
	}
	{
	   our $f;
	   &$f;
	}

#5
	use strict;
	yse vars '$f';
	{
	   $f = sub {print "Hi\n";};
	}
	&$f;

and possibly others.  Unless $f must be accessed by routines in different
files then the #1 (my $f) is probably best.  However, "our" does have the
advantage that you can declare it within smaller blocks as needed (#4),
which makes it available to less than the entire file (unlike my, where
everything after the declaration must see the variable if any of the code
is to see it).


------------------------------

Date: Wed, 23 Jun 2004 19:42:00 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Using closures with "use strict"
Message-Id: <cbcme8$qaj$2@wisteria.csv.warwick.ac.uk>


Quoth ddtl <this.is@invalid>:
> 
> If I define a "use strict" and want to create a closure,
> how can I refer to the anonymous functions defined in that 
> closure? Consider the following example:
> 
> -------------------------------------------
> use strict;
> 
> {
> 	our $f = sub {print "Hi\n";};
> }
> 
> &$f;
> -------------------------------------------
> 
> Here compiler complains, and reports an error:
> 
> "Variable "$f" is not imported at ~/test line 7."

The variable declared by 'our' is a lexically-scoped alias to the
package global, so you need to put it in the same place as you would put
an equivalent 'my' variable. What you want is

use strict;

our $f;
{
    # presumably there is more code here?

    $f = sub {...};
}

# I prefer this style of indirect function call, as it matches the other
# deref ops and doesn't lead to potential confusion about calling named
# subs with &.

$f->();

or perhaps

our $f = do {
    ...
    sub {...};
};

Ben

-- 
           All persons, living or dead, are entirely coincidental.
ben@morrow.me.uk                                                  Kurt Vonnegut


------------------------------

Date: Wed, 23 Jun 2004 20:19:34 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Using closures with "use strict"
Message-Id: <Xns9511A61425433dkwwashere@216.168.3.30>

ddtl <this.is@invalid> wrote in message
<news:nkpjd01jquhqinj084vb9ljjeuspr5vq5p@4ax.com>: 

> If I define a "use strict" and want to create a closure,
> how can I refer to the anonymous functions defined in that 
> closure? Consider the following example:
> 
> -------------------------------------------
> use strict;
> 
> {
>      our $f = sub {print "Hi\n";};
>}
> 
> &$f;
> -------------------------------------------

That doesn't fit my idea of a closure. It's just a code reference. 
As you noted, the code above is broken because variables declared 
with our() are lexically scoped. 

A closure (as I understand it) is a subroutine that keeps track of 
lexical variables that are within scope when that subroutine is 
defined, so that later it can still access those variables even 
though they're out of the present scope.

For example:

    use strict;

    my $f;
    {
        my $string = 'test';
        sub set_string { $string = $_[0] }
        sub get_string { $string }
        $f = \&get_string;
    }
    
    print get_string(), "\n";
    set_string('something else');
    print get_string(), "\n";

    # call get_string() using a code reference
    print $f->(), "\n";
    
    # this statement will generate an error, because $string is 
    # now out of scope
    print "\$string = $string\n";


set_string() and get_string() are closures. $f is just a code 
reference to what happens to be a closure.



------------------------------

Date: Wed, 23 Jun 2004 13:10:35 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: WWW::RobotRules
Message-Id: <40d9c79d$0$882$58c7af7e@news.kabelfoon.nl>

Jay wrote:

> I'm having some problems with robotrules where it doesn't seem to work.  I'm
> using the demo code and it doesn't seem to match directories when the
> robots.txt file has trailing slashes.  For example on my site I have in my
> robots.txt file
> 
> User-agent: *
> Disallow: /sptrap/
> 
> If I the program if I am allowed to access
> http://www.tequila-stuff.com/sptrap/ it works fine, if I ask it about
> http://www.tequila-stuff.com/sptrap it does not report that I'm not allowed
> to access it.  Is this an issue with my robots.txt file or with robotrules?

Technically, sptrap and sptrap/ are two different things. If you mean 
the latter, add *always* the trailing slash. AFAIK Apache, and most 
likely other webservers, check first if there is a *file* sptrap, and 
then a directory sptrap/. So technically (I guess) your URI is wrong.

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


------------------------------

Date: Wed, 23 Jun 2004 19:36:19 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: WWW::RobotRules
Message-Id: <cbcm3j$qaj$1@wisteria.csv.warwick.ac.uk>


Quoth John Bokma <postmaster@castleamber.com>:
> Jay wrote:
> 
> > I'm having some problems with robotrules where it doesn't seem to work.  I'm
> > using the demo code and it doesn't seem to match directories when the
> > robots.txt file has trailing slashes.  For example on my site I have in my
> > robots.txt file
> > 
> > User-agent: *
> > Disallow: /sptrap/
> > 
> > If I the program if I am allowed to access
> > http://www.tequila-stuff.com/sptrap/ it works fine, if I ask it about
> > http://www.tequila-stuff.com/sptrap it does not report that I'm not allowed
> > to access it.  Is this an issue with my robots.txt file or with robotrules?
> 
> Technically, sptrap and sptrap/ are two different things. If you mean 
> the latter, add *always* the trailing slash. AFAIK Apache, and most 
> likely other webservers, check first if there is a *file* sptrap, and 
> then a directory sptrap/. So technically (I guess) your URI is wrong.

Apache (and most other webservers I think) will issue a redirect to
/sptrap/ if you request /sptrap and it is a directory. So if you are
using LWP or whatever and you insert your RobotRules checks after every
redirect (as you should) you will catch it after the first.

Ben

-- 
perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
2047502190/'                                                 # ben@morrow.me.uk


------------------------------

Date: Wed, 23 Jun 2004 16:48:05 -0400
From: "Jay" <boatingN.O.S.P.A.M@cox.net>
Subject: Re: WWW::RobotRules
Message-Id: <_ZlCc.83$CR3.71@lakeread03>

"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:cbcm3j$qaj$1@wisteria.csv.warwick.ac.uk...
>
> Quoth John Bokma <postmaster@castleamber.com>:
> > Jay wrote:
> >
> > > I'm having some problems with robotrules where it doesn't seem to
work.  I'm
> > > using the demo code and it doesn't seem to match directories when the
> > > robots.txt file has trailing slashes.  For example on my site I have
in my
> > > robots.txt file
> > >
> > > User-agent: *
> > > Disallow: /sptrap/
> > >
> > > If I the program if I am allowed to access
> > > http://www.tequila-stuff.com/sptrap/ it works fine, if I ask it about
> > > http://www.tequila-stuff.com/sptrap it does not report that I'm not
allowed
> > > to access it.  Is this an issue with my robots.txt file or with
robotrules?
> >
> > Technically, sptrap and sptrap/ are two different things. If you mean
> > the latter, add *always* the trailing slash. AFAIK Apache, and most
> > likely other webservers, check first if there is a *file* sptrap, and
> > then a directory sptrap/. So technically (I guess) your URI is wrong.
>
> Apache (and most other webservers I think) will issue a redirect to
> /sptrap/ if you request /sptrap and it is a directory. So if you are
> using LWP or whatever and you insert your RobotRules checks after every
> redirect (as you should) you will catch it after the first.
>
> Ben
>
> --
> perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
> qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
> 1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
> 2047502190/'                                                 #
ben@morrow.me.uk


Ok, I see.  There are 301 redirects that I'm ignoring, that's the problem.

Thanks alot guys.





------------------------------

Date: Wed, 23 Jun 2004 18:43:47 -0400
From: "news" <jayNOSPAM@tequila-stuff.com>
Subject: Re: WWW::RobotRules
Message-Id: <MGnCc.900$CR3.653@lakeread03>

"Jay" <boatingN.O.S.P.A.M@cox.net> wrote in message
news:_ZlCc.83$CR3.71@lakeread03...
> "Ben Morrow" <usenet@morrow.me.uk> wrote in message
> news:cbcm3j$qaj$1@wisteria.csv.warwick.ac.uk...
> >
> > Quoth John Bokma <postmaster@castleamber.com>:
> > > Jay wrote:
> > >
> > > > I'm having some problems with robotrules where it doesn't seem to
> work.  I'm
> > > > using the demo code and it doesn't seem to match directories when
the
> > > > robots.txt file has trailing slashes.  For example on my site I have
> in my
> > > > robots.txt file
> > > >
> > > > User-agent: *
> > > > Disallow: /sptrap/
> > > >
> > > > If I the program if I am allowed to access
> > > > http://www.tequila-stuff.com/sptrap/ it works fine, if I ask it
about
> > > > http://www.tequila-stuff.com/sptrap it does not report that I'm not
> allowed
> > > > to access it.  Is this an issue with my robots.txt file or with
> robotrules?
> > >
> > > Technically, sptrap and sptrap/ are two different things. If you mean
> > > the latter, add *always* the trailing slash. AFAIK Apache, and most
> > > likely other webservers, check first if there is a *file* sptrap, and
> > > then a directory sptrap/. So technically (I guess) your URI is wrong.
> >
> > Apache (and most other webservers I think) will issue a redirect to
> > /sptrap/ if you request /sptrap and it is a directory. So if you are
> > using LWP or whatever and you insert your RobotRules checks after every
> > redirect (as you should) you will catch it after the first.
> >
> > Ben
> >
> > --
> > perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
> > qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
> > 1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
> > 2047502190/'                                                 #
> ben@morrow.me.uk
>
>
> Ok, I see.  There are 301 redirects that I'm ignoring, that's the problem.
>
> Thanks alot guys.
>
>
>

Well, I'm using LWP::UserAgent to actually get the the directory that is
generating the redirect.  I'm seeing a status 200 come back from the get and
from what I can tell LWP::UserAgent will keep processing up to 7 redirects
automatically.  Is there an easy way to get LWP::UserAgent not to
automatically redirect and allow me to check the robots in between each get?

Thanks,
Jay







------------------------------

Date: Thu, 24 Jun 2004 00:42:40 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: WWW::RobotRules
Message-Id: <cbd820$2tm$1@wisteria.csv.warwick.ac.uk>


Quoth "news" <jayNOSPAM@tequila-stuff.com>:
> "Jay" <boatingN.O.S.P.A.M@cox.net> wrote in message
> news:_ZlCc.83$CR3.71@lakeread03...
> > "Ben Morrow" <usenet@morrow.me.uk> wrote in message
> > news:cbcm3j$qaj$1@wisteria.csv.warwick.ac.uk...
> > >
> > > Apache (and most other webservers I think) will issue a redirect to
> > > /sptrap/ if you request /sptrap and it is a directory. So if you are
> > > using LWP or whatever and you insert your RobotRules checks after every
> > > redirect (as you should) you will catch it after the first.

[there's no need to quote someone's signature]

> > Ok, I see.  There are 301 redirects that I'm ignoring, that's the problem.
> 
> Well, I'm using LWP::UserAgent to actually get the the directory that is
> generating the redirect.  I'm seeing a status 200 come back from the get and
> from what I can tell LWP::UserAgent will keep processing up to 7 redirects
> automatically.  Is there an easy way to get LWP::UserAgent not to
> automatically redirect and allow me to check the robots in between each get?

Subclass LWP::UserAgent and override redirect_ok. I would have thought
there was a class on CPAN that did that for robots files already.

Ben

-- 
perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
2047502190/'                                                 # ben@morrow.me.uk


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6720
***************************************


home help back first fref pref prev next nref lref last post