[18766] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 934 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat May 19 00:05:39 2001

Date: Fri, 18 May 2001 21:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <990245106-v10-i934@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 18 May 2001     Volume: 10 Number: 934

Today's topics:
    Re: Closure: which variable does it refer to? <joe+usenet@sunstarsys.com>
    Re: Closure: which variable does it refer to? <ilya@math.ohio-state.edu>
    Re: Excluding certain output variables? <bart.lateur@skynet.be>
    Re: Has this already been done? (Martien Verbruggen)
    Re: Help: using constants from inherited parent class <ren@tivoli.com>
    Re: Help: using constants from inherited parent class (Abigail)
        How to match the password created in Linux shadow suite (Joseph Chen)
        image_button and onClick??? <bcoon@sequenom.com>
    Re: keys to variable names, or variable names to keys? <c_clarkson@hotmail.com>
        login script <Wojciech.Domalewski@pnti.waw.pl>
    Re: Need Example creating Registry variable names <vGeesink@Planet.NL>
        page transition when submitting form <ss@sl.net.ua>
    Re: page transition when submitting form (Abigail)
    Re: page transition when submitting form <tony_curtis32@yahoo.com>
    Re: Pronouncing ISA (Eric Bohlman)
    Re: Stubborn regex won't work <ren@tivoli.com>
    Re: Stubborn regex won't work (Abigail)
    Re: Stubborn regex won't work (Tad McClellan)
        Why can't I localize a lexical variable? <bart.lateur@skynet.be>
    Re: Why can't I localize a lexical variable? (Clinton A. Pierce)
    Re: Why can't I localize a lexical variable? <godzilla@stomp.stomp.tokyo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 18 May 2001 18:22:17 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Closure: which variable does it refer to?
Message-Id: <m34ruiwb52.fsf@mumonkan.sunstarsys.com>

logan@cs.utexas.edu (Logan Shaw) writes:

