[26509] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8665 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 14 18:05:40 2005

Date: Mon, 14 Nov 2005 15:05:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 14 Nov 2005     Volume: 10 Number: 8665

Today's topics:
    Re: Inject fresh variable in scope brianr@liffe.com
    Re: Looking for Equation Solver <bland@uNIVOFmich.edu.REMOVE.UPPER.CASE>
    Re: Looking for Equation Solver xhoster@gmail.com
        Pattern Matching problem! <francis@nospam.com>
    Re: strange problem with system() and backtick operator <news@chaos-net.de>
    Re: strange problem with system() and backtick operator <1usa@llenroc.ude.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 14 Nov 2005 16:45:43 +0000
From: brianr@liffe.com
Subject: Re: Inject fresh variable in scope
Message-Id: <vtwtjbp494.fsf@ssdevws28.admin.liffe.com>

"Eric J. Roode" <sdn.girths00869@zoemail.net> writes:

> "Andrei Alexandrescu (See Website For Email)"
> <SeeWebsiteForEmail@moderncppdesign.com> wrote in
> news:Ipv6pH.1tB0@beaver.cs.washington.edu: 
>
>> Hello,
>> 
>> 
>> I set out to write a useful OnScopeExit feature that's supposed to
>> execute some code when the current scope is exited. What I came up
>> with is: 
>> 
>> package OnScopeExit;
>> my $todo = sub {};
>> sub new { shift; $todo = shift; bless {}; }
>> sub DESTROY {
>>    &$todo;
>> }
>> package main;
>> sub OnScopeExit(&) {
>>    return new OnExit(shift);
>> }
>> 
>> Now I can say in any scope I please:
>> 
>> {
>>    ... code ...
>>    my $tempFile = "/tmp/temp.txt";
>>    my $cookie1 = OnScopeExit { unlink($tempFile); };
>>    ... code ...
>> }
>> 
>> Sure enough, when the current scope exits, the local $cookie1 will
>> have its reference count go to zero, the DESTROY sub gets called, and
>> the code that deletes the temporary file is invoked. Very nice.
>> 
>> The only annoying part is that I need to define some "my" variable
>> with a unique name for each OnScopeExit action I want to perform.
>> Ideally, I'd only need to write:
>> 
>> {
>>    ... code ...
>>    my $tempFile = "/tmp/temp.txt";
>>    OnScopeExit { unlink($tempFile); }
>>    ... code ...
>> }
>> 
>> that is, the creation of a lexically-scoped anonymous variable that
>> will go out of scope should be somehow automated.
>> 
>> I looked through symbol tables, scratchpads, and the ilk, for a couple
>> of hours, to no avail. Is there a way?
>
> I don't believe so.  I tried this same sort of thing a while back, 
> and I couldn't think of any way.
>
> Two things about your object class, above.  One, you should not use
> a package variable ($todo) to store your action.  What if someone needed
> to do this?
>
>     {
>         my $one = OnScopeExit {...};
>         {
>             my $two = OnScopeExit {...};
>         }
>     }
>
> For each object, store the to-do action *in* the object itself.
> Instead of:
>> sub new { shift; $todo = shift; bless {}; }
> do
>   sub new { shift; bless { todo => shift }; }
>
> Second: Don't hardcode OnScopeExit to be in package main.  What if
> you need an end-of-scope action in a module you're developing, a
> module that exists in a different namespace than "main"?  Export it
> properly, via the Exporter module.

ISTM that it could be done with a source filter e.g.

use strict;
use warnings;

package OnScopeExitHelper;

sub new ($&) {
    shift;
    return bless {code => shift};
}

sub DESTROY {
    &{$_[0]->{code}};
}

package UniqueVarName;

require Tie::Scalar;
our @ISA = qw /Tie::StdScalar/;

our $base_var = "aaaaaaaaa";

sub FETCH {
    return $base_var++;
}

package OnScopeExit;

use Filter::Simple;
use Regexp::Common qw /balanced/;

our $var;
tie $var, 'UniqueVarName';

FILTER_ONLY code => sub { s{\b OnScopeExit \b
			    \s+
			    ( $RE{balanced}{-parens=>'{}'} )
			   }
			   {my \$$var = OnScopeExitHelper->new(sub $1)
                           }gsx
		       };

