[21928] in Perl-Users-Digest
Perl-Users Digest, Issue: 4150 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 20 21:05:50 2002
Date: Wed, 20 Nov 2002 18:05:12 -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 Wed, 20 Nov 2002 Volume: 10 Number: 4150
Today's topics:
"use of uninitialized value" when all values are initia <miscellaneousemail@yahoo.com>
Re: "use of uninitialized value" when all values are in <jeff@vpservices.com>
Re: "use of uninitialized value" when all values are in (Tad McClellan)
Re: "use of uninitialized value" when all values are in <miscellaneousemail@yahoo.com>
Re: "use of uninitialized value" when all values are in <miscellaneousemail@yahoo.com>
Re: 'Burning' memory in perl <mgjv@tradingpost.com.au>
Re: 'Burning' memory in perl <tassilo.parseval@post.rwth-aachen.de>
5.8.0 port? <gohabsgoNOSPAM@mailandnews.com>
[How to detect and delete duplicate words in a file?] <terachichi@yahoo.com>
Re: [How to detect and delete duplicate words in a file (Tad McClellan)
Re: [How to detect and delete duplicate words in a file <krahnj@acm.org>
Re: [How to detect and delete duplicate words in a file <me@dwall.fastmail.fm>
Re: [How to detect and delete duplicate words in a file <me@dwall.fastmail.fm>
Any clue as to why this short function works? <miscellaneousemail@yahoo.com>
Re: Any clue as to why this short function works? <mgjv@tradingpost.com.au>
Re: Any clue as to why this short function works? (Tad McClellan)
Re: Any clue as to why this short function works? <miscellaneousemail@yahoo.com>
Re: Any clue as to why this short function works? <miscellaneousemail@yahoo.com>
Re: Convert Perl script to C program (and Why was this <islaw@w98.sr>
extracting path/file names (was Re: regex confusion) <usenet@dwall.fastmail.fm>
help this Newbie in Distress <doris@chris.com>
Re: help this Newbie in Distress <me@dwall.fastmail.fm>
Re: implementing a self() method for classes <eric.anderson@cordata.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 20 Nov 2002 22:02:46 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: "use of uninitialized value" when all values are initiatlized?
Message-Id: <MPG.1845b469af2375ed9898de@news.edmonton.telusplanet.net>
Hi everyone,
In the code below why does Perl report the use of an uninitialized value
in line 7 (with the print) when all values appear to be initialized? The
print out from this script indicates that it is working just fine.
CODE BEGIN
#!/usr/bin/perl
use strict;
my @a;
for my $x (1..10) {
$a[$x] = "Hello\n";
}
print @a;
CODE END
Any insight on this one?
Thanks.
--
Carlos
www.internetsuccess.ca/best_web_hosting.html
------------------------------
Date: Wed, 20 Nov 2002 14:07:32 -0800
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: "use of uninitialized value" when all values are initiatlized?
Message-Id: <3DDC07A4.7030302@vpservices.com>
Carlos C. Gonzalez wrote:
> #!/usr/bin/perl
No warnings????
> use strict;
> my @a;
> for my $x (1..10) {
> $a[$x] = "Hello\n";
> }
> print @a;
> Any insight on this one?
There's zero wrong with your code.
But since arrays begin with zero and you didn't initial $a[0], of course
you have an uninitialized warning.
--
Jeff
------------------------------
Date: Wed, 20 Nov 2002 16:16:32 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: "use of uninitialized value" when all values are initiatlized?
Message-Id: <slrnato2e0.2gc.tadmc@magna.augustmail.com>
Carlos C. Gonzalez <miscellaneousemail@yahoo.com> wrote:
>
> In the code below why does Perl report the use of an uninitialized value
> in line 7 (with the print) when all values appear to be initialized?
Appearances can be deceiving.
> for my $x (1..10) {
> $a[$x] = "Hello\n";
> }
> print @a;
What is in $a[0] ?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Nov 2002 01:24:59 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: "use of uninitialized value" when all values are initiatlized?
Message-Id: <MPG.1845e3c7c9d4004f9898df@news.edmonton.telusplanet.net>
In article <slrnato2e0.2gc.tadmc@magna.augustmail.com>,
tadmc@augustmail.com says...
[ some snipped ]
> > for my $x (1..10) {
> > $a[$x] = "Hello\n";
> > }
> > print @a;
>
>
> What is in $a[0] ?
>
Oops! I see what the problem is now Tad. I kid you not I kept staring
and staring and staring some more at the code and just couldn't for the
life of me figure out where the uninitialized value was. 10 strings of
"Hello" were printing out and everything looked good.
It was right there in front of me and I just didn't see it.
Thanks much Tad.
--
Carlos
www.internetsuccess.ca/best_web_hosting.html
------------------------------
Date: Thu, 21 Nov 2002 01:31:10 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: "use of uninitialized value" when all values are initiatlized?
Message-Id: <MPG.1845e5397eaa182c9898e0@news.edmonton.telusplanet.net>
In article <3DDC07A4.7030302@vpservices.com>, jeff@vpservices.com says...
> > #!/usr/bin/perl
>
>
> No warnings????
>
[some snipped]
Thanks for your input Jeff. As I told Tad I just didn't see it.
As for warnings I guess I should put a -W in there when I ask questions
about my code. The code actually does run with a "-W" from within my
editor's Perl hot key. I put the shebang line in my posted code mostly
to keep down questions about why I am missing it. Under Windows, Perl
runs the code and doesn't even pay attention to the shebang line. At
least that's the way I have it set up.
Thanks for pointing that out Jeff. I'll just add a "-W" in the future to
better reflect how I actually run the code.
--
Carlos
www.internetsuccess.ca/best_web_hosting.html
------------------------------
Date: Wed, 20 Nov 2002 23:17:20 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: 'Burning' memory in perl
Message-Id: <slrnato64a.djj.mgjv@verbruggen.comdyn.com.au>
On Wed, 20 Nov 2002 14:18:03 +1000,
MB <mb@uq.not.au> wrote:
> Hi all,
>
> I'm writing a program that prompts for passwords, and I'd like to make
> sure that the password is gone from memory once I've finished with it
> (so it doesn't appear in a core dump or something). In C I'd use
> memset(...), but since memory management is not right on the surface of
> perl I'm not sure of the exact procedure.
>
> Would something like
>
> $passwd = ' ' x length($passwd);
>
> work? Or would that just allocate another bit of memory and leave the
> old chunk still containing the password?
Without looking at the perl sources to see what happens when you do
that, there is no real way of knowing. It is possible that perl avoids
a enw malloc or realloc when a SV already has room for the string you
want to store, and it just copies things in, overwriting what is
already there. It is also possible that the copying process works in
an entirely different manner under different circumstances.
The problem is that from the language point of view, you can't
guarantee how memory is used. You could find out for the current
implementation of perl, or for any older one, and you could draw
conclusions about what future implementations would or would not do,
but there are no guarantees.
> What about:
> {
> my $passwd = get_passwd();
> do_something_with_passwd($passwd);
> }
>
> Will going out of scope scrub the memory as well? I'm guessing not, but
> you never know...
If it happens, it's an artefact of the implementation, and it could
even be different on various platforms.
If you really need to do this, you should maybe implement something
yourself in C, and maybe tie that into Perl with XS or Inline::C. Or
maybe you could do something with shared memory segments. Or maybe
there's already a module on CPAN that provides this sort of memory
management. Perl itself has no facilities to do this sort of thing.
Martien
--
|
Martien Verbruggen | True seekers can always find something to
Trading Post Australia | believe in.
|
------------------------------
Date: 20 Nov 2002 23:41:29 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: 'Burning' memory in perl
Message-Id: <arh6j9$8rc$1@nets3.rz.RWTH-Aachen.DE>
Also sprach MB:
> I'm writing a program that prompts for passwords, and I'd like to make
> sure that the password is gone from memory once I've finished with it
> (so it doesn't appear in a core dump or something). In C I'd use
> memset(...), but since memory management is not right on the surface of
> perl I'm not sure of the exact procedure.
>
> Would something like
>
> $passwd = ' ' x length($passwd);
>
> work? Or would that just allocate another bit of memory and leave the
> old chunk still containing the password?
>
> What about:
> {
> my $passwd = get_passwd();
> do_something_with_passwd($passwd);
> }
>
> Will going out of scope scrub the memory as well? I'm guessing not, but
> you never know...
Speaking about core dumps, this wont help since a corefile is actually
a backtrace. I just tried the following:
ethan@ethan:~$ perl
my $password = "hallo";
$password = ' ' x length($password);
<STDIN>;
__END__
And on another console
ethan@ethan:~$ kill -11 `pidof perl`
Running 'strings' on the corefile, I see for instance:
main
$password
hallo
and later:
= ' ' x length($password);
If you absolutely want to avoid corefiles, you could add a signal
handler for sigsegv:
$SIG{SEGV} = sub { warn "segfaulted"; exit 1 };
As for really clearing or overwriting a string, you could probably do it
with a small xsub:
void
clear (var)
PREINIT:
STRLEN len;
INPUT:
SV *var;
char *string = SvPV(var, len);
PPCODE:
memset(string, 0, strlen(string));
Wrap that into your own little XS module and do a 'clear($password)'.
But then I think you are just a little too paranoid here.
Tassilo
--
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;
------------------------------
Date: 20 Nov 2002 20:26:49 GMT
From: GoHabsGo <gohabsgoNOSPAM@mailandnews.com>
Subject: 5.8.0 port?
Message-Id: <Xns92CC9D0846BB9gohabsgomailandnewsc@130.133.1.4>
Has anyone built a port of perl 5.8.0 for OS/390 or Z/OS? If not, how
long before someone usually gets around to doing this?
I'm having trouble with the Configure shell script on OS/390 2.8 and am
hoping I could get a binary distribution instead.
Alternatively, could someone walk me through the configure process?
Thanks,
Larry
------------------------------
Date: Wed, 20 Nov 2002 16:51:12 -0800
From: PL <terachichi@yahoo.com>
Subject: [How to detect and delete duplicate words in a file?]
Message-Id: <3DDC2E00.34AFC36D@yahoo.com>
Hi, all:
How do I detect and delete duplicate words in a file in PERL?
thanks
------------------------------
Date: Wed, 20 Nov 2002 19:12:50 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: [How to detect and delete duplicate words in a file?]
Message-Id: <slrnatocoi.30v.tadmc@magna.augustmail.com>
PL <terachichi@yahoo.com> wrote:
> Subject: [How to detect and delete duplicate words in a file?]
What's with the [square brackets] in the Subject?
> How do I detect and delete duplicate words in a file in PERL?
my %seen;
while (<>) {
s/(\S+)/ $seen{$1}++ ? '' : $1/ge;
print;
}
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Nov 2002 01:16:12 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: [How to detect and delete duplicate words in a file?]
Message-Id: <3DDC339E.2AA0EA70@acm.org>
PL wrote:
>
> How do I detect and delete duplicate words in a file in PERL?
Do you mean duplicate adjacent words like "the the"? What characters do
these "words" consist of? What characters are invalid?
Perhaps these FAQs will help.
perldoc -q "I'm having trouble matching over more than one line"
perldoc -q "How can I remove duplicate elements from a list or array"
perldoc -q "What's the difference between \"perl\" and \"Perl\""
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 21 Nov 2002 01:24:04 GMT
From: "David K. Wall" <me@dwall.fastmail.fm>
Subject: Re: [How to detect and delete duplicate words in a file?]
Message-Id: <Xns92CCCF8991351dkwwashere@63.240.76.16>
PL <terachichi@yahoo.com> wrote:
> How do I detect and delete duplicate words in a file in PERL?
It depends on what you mean by duplicated. See
perldoc -q duplicate
and if that doesn't help, try posting with more information about what
you're trying to do.
Also, there is no such thing as PERL. The language is Perl, and perl is
the interpreter/compiler.
--
David Wall - me@dwall.fastmail.fm
"Oook."
------------------------------
Date: Thu, 21 Nov 2002 01:30:07 GMT
From: "David K. Wall" <me@dwall.fastmail.fm>
Subject: Re: [How to detect and delete duplicate words in a file?]
Message-Id: <Xns92CCD08F420D2dkwwashere@63.240.76.16>
tadmc@augustmail.com (Tad McClellan) wrote:
> PL <terachichi@yahoo.com> wrote:
>
>> Subject: [How to detect and delete duplicate words in a file?]
>
> What's with the [square brackets] in the Subject?
Possibly an attempt to get to the "top" of the list of articles? I suppose
it would work if I turned off threading and sorted articles by subject
instead of date.
--
David Wall - me@dwall.fastmail.fm
"Oook."
------------------------------
Date: Wed, 20 Nov 2002 21:35:40 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Any clue as to why this short function works?
Message-Id: <MPG.1845ade5b300d12e9898dd@news.edmonton.telusplanet.net>
Hi everyone,
I have been playing around with the caller function some and have
absolutely no clue as to why the following code seems to work (the idea
for this code came from inside the Carp::CGI and Carp internals).
Specifically how is it that the blocked code after the while loop prints
out the entire contents of the @a array? When there is no loop to
traverse and print out the array? And why is it that if I take away the
block "{}" from the code I get a syntax error?
CODE BEGIN
#!/usr/bin/perl
a();
sub a { b() }
sub b { die trace() }
sub trace {
my (@a, $i);
while (@a = caller($i++))
{ # <- TAKING THIS BLOCK AWAY CAUSES SYNTAX ERROR
my ($pack,$file,$line,$sub) = @a;
print "package = $pack\n";
print "file = $file\n";
print "line = $line\n";
print "sub = $sub\n\n";
} # <- CODE WORKS IF I LEAVE IT IN.
}
CODE END
If I run the code as is I also get a strange ending line that says
"0 at D:\internetsuccess.ca\cgi-bin\test\trace_test.pl line 4." I believe
that is a zero at the beginning of this line but what's with that? Why
does the code return such a line?
Any insight on any of the above would be most appreciated.
Thanks.
--
Carlos
www.internetsuccess.ca/best_web_hosting.html
------------------------------
Date: Wed, 20 Nov 2002 23:36:15 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Any clue as to why this short function works?
Message-Id: <slrnato77p.djj.mgjv@verbruggen.comdyn.com.au>
On Wed, 20 Nov 2002 21:35:40 GMT,
Carlos C Gonzalez <miscellaneousemail@yahoo.com> wrote:
> Hi everyone,
>
> I have been playing around with the caller function some and have
> absolutely no clue as to why the following code seems to work (the idea
> for this code came from inside the Carp::CGI and Carp internals).
> Specifically how is it that the blocked code after the while loop prints
> out the entire contents of the @a array?
It doesn't. It prints out the values of the four named variables.
Those variables have been assigned the values of the first four (of
ten) elements of @a, on the first line inside the block. The other six
elements of @a do not get printed.
What exactly don't you understand about that code?
> When there is no loop to
> traverse and print out the array?
There is a loop. In each iteration of the loop, the array @a gets
assigned the return values of the caller() function. Inside the loop,
the first four elements of that array are assigned to four named
variables, which subsequently get printed.
Again, I am confused about exactly what you think the problem is...
> And why is it that if I take away the
> block "{}" from the code I get a syntax error?
Because the syntax of a while statement is
LABEL while (EXPR) BLOCK
where the only optional bit is the LABEL. If the block is not there,
you violate the syntax.
> CODE BEGIN
> #!/usr/bin/perl
> a();
> sub a { b() }
> sub b { die trace() }
>
> sub trace {
> my (@a, $i);
> while (@a = caller($i++))
> { # <- TAKING THIS BLOCK AWAY CAUSES SYNTAX ERROR
> my ($pack,$file,$line,$sub) = @a;
> print "package = $pack\n";
> print "file = $file\n";
> print "line = $line\n";
> print "sub = $sub\n\n";
> } # <- CODE WORKS IF I LEAVE IT IN.
> }
> CODE END
>
> If I run the code as is I also get a strange ending line that says
> "0 at D:\internetsuccess.ca\cgi-bin\test\trace_test.pl line 4." I believe
> that is a zero at the beginning of this line but what's with that? Why
> does the code return such a line?
Because of the definition of sub b. It calls die with the return value
of trace(). trace() returns, like any subroutine without an explicit
return, whatever the last command returned. The last command executed
was the condition of the while loop, evaluating to false. In the
scalar context it was called in, that means it evaluates to 0. That is
the the zero that is returned from trace(), and which becomes the
first argument to die(). die(), as it is supposed to, prints that out,
follows it by line information (since "0" doesn't end in a newline),
and exits.
If this doesn't answer your questions, could you please try to be more
precise in stating what you expect, and why you don't understand what
is actually happening?
Martien
--
|
Martien Verbruggen |
Trading Post Australia | 42.6% of statistics is made up on the spot.
|
------------------------------
Date: Wed, 20 Nov 2002 18:00:43 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Any clue as to why this short function works?
Message-Id: <slrnato8hb.2lv.tadmc@magna.augustmail.com>
Carlos C. Gonzalez <miscellaneousemail@yahoo.com> wrote:
>
> I have been playing around with the caller function
> some and have
> absolutely no clue as to why the following code seems to work (the idea
> for this code came from inside the Carp::CGI and Carp internals).
> Specifically how is it that the blocked code after the while loop
There is no block of code after the while loop!
There is, however, a block of code that is the body of
the while loop.
> prints
> out the entire contents of the @a array?
_Your code_ copies each element and then prints it.
I do not understand your question.
Try asking it again.
> When there is no loop to
> traverse and print out the array?
while() _is_ a looping construct.
> And why is it that if I take away the
> block "{}" from the code I get a syntax error?
Are you serious?
while() loops are documented in:
perldoc perlysn
where it says:
But generally, a block is delimited by curly brackets, also known
as braces. We will call this syntactic construct a BLOCK.
...
LABEL while (EXPR) BLOCK
the {} are a _required_ part of the syntax of a while loop.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Nov 2002 01:40:01 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Any clue as to why this short function works?
Message-Id: <MPG.1845e74ec5a978879898e1@news.edmonton.telusplanet.net>
In article <slrnato8hb.2lv.tadmc@magna.augustmail.com>,
tadmc@augustmail.com says...
> There is no block of code after the while loop!
>
> There is, however, a block of code that is the body of
> the while loop.
>
Boy I just don't seem to be with it in Perl today. I can't believe that
I didn't see that either. I could have sworn that the expression in the
while statement was the actual body of the loop. Did I get that mixed up
or what!
I am laughing out loud at how I missed something so obvious. I even
looked at perlsync and several other pages on using while loops. What
struck me is that none of them looked quite like mine :-):).
Thanks very much for your input!
--
Carlos
www.internetsuccess.ca/best_web_hosting.html
------------------------------
Date: Thu, 21 Nov 2002 01:47:01 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Any clue as to why this short function works?
Message-Id: <MPG.1845e8f66b50db9d9898e2@news.edmonton.telusplanet.net>
In article <slrnato77p.djj.mgjv@verbruggen.comdyn.com.au>,
mgjv@tradingpost.com.au says...
[some nipped]
> > If I run the code as is I also get a strange ending line that says
> > "0 at D:\internetsuccess.ca\cgi-bin\test\trace_test.pl line 4." I believe
> > that is a zero at the beginning of this line but what's with that? Why
> > does the code return such a line?
>
> Because of the definition of sub b. It calls die with the return value
> of trace(). trace() returns, like any subroutine without an explicit
> return, whatever the last command returned. The last command executed
> was the condition of the while loop, evaluating to false. In the
> scalar context it was called in, that means it evaluates to 0. That is
> the the zero that is returned from trace(), and which becomes the
> first argument to die(). die(), as it is supposed to, prints that out,
> follows it by line information (since "0" doesn't end in a newline),
> and exits.
>
I see now why my code was printing out the zero based on your
explanation. Please see the rest of my responses in this thread as to
what I didn't understand and how I was just missing what should have been
obvious to me.
Thanks for taking the time to explain things Martien. I appreciate it.
--
Carlos
www.internetsuccess.ca/best_web_hosting.html
------------------------------
Date: Wed, 20 Nov 2002 19:44:50 GMT
From: "Islaw" <islaw@w98.sr>
Subject: Re: Convert Perl script to C program (and Why was this group's name changed?)
Message-Id: <SCRC9.1760$%z1.116822241@newssvr21.news.prodigy.com>
Alan J. Flavell wrote:
> On Nov 20, Islaw inscribed on the eternal scroll:
>
>> I'd really like to know how you got my name in this list? I've almsot
>
> "almsot"?
You can't seriously believe a single typo =='s me to someone else? I can
see how you might think it could of been, however.
>> never posted here, though I've always been a reader.
>
> ..and sporadic troll, under various assumed identities, it seems.
Of which I am not.
> Do something constructive, or go away.
I am always doing something constructive, which would leave me little
time to even want to get into anything like this. It's called having a
life, a job, and a family.
I'll consider my appearance on that list to be an honest mistake. I did
once reply to the one apparently known as "manny" or "fredo", the latter
being the alias I responded to, but that was to point out that the
people he was putting down were in fact some of the most helpful who
have been dealing with newbies, trolls, and the likes. I bid you no hard
feelings, and I hope at least the mistaking me as this troll can be
acknowledged as such or at least left at this. This is all true,
although I have much better things to do then to concentrate on things
like this.
Thank you,
Islaw
P.S. if anyone would like to contact me via email, islaw@adexec.com. I
am more then happy to get back to anyone who wishes to contact me.
------------------------------
Date: Wed, 20 Nov 2002 21:59:30 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: extracting path/file names (was Re: regex confusion)
Message-Id: <Xns92CCACD92DAD0dkwwashere@216.168.3.30>
Steve Holland <holland@origo.phys.au.dk> wrote on 20 Nov 2002:
> "Canucklehead" <mememe@meme.com> writes:
>
>> Say I have a string containing
>> "usr/local/home/someWebsite/foo/htdocs/pdr/xmlData/test/test4.xml" I
>> would like to parse out everything except the ending file name (which
>> could be any length of characters). In my head I know what needs to be
>> done but translating that into perl is another thing. =)
>>
>
> #! /usr/local/bin/perl
> use strict;
> use warnings;
> print "Enter a string : ";
> my $dir = <STDIN>;
> chomp($dir);
> $dir =~ m/.\/([^\/]*)$/;
> print "$1\n";
>
> I have a nagging feeling that there is a much neater way to do
> this.
Just use the standard module File::Basename.
use strict;
use warnings;
use File::Basename;
print basename '/path/to/the/file.ext';
will print 'file.ext'. That's easy enough for me. :-)
--
David K. Wall - usenet@dwall.fastmail.fm
"Oook."
------------------------------
Date: Wed, 20 Nov 2002 22:09:34 GMT
From: D <doris@chris.com>
Subject: help this Newbie in Distress
Message-Id: <3DDC0806.87ACA310@chris.com>
Thank you very much in advance. I have a question, which I cannot figure
out (I have spent days on this).
My goal is to take a book title, which starts out in all upper case, and
then make the whole thing lower case, except for the first letter of
each word.
EXAMPLE:
"THE STORY OF THE CAT AND DOG"
will turns into this:
"The Story of the Cat and Dog"
...However....let's say that the title has either the word "CD" or
"CD-ROM" in it. In that case, I want to leave the "CD" or "CD-ROM" in
it's original uppercase.
EXAMPLE:
"THE STORY OF THE CAT AND DOG (WITH CD-ROM)"
should turn into this:
"The Story of the Cat and Dog (With CD-ROM)"
Here's the code that I've been using. I am wanting to avoid ADDING an
extra line of code that will switch the lowercase back to uppercase.
What I am HOPING to do is to "tell Perl" to "execute my code" EXCEPT
when it comes across either 'CD' or 'CD-ROM'
//heres my code:
$_ = lc,
s/\b(\w)/\u$1/g,
s/('S|\bOf| And| The)\b/\L$1/g,
s/\(P\b/(paperback/,
for $list_of_titles[2];
Any ideas?
Thank you again!!!
------------------------------
Date: Thu, 21 Nov 2002 00:56:26 GMT
From: "David K. Wall" <me@dwall.fastmail.fm>
Subject: Re: help this Newbie in Distress
Message-Id: <Xns92CCCAD945B14dkwwashere@63.240.76.16>
D <doris@chris.com> wrote:
> My goal is to take a book title, which starts out in all upper case,
> and then make the whole thing lower case, except for the first letter
> of each word.
>
> EXAMPLE:
> "THE STORY OF THE CAT AND DOG"
> will turns into this:
> "The Story of the Cat and Dog"
>
> ...However....let's say that the title has either the word "CD" or
> "CD-ROM" in it. In that case, I want to leave the "CD" or "CD-ROM" in
> it's original uppercase.
Text::Autoformat will do most of this for you if you use the headline
option. When I tried a short test it turned CD-ROM into Cd-rom, but
you can always change it back.
There's an FAQ entry on capitalization (perldoc -q capitalization)
which you seem to have read or independently came up with a similar
solution.
--
David Wall - me@dwall.fastmail.fm
"Oook."
------------------------------
Date: Wed, 20 Nov 2002 19:02:57 -0500
From: Eric Anderson <eric.anderson@cordata.net>
Subject: Re: implementing a self() method for classes
Message-Id: <pan.2002.11.21.00.02.56.260486.21494@cordata.net>
On Mon, 18 Nov 2002 15:26:36 -0500, Bryan Castillo wrote:
> I don't quite understand what you want. The 2nd paragraph says you don't
> want the object passed as the first argument of the method, but here in
> the 3rd paragraph you say you will be using it. Do you mean that you
> will only use the object passed as the first argument on the first
> method call?
Perhaps an example would help:
# The user creates this
sub my_method {
print self->attribute1;
}
# My code does this
$old_method = \&UserPackage::my_method;;
*{UserPackage::my_method} = sub {
my $instance = shift;
... store $instance somewhere...
$old_method->()
}
sub self {
return ...stored reference to object...
}
> I've been trying to fathom why you want to do this (trying rather
> objectively). But I still can't figure out why you would want this How
> would you invoke methods without the object instance:
>
> if you weren't using the first argument as the object
> $obj->method
> wouldn't make sense. If you find a way to do this, you will
> probably
> confuse the programmers who will have to maintain your code. (This is
> a valid point, I have nothing against obfuscating code if it
> means
> the company can't hire cheaper programmers to take your position :)
> )
You would invoke methods by calling the self function which returns the
instance. I.E.
self->call_method( $argument );
Why I want to do this is because I want a method to be defined a little
more like other OO languages define them. Instead of over method having
to grab to first argument and use that they can simply call self and from
a syntax view it looks like some sort of built-in variable (except for
the fact that the $ is not in front of it.
> Variables are only shared between threads through threads::shared. If
> you don't share the global variable, you don't have to worry.
OK, well then that solves my problem. I assumed that threads in Perl
worked like other languages (i.e. the address space is shared), but it
seems that threads in Perl are a little more like just a multiprocess
application that uses a shared memory library. I guess I should go and
spend half an hour and figure out threads in Perl. :)
> I have never used Class::Contract, but it seems to me that it has
> points, such as prototypes on methods and enforcement of a inheritance
> hierarchy. What is your goal?
Going into my goal would take a bit to write, so I will just say for now
that there is a point to this :)
>
>> So I am planning to continue to look at Class::Contract since it pretty
>> much implements exactly what I want, but I thought I would post my
>> question to see if anybody sees an easy way to implement this safely.
>
> Why, don't you use Class::Contract?
It doesn't do everything I want and it does some things that I do not
want. If you want a quick summary of what I am trying to do is that I am
trying to create a modular system to implement things like
Class::Contract (and other modules in Class::*) AND have them be able to
interact together (in other words I could use Class::Contract to provide
me contract-based programming and use some Class::Persistant module to
provide me object persistance and they will both work).
Anyway, I like the fact that Class::Contract provides a self method and
removes the first argument so that it behaves more like a "standard" OO
language so I wanted to implement it myself if possible.
Eric Anderson
------------------------------
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 4150
***************************************