> There is one explanation that may make sense at first but which can't
> be right.  That explanation is the one you might think of when reading
> this text (from "perldoc perlsyn"):
> 
> 	The `foreach' loop iterates over a normal list value and
> 	sets the variable VAR to be each element of the list in
> 	turn.  If the variable is preceded with the keyword `my',
> 	then it is lexically scoped, and is therefore visible only
> 	within the loop.  Otherwise, the variable is implicitly
> 	local to the loop and regains its former value upon exiting
> 	the loop.  If the variable was previously declared with
> 	`my', it uses that variable instead of the global one, but
> 	it's still localized to the loop.
> 
> You might think that a different $x is being created, and that is
> the one that's bound into the closure.  But if I understand local
> variables in Perl properly, that's not the case, because making
> something local just saves the value (as on a stack) and restores the
> value later.  That means it wouldn't really be a separate variable.

I'm certain that you have a good point in here, but I'm having trouble
following.  For the purposes if this post, I'm going to take the
definition that a closure is an anonymous sub containing a variable
which is accessed from outside its native scope.

Examples (untested):

  { 
    my $foo = 1;  
    $j = sub {print $foo};      # &$j is not yet a closure
  }

  $j->() ;                      # here &$j is a closure as $foo is gone


  {
    my @x = 1..3;

    for my $x (@x) { 
      $j = sub {print $x}       # (*)
    }

    $j->();                     # not a closure, since references $x[-1]
  }

  $j->();                       # closure since $x[-1] is gone


  {
    my @y = 1..3;
    $j = sub {print $_} for @y; # note difference with (*)
  }
  $j->();                       # never a closure, since references $_


  {
    local @z = 1..3;
    for my $z (@z) { 
        $j = sub {print $z}     # same as (*)
    }
  
    $j->();                     # not a closure (prints 3)
   ++$z[-1];
  }

  $z[3] = 1;
  $j->();                       # prints 4 - a closure since it references
                                # a var which has now gone out of scope


> So, instead we have to go with the other explanation.  What your loop
> is doing is creating a closure (well, three of them, but two of them
> are not referred to and presumably garbage collected), and the value
> that is being bound into the closure is not the loop variable but an

Your terminology differs with mine. What you call a closure, I'd say 
is still just an anonymous sub. Note that the type of loop variable 
(global or lexical) has a profound effect on how perl constructs these 
anonymous subs, and the binding effect you accurately describe here
has more to do with $x being a lexical than @x being one.

> element of the array that you've created, i.e. the array "1..3".
                                                     ^^^^^^^^^^^

There is no array in John's code, just a list (in fact, I think not even
that is true- for() works some special magic with ..).  

[...]

> I'm kind of curious whether this is something anyone intended to happen
> or not.  Mixing closures with the special behavior of foreach loop
> variables is something that may not have come up before.  (Or maybe it
> has.)

IOW a for() loop with a lexical loop var doesn't necessarily create a 
closure (using my terminology), since that depends on the scope of
the scalars being aliased.  Is this what you are saying?

If so, I certainly agree, but AFAICT this subtlety doesn't appear 
to be relevant in John's question.  To help clarify his issue, 
perhaps John could rephrase his question without using for() loops?

> I'm particularly interested in knowing what happens with garbage
> collection.  For instance, if I write this:
> 
> 	{
> 	    my $x;
> 
> 	    my @x = 1 .. 3;
> 
> 	    foreach $x (@x)
> 	    {
> 		$foo = sub { $x; };
> 	    }
> 	}
> 
> Then after the outer block, @x is no longer in scope, but it is
> referred to by the closure.  But does perl know this?  
                                        ^^^^^^^^^^^^^^

Yes, perl learned about it when it raised the ref count for the scalars
in @x when it ran "$foo=sub{$x}".  Each reasssignment reduced
the refcount back to 1 though - so $x[-1] is the only the scalar 
which remains alive when perl exits the outer block.

> Does perl take into account the possibility that a closure can refer
> not to a whole (first-class) variable but just to a value within an
> array?  And, since only one value of the array is needed, is the whole
> thing kept around just to ensure that one value remains available?

No, nothing is kept around except for the last element.  
Array "destruction" happens separately from destruction 
of the scalar elements within.

[...]

> I can imagine
> code, though, in which handling closures plus variable aliasing into
> arrays would have to be a special case, and I can imagine someone might
> not have thought of that special case.  (But, I can imagine it other
> ways too.)

I don't see any problem in how for loops and anonymous subs work- 
but perhaps it's been a long day :-)

-- 
Joe Schaefer   "The worst thing you can do to a man is to tell him he can have
                                       what he wants."
                                               --Mark Twain


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

Date: 19 May 2001 00:26:16 GMT
From: Ilya Zakharevich <ilya@math.ohio-state.edu>
Subject: Re: Closure: which variable does it refer to?
Message-Id: <9e4ej8$mgf$1@agate.berkeley.edu>

[A complimentary Cc of this posting was sent to
Logan Shaw
<logan@cs.utexas.edu>], who wrote in article <9e2k2d$l0g$1@charity.cs.utexas.edu>:
> You might think that a different $x is being created, and that is
> the one that's bound into the closure.  But if I understand local
> variables in Perl properly, that's not the case, because making
> something local just saves the value (as on a stack) and restores the
> value later.  That means it wouldn't really be a separate variable.

This is not entirely true:

  > perl -wle "print \$a; local $a; print \$a"
  SCALAR(0x3f7a0)
  SCALAR(0x3205c)

Ilya


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

Date: Fri, 18 May 2001 23:34:49 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Excluding certain output variables?
Message-Id: <t5cbgtcup0r1ebpiam369tip3j1q981gtq@4ax.com>

David Soming wrote:

>I have this script which displays SSI environment variables in browser:
>  #!/usr/local/bin/perl
>   print "Content-type: text/html\n\n";
>   print "<tt>\n";
>   foreach $key (sort keys(%ENV)) {
>      print "$key = $ENV{$key}<p>";
>   }
>
>Using: <!--#exec cgi="cgi-bin/ALL_ENV.cgi"-->
>
>How can I exclude specific variables in the output namely;
>UNIQUE_USER_ID=
>REDIRECT_UNIQUE_ID=
>REDIRECT_USER_NAME=
>What do I need to do to exclude these? Any suggestions what I need to read
>up on please?

Somewhere at the start of your loop block:

	next if SOMETHING $key;

You can do individual equality tests, or create a hash containing your
items to exclude as keys:

	my %exclude = map { $_ => 1 } qw/UNIQUE_USER_ID
	  REDIRECT_UNIQUE_ID REDIRECT_USER_NAME/;
	foreach $key (sort keys(%ENV)) {
	    next if $exclude{$key};
	    print "$key = $ENV{$key}<p>";
	}

That's one.

Two is: make a copy of the hash, if you want to keep the original, and
use delete() to remove items you don't want.

	my %env = %ENV;
	delete @env{qw/UNIQUE_USER_ID REDIRECT_UNIQUE_ID
	  REDIRECT_USER_NAME/};
	foreach $key (sort keys(%env)) {
	    print "$key = $env{$key}<p>";
	}

-- 
	Bart.


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

Date: Sat, 19 May 2001 08:58:35 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Has this already been done?
Message-Id: <slrn9gba8q.v5e.mgjv@martien.heliotrope.home>

On Fri, 18 May 2001 20:11:50 GMT,
	Bart Lateur <bart.lateur@skynet.be> wrote:
> Craig Berry wrote:
> 
>>Not so much a crap idea as one with limited utility.  If you can guarantee
>>that you won't get multiple requests for a unique id during a single
>>second, this works as well as any other technique -- though you can get
>>the same effect with less work by just using the seconds-since-epoch value
>>returned by the time() function.  However, guaranteeing that you won't get
>>multiple requests in a single second can get arbitrarily tricky,
>>especially as your application grows and mutates over time. 
> 
> Take time since the epoch as a hex number. 8 characters. Append $$ as a
> hex number. Likely 4 characters.
> 
> Do a sleep(1), for example in an END block, and this makes sure you
> don't release the current PID before this second is over. So the next
> copy of this script running can not get this PID assigned until the
> time() has changed.

If you're on a machine where you have to worry about the pid rolling
over within a second, you shouldn't make processes hang around for that
long for no reason. Unless you're just being cautious in case you end up
on a machine where pids are not assigned incrementally? 

Besides that: Using a pid only works when each instance of a unique ID
gets assigned from a different program. And your approach still doesn't
make an ID unique in the case where the application that doles out the
id runs on multiple machines, which many applications (especially Web
applications) do, nowadays.

One approach that I've used successfully in the past is to use a central
process on each machine in the "cluster" which writes continuously to a
FIFO. Clients just read one line from that same FIFO when they need an
ID. IPC is simple and fast. Or, if you don't have external clients, the
code could just live in the process that needs to generate the id.

There are many ways in which that program could spit out a unique
string, but one part of it should always be some host id. Another part
could be based on the output of time(), or something more high
resolution, followed by a number that increments within the resolution
of the time you use. Or maybe simply reset the base time once a day. The
only thing still to take care of is the restarting of this process. If
it stops and starts within the resolution of your time measurement, you
could end up handing out the same ID twice. There are ways around that,
but you'd be paying a speed penalty. One way is to make the last id
persistent, on the file system or so. Another is to make sure your
process never starts up within the resolution of your time measurement,
by making it wait. 

I've used a concatenation of host id, time() and a 16 bit serial number
which gives me some 64000 unique IDs per second, which was more than
enough for the application I was working on. I used a hex base, which
gave me a 20 character string, IIRC, but choosing a larger base can
shrink that considerably if that's a concern.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Unix is user friendly. It's just
Commercial Dynamics Pty. Ltd.   | selective about its friends.
NSW, Australia                  | 


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

Date: 18 May 2001 18:28:28 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Help: using constants from inherited parent class
Message-Id: <m37kzefd9f.fsf@dhcp9-172.support.tivoli.com>

On Fri, 18 May 2001, rstands@hotmail.com wrote:

> This must be fairly common but I can't find an example in my
> ActiveState installation nor any reference to it in AS's docs, Camel
> or Conway's OOP. I am sub-classing a package. The parent(SUPER)
> package allows exporting some constants e.g.:

It may not be that common as exporting symbols is not very OO.
Normally, these types of things would be Class variables.  In fact,
the concept of exporting is so contrary to OO that the new "base"
pragma doesn't even provide a facility for passing in the export
requests.

[snip]

> What's the secret syntax and where does it go?

Try something like:

======== Class.pm =========
package Class;
use base Exporter;
our @EXPORT_OK = qw/APPLE BANANA/;
our %EXPORT_TAGS = (CONSTANTS => [qw/APPLE BANANA/]);
sub APPLE { "apple" }
sub BANANA { "banana" }
1;
===========================

======= SubClass.pm =======
package SubClass;
use Class qw/:CONSTANTS/;
our @ISA = qw/Class/;
our @EXPORT_OK = @Class::EXPORT_OK;
our %EXPORT_TAGS = %Class::EXPORT_TAGS;
1;
===========================

#!/usr/bin/perl
use strict;
use warnings;
use SubClass qw/:CONSTANTS/;
print APPLE, "\n";
print BANANA, "\n";
__END__


-- 
Ren Maddox
ren@tivoli.com


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

Date: Sat, 19 May 2001 00:03:59 +0000 (UTC)
From: abigail@foad.org (Abigail)
Subject: Re: Help: using constants from inherited parent class
Message-Id: <slrn9gbe3f.anl.abigail@tsathoggua.rlyeh.net>

Ren Maddox (ren@tivoli.com) wrote on MMDCCCXVII September MCMXCIII in
<URL:news:m37kzefd9f.fsf@dhcp9-172.support.tivoli.com>:
][  On Fri, 18 May 2001, rstands@hotmail.com wrote:
][  
][ > This must be fairly common but I can't find an example in my
][ > ActiveState installation nor any reference to it in AS's docs, Camel
][ > or Conway's OOP. I am sub-classing a package. The parent(SUPER)
][ > package allows exporting some constants e.g.:
][  
][  It may not be that common as exporting symbols is not very OO.

I've never understood why. I've always learned that use of constants
was a good thing, and that duplication of data should be avoided.


Doesn't the school of OO agree? 



Abigail
-- 
perl -we '$_ = q ?4a75737420616e6f74686572205065726c204861636b65720as?;??;
          for (??;(??)x??;??)
              {??;s;(..)s?;qq ?print chr 0x$1 and \161 ss?;excess;??}'


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

Date: 18 May 2001 21:03:51 -0700
From: yen_hung@yahoo.com (Joseph Chen)
Subject: How to match the password created in Linux shadow suite?
Message-Id: <bf927196.0105182003.1e825283@posting.google.com>

Dear Perl Users,

I move a CGI program from an IBM machine to a Linux machine.
In the IBM machine, the program allows users to enter their 
login password and uses the password to match the one in 
/etc/passwd file. It works ok. But, after I move the program
to the Linux machine. The password comparison fails.

I use the perl function crypt to create the password for
comparison in Linux. The encrypted password has 13 characters.
But, due to the useage of shadow suite, the suite creates
another file /etc/shadow to store user passwords which are
way much longer than the 13-character encripted passwords
created by the crypt function. Therefore, the shadow suite
seems to be the reason why the password comparison fails.
That is, a string longer than 13 characters is never equal
to a string of 13 character long.

Do we have any user experiencing the same problem as well?
If we do, what would be the solution for solving my problem
here?

Any help would be much appreciated.


Joseph Chen


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

Date: Fri, 18 May 2001 17:16:42 -0700
From: Bryan Coon <bcoon@sequenom.com>
Subject: image_button and onClick???
Message-Id: <3B05BB6A.887D60DA@sequenom.com>

For some reason, I cannot get the onclick javascript action to work with
CGI.pm's image_button.  Here is a sample of code:

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

use CGI qw(:standard);

print header();
print start_html();
print start_form();

my $JS=<<END;
alert('Delete Selection?');
END

print image_button(-name    => 'pop',
                          -src =>
'http://www.mysql.com/images/navi-logo.gif',
                          -align   => 'MIDDLE',
                          -onClick => $JS,
                          "border=0");
print end_form();
print end_html();
--------------------------

I have tried many variations, but no luck.   The javascript is correct,
it works just fine with any other form submit buttons, or whatever.  Its
just with image_button.  The html even looks correct to me, I dont get
it.

Anyone see what I am missing?

Thanks,
Bryan




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

Date: Fri, 18 May 2001 20:08:34 -0500
From: "Charles K. Clarkson" <c_clarkson@hotmail.com>
Subject: Re: keys to variable names, or variable names to keys?
Message-Id: <B74211237D100F9E.51220C9111C47329.D9FC3F4B1198DB8F@lp.airnews.net>


"Paul Johnson" <pauljohn@ukans.edu> wrote in message
news:tga2a0fkbru2e3@corp.supernews.com...
: I wrote a CGI script that processes a bunch of string variables and saves
: them into a dataset.  I find my self doing the same repetitive, stupid
: things, one variable after another.  I wonder why can't I find a way to
: make a hash of variable names and then just iterate over the hash?
:
: I am no perl veteran, as you may guess.  So I'm not ashamed to share with
: you my ignorance.  Here's a subroutine that checks the lengths of the
: variables that have been inputted.  Earlier, the script has already read
: the values out of the CGI input with declarations like
:
: $lastname = $cgi_input->param('lastname');
:

    Is there a reason for not using the MAXLENGTH attribute
for <INPUT>?

    I tried it on IE and I was limited to 10 characters of input with:

<input type="text" name="field_name" size=50 maxlength=10 />

    I don't play with CGI much, though. Do some browsers ignore
MAXLENGTH?

HTH,
Charles K. Clarkson








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

Date: Sat, 19 May 2001 00:10:04 GMT
From: Wojciech Domalewski <Wojciech.Domalewski@pnti.waw.pl>
Subject: login script
Message-Id: <3B05B979.4EFFBF70@pnti.waw.pl>

Hello:

Does anyone knows if there's any perl cgi script that performs user
authentication (Apache/.htaccess)?

And what modules if any, to be compiled with apache, is required before
this can be done?

Thanks!

Wojciech.Domalewski@pnti.waw.pl .



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

Date: Sat, 19 May 2001 02:34:17 +0200
From: "Frank van Geesink" <vGeesink@Planet.NL>
Subject: Re: Need Example creating Registry variable names
Message-Id: <9e4ekp$6ogtc$1@reader01.wxs.nl>

Thanks, for the information.

>
> > Here is the test I'm working on.
>
> I do not believe you.  I think there are a lot of transcription
> errors.  This makes it impossible to tell what is your real problem
> and what is a transcription error.
>

The example I used is from a manuel I found about win32::Registry.
I'm using build 522 again. With build 626 the Perl2Exe didn't function.
The program does function like I wrote on my machine.
Because the program also has to work on some old Win95 machines in the near
future I choose the Create function of Win32::Registry. It was designed for
windows 3.1, so it certainly will function on the Win95 machines (as it did
so far on my NT machine). I don't know if I can expect some problems with
the win32API of Win95, when I use the new functions.
The Win32::TieRegistry function you used, did the job. So I will use it and
see if it also will function on the old Win95 machines.
Thanks.

> > use Win32::Registry;
>
> Have you looked at the documentation of Win32::Registry....
>
> DESCRIPTION
>
>     NOTE: This module provides a very klunky interface to access the
>     Windows registry, and is not currently being developed actively.
>     It only exists for backward compatibility with old code that uses it.
>
>     For more powerful and flexible ways to access the registry, use
>     Win32::TieRegistry.
>
>
> > my $Register="Software\\Funstuff";
> > my $hkey, $SubKey;
> >
> > $HKEY_LOCAL_MACHINE->Open($Register,$hkey) || die $!;
> > $Key="Frank";
> > $hkey->Create($New,$SubKey);
> > $hkey->SetValue($Key,REG_SZ,'Hello');
> > $hkey->Close();
> >
> > The program creates a subkey "Frank" an places a default variabele with
the
> > value "Hello".
>
> No it doesn't.
>
> > I would like to give this value a name so I get in subkey
> > "Frank" a variable named "Isay" with the value "Hello". Are there other
> > optios then $SubKey, I.e. $SubVar?
>
> It's just a variable, you can call it $YesWeHaveNoBananas if you like.
>
> > Can somebody give me a hand?
>
> use Win32::TieRegistry Delimiter => '/';
>
> $Registry->{'LMachine/Software/Funstuff/'} = {
>     'Frank/' =>
>        { '/Isay' => 'Hello' }
>     } or die "$^E";
>
> --
>      \\   ( )
>   .  _\\__[oo
>  .__/  \\ /\@
>  .  l___\\
>   # ll  l\\
>  ###LL  LL\\




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

Date: Sat, 19 May 2001 02:29:09 +0300
From: dene K <ss@sl.net.ua>
Subject: page transition when submitting form
Message-Id: <s1cbgts7c8sleeu5u3i24otk1a9l6l6vjl@4ax.com>


sorry for a lamers question, maybe.

how is possible to call perl script(form submitting) using post
method, avoiding page transition?


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

Date: Sat, 19 May 2001 00:07:38 +0000 (UTC)
From: abigail@foad.org (Abigail)
Subject: Re: page transition when submitting form
Message-Id: <slrn9gbeaa.anl.abigail@tsathoggua.rlyeh.net>

dene K (ss@sl.net.ua) wrote on MMDCCCXVII September MCMXCIII in
<URL:news:s1cbgts7c8sleeu5u3i24otk1a9l6l6vjl@4ax.com>:
;;  
;;  sorry for a lamers question, maybe.
;;  
;;  how is possible to call perl script(form submitting) using post
;;  method, avoiding page transition?


I think you are utterly confused.

The "POST" method is a way for an HTTP client to request an object
from an HTTP server.

This has nothing to do with Perl.


Perl programs can be called, and information passed to it, in all the
ways you can with other languages too. Typical ways of doing so include,
but are not limited to, command line arguments, environment variables,
stdin, and signals.

Could you rephrase your question?


Abigail
-- 
# Perl 5.6.0 broke this.
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC;1while+s/(\d+)((.)
(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers of Hanoi


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

Date: 18 May 2001 19:08:08 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: page transition when submitting form
Message-Id: <87g0e219qv.fsf@limey.hpcc.uh.edu>

>> On Sat, 19 May 2001 02:29:09 +0300,
>> dene K <ss@sl.net.ua> said:

> how is possible to call perl script(form submitting)
> using post method, avoiding page transition?

What's a "page transition"?  I think you want LWP though,

    perldoc lwpcook

hth
t
-- 
Just reach into these holes.  I use a carrot.


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

Date: 18 May 2001 22:24:21 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Pronouncing ISA
Message-Id: <9e47el$2fo$1@bob.news.rcn.net>

"Richard Stands" <rstands@hotmail.com> wrote:
> I thought this was funny. In my (ActiveState) Perl documentation it says:

> "This magical @ISA variable (pronounced ``is a'' not ``ice-uh'')".

> Uh, OK... what? After all that is it "iz uh" or "iz ay"? I mean really, 
> 'cause I'm not gonna spend one more dime of my company's $$$ until I can 
> properly pronounce the magical ISA! ;o) Cheers.

Well, it's usually followed by an equal sign, and to me "iz uh ee kwulz" 
sounds more euphonic than "iz ay ee kwulz."



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

Date: 18 May 2001 17:03:00 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Stubborn regex won't work
Message-Id: <m3bsoqfh7v.fsf@dhcp9-172.support.tivoli.com>

On Fri, 18 May 2001, EUSWMCL@am1.ericsson.se wrote:

> In the following, I'm trying to pull the "ERI000001" in every case,
> but I can't get it.  Can anyone help?
> 
> 
> $x='DISK02:[USERS.EUSWILC]ERI000001.INO;1';
> $y='DISK02:[USERS.EUSWILC]ERI000001.;1';
> $z='DISK02:[USERS.EUSWILC]ERI000001.';
> $w='DISK02:[USERS.EUSWILC]ERI000001.INO';
> 
> # Last word string terminated by a period
> for $t ($x,$y,$z,$w) {
>   if ($t =~ /(\w+)\.\G/g) {$a = $1} # this for example doesn't work
>   print "$a\n";
> }

Try:

  if ($t =~ /.*\b(\w+)\./) {$a = $1}

Sticking a ".*" on the front basically makes the regex work backwards
through the string, back-tracking until it finds a match.  The "\b" is
needed to allow the "\w+" to match the entire last word.  Otherwise,
it would succeed when matching the last letter of the last word.

-- 
Ren Maddox
ren@tivoli.com


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

Date: Sat, 19 May 2001 00:09:44 +0000 (UTC)
From: abigail@foad.org (Abigail)
Subject: Re: Stubborn regex won't work
Message-Id: <slrn9gbee8.anl.abigail@tsathoggua.rlyeh.net>

Ren Maddox (ren@tivoli.com) wrote on MMDCCCXVII September MCMXCIII in
<URL:news:m3bsoqfh7v.fsf@dhcp9-172.support.tivoli.com>:
$$  On Fri, 18 May 2001, EUSWMCL@am1.ericsson.se wrote:
$$  
$$ > In the following, I'm trying to pull the "ERI000001" in every case,
$$ > but I can't get it.  Can anyone help?
$$ > 
$$ > 
$$ > $x='DISK02:[USERS.EUSWILC]ERI000001.INO;1';
$$ > $y='DISK02:[USERS.EUSWILC]ERI000001.;1';
$$ > $z='DISK02:[USERS.EUSWILC]ERI000001.';
$$ > $w='DISK02:[USERS.EUSWILC]ERI000001.INO';
$$ > 
$$ > # Last word string terminated by a period
$$ > for $t ($x,$y,$z,$w) {
$$ >   if ($t =~ /(\w+)\.\G/g) {$a = $1} # this for example doesn't work
$$ >   print "$a\n";
$$ > }
$$  
$$  Try:
$$  
$$    if ($t =~ /.*\b(\w+)\./) {$a = $1}
$$  
$$  Sticking a ".*" on the front basically makes the regex work backwards
$$  through the string, back-tracking until it finds a match.  The "\b" is
$$  needed to allow the "\w+" to match the entire last word.  Otherwise,
$$  it would succeed when matching the last letter of the last word.


    /](\w+)/


No back-tracking necessary.



Abigail
-- 
perl -wle '$, = " "; sub AUTOLOAD {($AUTOLOAD =~ /::(.*)/) [0];}
           print+Just (), another (), Perl (), Hacker ();'


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

Date: Fri, 18 May 2001 19:00:35 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Stubborn regex won't work
Message-Id: <slrn9gbacj.t4v.tadmc@tadmc26.august.net>

Ciaran McCreesh <keesh@users.pleaseremovethisbit.sourceforge.net> wrote:
>In article <3B055618.1AB65E3C@am1.ericsson.se>, "William Cardwell"
><EUSWMCL@am1.ericsson.se> wrote:

>> for $t ($x,$y,$z,$w) {
>
>Surely foreach ?


   perldoc perlsyn

"The C<foreach> keyword is actually a synonym for the C<for> keyword"


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


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

Date: Fri, 18 May 2001 23:28:32 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Why can't I localize a lexical variable?
Message-Id: <0mbbgtg0o7j174g31qka22h1jvi5garudf@4ax.com>

I don't get it. local() is very liberal in what it accepts, you can even
apply local to individual hash and array elements of lexical variables:

	use Data::Dumper;
	my %foo = ( a => 'alpha', b => 'beta', d => 'delta');
	{
	    local $foo{b} = 'book';
	    local $foo{c} = 'cat';
	    print Dumper \%foo;
	}
	print Dumper \%foo;
-->
	$VAR1 = {
	          'a' => 'alpha',
	          'b' => 'book',
	          'c' => 'cat',
	          'd' => 'delta'
	        };
	$VAR1 = {
	          'a' => 'alpha',
	          'b' => 'beta',
	          'c' => undef,
	          'd' => 'delta'
	        };

Well alright, the presence of the 'c' key in the hash after it's all
finished, is a bit of a beauty spot.

It works with arrays too. It looks handy, if ever you need temporal
localization.

So why on earth does this NOT work?

	my $foo = 'Fred';
	{
	    local $foo = 'Barney';
	    print Dumper \$foo;
	}
-->
	Can't localize lexical variable $foo at test.pl line 5.

-- 
	Bart.


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

Date: Sat, 19 May 2001 00:48:03 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: Why can't I localize a lexical variable?
Message-Id: <7pjN6.48550$V5.8973014@news1.rdc1.mi.home.com>

In article <0mbbgtg0o7j174g31qka22h1jvi5garudf@4ax.com>,
	Bart Lateur <bart.lateur@skynet.be> writes:
> So why on earth does this NOT work?
> 
> 	my $foo = 'Fred';
> 	{
> 	    local $foo = 'Barney';
> 	    print Dumper \$foo;
> 	}
> -->
> 	Can't localize lexical variable $foo at test.pl line 5.

Because it hurts my head.    Example:

	# Does not compile:
	# Can't localize lexical...
	my $foo=66;
	sub cartman {
		print "Foo is $foo\n";
	}

	sub kenny {
		local $foo=67;
		cartman();
	}
	cartman();

I should be able to determine the scope of a lexical by visual 
examination -- I don't need to know the program's flow to know what's
in scope where.  So within cartman() I'm going to get the lexical $foo
declared with my() or the localized $foo from kenny(), depending on
whether or not cartman() was called from the main or kenny().

Ouch.

Where my underwear gets in a knot is here:

	# Compiles fine.
	# Runs okay (with a warning).
	# Can you guess what it does?  (beginners only!)
	sub cartman {
		print "Foo is $foo\n";
	}

	sub kenny {
		local $foo=67;
		cartman();
	}
	my $foo=66;
	kenny();
	cartman();

Yuck.  This is why I always code:

	Declarations
	Subroutines (reverse call order, no forward decl. needed)
	Main

To avoid this nonsense.
-- 
    Clinton A. Pierce              Teach Yourself Perl in 24 Hours  *and*
  clintp@geeksalad.org         Perl Developer's Dictionary -- May 2001
"If you rush a Miracle Man,     for details, see http://geeksalad.org     
	you get rotten Miracles." --Miracle Max, The Princess Bride


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

Date: Fri, 18 May 2001 18:50:06 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Why can't I localize a lexical variable?
Message-Id: <3B05D14E.E333DB8F@stomp.stomp.tokyo>

Bart Lateur wrote:
 
> I don't get it. local() is very liberal in what it accepts, you can even
> apply local to individual hash and array elements of lexical variables:

(snipped)

> Can't localize lexical variable $foo at test.pl line 5.


I posted an article within the past couple of weeks on
how a rogue programmer, such as myself, would bend a rule
to accomplish this.


#!perl -w

my ($variable) = "Godzilla Rocks";

print $variable;

&Rock;

sub Rock
 {
  my ($variable) = " N Rolls! ";
  print $variable;
 }

print "$variable!";

exit;


*****


#!perl -w

our ($variable) = "Godzilla Rocks";

print $variable;

&Rock;

sub Rock
 {
  local ($variable) = " N Rolls! ";
  print $variable;
 }

print "$variable!";

exit;



Neither method cop any complaints. I am certain my first
method will cause anal retentive fits amongst the denizens
of the crowded peanut gallery here.

Godzilla!


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

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.  

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


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