1;

Seems to work, but I expect that it could be improved.

HTH

-- 
Brian Raven
That should probably be written:
    no !@#$%^&*:@!semicolon
             -- Larry Wall in <199710161841.LAA13208@wall.org>


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

Date: Mon, 14 Nov 2005 13:55:12 -0500
From: Peyton Bland <bland@uNIVOFmich.edu.REMOVE.UPPER.CASE>
Subject: Re: Looking for Equation Solver
Message-Id: <141120051355122243%bland@uNIVOFmich.edu.REMOVE.UPPER.CASE>


Hi,

I'm coming to the party a bit late, but you guys are scaring me, so I
had to reply (!)...

In article <20050904211634.239$7v@newsreader.com>, <xhoster@gmail.com>
wrote:

> Ignoramus14363 <ignoramus14363@NOSPAM.14363.invalid> wrote:
> > On 03 Sep 2005 22:51:13 GMT, xhoster@gmail.com <xhoster@gmail.com> wrote:
> > > Ignoramus14363 <ignoramus14363@NOSPAM.14363.invalid> wrote:
> > >> I wrote one
> > >>
> > >>   http://www.algebra.com/services/rendering/simplifier.mpl
> > >>
> > >> Besides simplifying, it shows work and plots math formulas. It is not
> > >> available publicly though.
> > >
> > > Also, it doesn't give the right answer.
> > >
> > > "Text form: ((x^2-6x+9)/(x^2-3x-10))/((x^2-5x+6)/(x^2-8x+15))
> > > simplifies to (x-3)/(x+2)"
> > >
> > > No, it doesn't.
> >
> > I will appreciate corrections and suggestions. What's the right answer
> > there?
> 
> I didn't solve it fully; I just set x to zero which was enough to show the
> simplification was wrong.

I did this, too, and got -1 when x = 0.  What did you get, Xho?

> It appears that the correct answer is (x-3)^2/(x-2)/(x+2).  It seem like
> your program realizes that (x-3) is a common factor in two of the quadratic
> groups, but then "cancels out" both of those groups in their entirety,
> rather than just the factor of x-3.

Have another look...  I get (x+2)/(x-2) .  Hopefully, no one coerced
the software in question to yield any of these other answers given
earlier.

Cheers,
Peyton Bland


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

Date: 14 Nov 2005 20:01:14 GMT
From: xhoster@gmail.com
Subject: Re: Looking for Equation Solver
Message-Id: <20051114150114.337$cg@newsreader.com>

Peyton Bland <bland@uNIVOFmich.edu.REMOVE.UPPER.CASE> wrote:
> Hi,
>
> I'm coming to the party a bit late, but you guys are scaring me, so I
> had to reply (!)...

That being the case, you might want to spend some more time explaining
what it is you are trying to say.  It is hard to just pick up a thread
after 3 months.

> In article <20050904211634.239$7v@newsreader.com>, <xhoster@gmail.com>
> wrote:
>
> > Ignoramus14363 <ignoramus14363@NOSPAM.14363.invalid> wrote:
> > > On 03 Sep 2005 22:51:13 GMT, xhoster@gmail.com <xhoster@gmail.com>
> > > wrote:
> > > > Ignoramus14363 <ignoramus14363@NOSPAM.14363.invalid> wrote:
> > > >> I wrote one
> > > >>
> > > >>   http://www.algebra.com/services/rendering/simplifier.mpl
> > > >>
> > > >> Besides simplifying, it shows work and plots math formulas. It is
> > > >> not available publicly though.
> > > >
> > > > Also, it doesn't give the right answer.
> > > >
> > > > "Text form: ((x^2-6x+9)/(x^2-3x-10))/((x^2-5x+6)/(x^2-8x+15))
> > > > simplifies to (x-3)/(x+2)"
> > > >
> > > > No, it doesn't.
> > >
> > > I will appreciate corrections and suggestions. What's the right
> > > answer there?
> >
> > I didn't solve it fully; I just set x to zero which was enough to show
> > the simplification was wrong.
>
> I did this, too, and got -1 when x = 0.  What did you get, Xho?

Well, for the original full form, I got -9/4.  For the original incorrect
simplification I got -3/2.  For the correct simplification I got
(obviously) -9/4.  For your new incorrect simplification I got -1.


>
> > It appears that the correct answer is (x-3)^2/(x-2)/(x+2).  It seem
> > like your program realizes that (x-3) is a common factor in two of the
> > quadratic groups, but then "cancels out" both of those groups in their
> > entirety, rather than just the factor of x-3.
>
> Have another look...

Are you trying to tell me to double check my work, or are you telling me
that the bug has been fixed and I should go look at the original web page
again?


> I get (x+2)/(x-2) .

Which is obiously wrong, as (setting x=0) -9/4 != -1.

> Hopefully, no one coerced
> the software in question to yield any of these other answers given
> earlier.

I have no idea what that means.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Mon, 14 Nov 2005 23:01:52 GMT
From: "Francis Sylvester" <francis@nospam.com>
Subject: Pattern Matching problem!
Message-Id: <AH8ef.16551$Es4.9736@fe2.news.blueyonder.co.uk>

Hi,

I'm a Perl newbie and am having a nightmare trying to get the code below 
working. I'm trying to fetch a webpage and if a link within the page matches 
the search criterion - return the text after the link. It doesn't seem to be 
working and I'm wondering if it's because the pattern match is within the 
while loop. If anybody can shed some light I'd be eternally grateful!

Cheers,
Francis

# --------------------------
use LWP::Simple;
use HTML::TokeParser;

my $document = get("http://www.anexamplesite.com");
my $mymatch = "searchstring";

my $parser = HTML::TokeParser->new(\$document);

while ($token = $parser->get_tag("a")) {
         if ($token->[1]->{"href"} =~ /$mymatch/) {
#            print $server.$token->[1]->{href}."\n";
              $document =~ /$searchstring(.+?)someidentifier/;
               print "$1";
 }
}




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

Date: Mon, 14 Nov 2005 23:33:11 +0100
From: Martin Kissner <news@chaos-net.de>
Subject: Re: strange problem with system() and backtick operator
Message-Id: <slrndni457.5a2.news@maki.homeunix.net>

Tad McClellan wrote :
> Martin Kissner <news@chaos-net.de> wrote:
>> John W. Krahn wrote :
>
>>> perldoc -f chmod
>> 
>> I tried it quickly and it did not quite work as expected.
>
> Are you writing the permissions in octal (with a leading zero)?

I was writing 
	chmod oct($octal), $local_file
as suggested in perldoc -f chmod.
>
>> Nevertheless I'd be interessted, why my chmod command fails if the file
>> exists.
>
>
> It fails because you are doing something wrong.
>
> We don't know what it is that you are doing, because you
> have not shown us your code...
>
Well, I've been showing plenty of code in my first posting.
I don't want to post the whole script because it is quite long and I
don't want to bother.

I guess the `chmod ...` fails, because the ftp connection is still open
and the file did not really download successfully. I think so because I
can easyly chmod when the scipt has finished and the connection is
closed.

Can anyone confirm this assumption?

Regards
Martin

-- 
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'


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

Date: Mon, 14 Nov 2005 22:46:04 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: strange problem with system() and backtick operator
Message-Id: <Xns970EB4C10AFCAasu1cornelledu@127.0.0.1>

Martin Kissner <news@chaos-net.de> wrote in 
news:slrndni457.5a2.news@maki.homeunix.net:

> Tad McClellan wrote :
>> Martin Kissner <news@chaos-net.de> wrote:
>>> John W. Krahn wrote :
>>
>>>> perldoc -f chmod
>>> 
>>> I tried it quickly and it did not quite work as expected.
>>
>> Are you writing the permissions in octal (with a leading zero)?
> 
> I was writing 
>      chmod oct($octal), $local_file
> as suggested in perldoc -f chmod.

Let's see:

    $mode = '0644'; chmod oct($mode), 'foo'; # this is better
    $mode = 0644;   chmod $mode, 'foo';      # this is best

Why not use the best method?

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

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 8665
***************************************


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