[22156] in Perl-Users-Digest
Perl-Users Digest, Issue: 4377 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jan 10 14:06:14 2003
Date: Fri, 10 Jan 2003 11:05:14 -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 Fri, 10 Jan 2003 Volume: 10 Number: 4377
Today's topics:
Re: Arrays. What don't I get? <spikey-wan@bigfoot.com>
Re: Converting CGI to XML (Malcolm Dew-Jones)
Re: Finding required .pl's home ? <craznar01@hotmail.com>
Re: Finding required .pl's home ? <craznar01@hotmail.com>
Re: Finding required .pl's home ? (Anno Siegel)
How does one Iterate an array to remove duplicates? (Lance)
Re: How does one Iterate an array to remove duplicates? <nobull@mail.com>
Re: How does one Iterate an array to remove duplicates? (Tad McClellan)
Re: I need to create a .zip file, as an attachment in e <bongie@gmx.net>
Re: Isogest 3.1 New release <clay@panix.com>
Matching entries in lists (Bernd Schandl)
Re: Matching entries in lists (Anno Siegel)
muli-spaced fields and split() (bad_knee)
Re: muli-spaced fields and split() <barbr-en@online.no>
Re: muli-spaced fields and split() <barbr-en@online.no>
Re: muli-spaced fields and split() <alecler@sympatico.ca>
Re: My, our, etc. <spikey-wan@bigfoot.com>
Re: My, our, etc. <spikey-wan@bigfoot.com>
Re: My, our, etc. (Ben Morrow)
Re: My, our, etc. (Ben Morrow)
Re: My, our, etc. (Tad McClellan)
Net::Mysql - capture error messages (Mike Solomon)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 10 Jan 2003 15:58:23 -0000
From: "Richard S Beckett" <spikey-wan@bigfoot.com>
Subject: Re: Arrays. What don't I get?
Message-Id: <avmqki$nk5$1@newshost.mot.com>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:avkcb1$s0j$1@mamenchi.zrz.TU-Berlin.DE...
> ...which is a very good reason. However, this is where hashes come in
> in Perl. Collecting your variables in a hash %var (say) allows you to
> have your cake (pass all variables around as a single collective %var)
> and eat it too (access single values through meaningful names: $var{ one},
> $var{ two}, etc).
OK, I'll go away and read up on hashes.
>
> > > If you must collect the variables into a list for some reason, use
> > > references:
> > >
> > > my @refs = ( \ $one, \ $two, \ $three, \ $four);
> > > $$_ *= 2 for @refs;
> >
> > >
> > > The first line could also be "my @refs = \ ( $one, $two, $three,
$four)",
> > > but I don't like that.
> >
> > A quick example is when I have to collect data from my entry boxes.
> > Currently I have:
> > sub get_values {
> > $host = $host_entry -> get;
> > $interval = $interval_entry -> get;
> > $timeout = $timeout_entry -> get;
> > $size = $size_entry -> get;
> > $number = $number_entry -> get;
> > $save = $save_entry -> get;
> > ......
> > }
>
> I don't understand how this illustrates the problem. This is an entirely
> different situation. It looks like you have a bunch of objects
($host_entry,
> $interval_entry, etc) and you are applying the get method to each of them.
> This is rather, umm... atypical for an OO approach. I'd rather expect a
> single data object ($entry, say) with a bunch of methods (get_host,
> get_interval, etc). I'm not saying it must be wrong, but the approach
> looks strange.
Well, I'm quite new to all this, so I probably am strange ;-)
What I was trying to say, was that I have many lines with a
$variable = $variable_entry -> get;
on them, and I wanted to make it into a loop for each variable in an array,
so it woule end up something like,
foreach $thing (@my_list) {
$variable = $variable_entry -> get [where $variable is a $thing fom
@my_list]
}
> Much nicer. I'm also sure you can get something like this to work, if
> it only were clear what you actually need. I believe your first step
> must be to find the right data structure(s) for your problem. A bunch of
> single scalar variables has too little structure to be conveniently
> handled. In fact, most programmers would start thinking of something else
> when the variable count reaches 10 or so, not to speak of 69 or 100. A
> hash, used as a data record, looks like a good candidate.
Right then, hash learning coming up! ;-)
Thanks.
R.
------------------------------
Date: 10 Jan 2003 09:26:16 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Converting CGI to XML
Message-Id: <3e1f0238@news.victoria.tc.ca>
Kåre Olai Lindbach (barbr-en@online.no) wrote:
: On 10 Jan 2003 11:17:27 GMT, Tina Mueller <usenet@tinita.de> wrote:
: >Keith R. <kmrob72@comcast.net> wrote:
: >> Does anybody know if it's possible to convert CGI to XML?
: >
: >s/CGI/XML/;
: >
: >SCNR =)
: $Tina =~ s/SMOKER/NON_SMOKER/;
Or more commonly
while ($Tina =~ /SMOKER/)
{
$Tina =~ s/SMOKER/NON_SMOKER/;
}
------------------------------
Date: Fri, 10 Jan 2003 16:37:27 GMT
From: "127.0.0.1" <craznar01@hotmail.com>
Subject: Re: Finding required .pl's home ?
Message-Id: <Xns93001AAB4587C127001craznarcom@61.9.128.12>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in news:avjhrn$3lg$1
@mamenchi.zrz.TU-Berlin.DE:
> ...or from $INC{ 'Shared.pl'} (after "require Shared").
Sorry - don't understand this one.... where in Shared.pl do I put this ?
--
HOTMAIL T&C: "Thanks for writing to MSN Hotmail. This is XXXX and I am
writing in response to your inquiry regarding the status of your account. I
apologize for the inconvenience this has caused you, however, we cannot
provide you with the information you needed. Hotmail, in its sole
discretion, may terminate your account for violation of the letter or spirit
of the TOS. Upon termination of the Service, your right to use the Service
immediately ceases. Hotmail shall have no obligation to maintain any content
in your account or to forward any unread or unsent messages to you or any
third party. I appreciate your understanding."
: Questions to service_x@hotmail.com
------------------------------
Date: Fri, 10 Jan 2003 16:38:15 GMT
From: "127.0.0.1" <craznar01@hotmail.com>
Subject: Re: Finding required .pl's home ?
Message-Id: <Xns93001ACE0A60C127001craznarcom@61.9.128.12>
[posted and mailed]
Brian McCauley <nobull@mail.com> wrote in news:u9fzs2ofue.fsf@wcl-
l.bham.ac.uk:
>> How do I find (from within) the directory a required .pl file resides in?
>
> It can be inferred from __FILE__
>
Thanks... I'll give that a burl....
--
HOTMAIL T&C: "Thanks for writing to MSN Hotmail. This is XXXX and I am
writing in response to your inquiry regarding the status of your account. I
apologize for the inconvenience this has caused you, however, we cannot
provide you with the information you needed. Hotmail, in its sole
discretion, may terminate your account for violation of the letter or spirit
of the TOS. Upon termination of the Service, your right to use the Service
immediately ceases. Hotmail shall have no obligation to maintain any content
in your account or to forward any unread or unsent messages to you or any
third party. I appreciate your understanding."
: Questions to service_x@hotmail.com
------------------------------
Date: 10 Jan 2003 18:56:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Finding required .pl's home ?
Message-Id: <avn50o$5ri$1@mamenchi.zrz.TU-Berlin.DE>
127.0.0.1 <craznar01@hotmail.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in news:avjhrn$3lg$1
> @mamenchi.zrz.TU-Berlin.DE:
>
> > ...or from $INC{ 'Shared.pl'} (after "require Shared").
>
> Sorry - don't understand this one.... where in Shared.pl do I put this ?
Anywhere you want. %INC is a magic hash that contains, keyed on the module
name, the file a module was loaded from. Perl uses it internally to see
if a module is already loaded, but you're free to use it. See "perldoc
perlvar" for the full story.
Anno
------------------------------
Date: 10 Jan 2003 07:54:54 -0800
From: ls@worldpath.net (Lance)
Subject: How does one Iterate an array to remove duplicates?
Message-Id: <c48c82ed.0301100754.5687e6b0@posting.google.com>
Greetings from Perl plebe land!
Before I start, it is important to note that I am not only new to
Perl, but programming as a whole. I've mixed a little php in with my
html, but that's about it. That being said, I find myself tasked with
a project I am not sure how to proceed with, and I ask for your help.
I need to audit a web server to find out which web dirs have cgi
installed, and the same for FPX and which are using php. I have come
up with a way to search out these directories and compile a list.
However, I need a way to iterate through the list to remove duplicate
parent directories. Here is what I have thus far;
#! /usr/bin/perl -w
@cgi = `find /www3 -name cgi-bin`;
@fpx = `find /www3 -name "*_vti_pvt*"`;
foreach $domain (@cgi) {
if ($domain =~ m#^/#) {
$domain =~ s#/cgi-bin##;
push(@newcgi, "${domain}");
}
}
foreach $domain (@fpx) {
if ($domain =~ m#^/#) {
$domain =~ s#/_vti_pvt##;
push(@newfpx, "${domain}");
}
}
open(WEBS, ">>/www2/kryhavok/websaudit");
print WEBS "\#\#These have CGI installed\#\#\n\n";
print WEBS sort @newcgi;
print WEBS "\n\#\#These have FPX installed\#\#\n\n";
print WEBS sort @newfpx;
close(WEBS);
This returns a list that looks like this;
##These have CGI installed##
/www3/altonyouthleague
/www3/apache-1.3.12-php4
/www3/bigideagroup
/www3/bigideahunt
/www3/binaryaddiction
/www3/binaryaddiction/.cpan/build/libwww-perl-5.53/t/net
/www3/binaryaddiction/awstats-3.2/wwwroot
/www3/blueigloo
/www3/blueigloo/members/sa
/www3/cccote
/www3/chameleonsales
(....)
##These have FPX installed##
/www3/2ferment
/www3/2ferment/catalog
/www3/absoluteconcepts
/www3/ci.rochester
/www3/cramerfabrics/www
/www3/cramerfabrics/www/Test_Area
/www3/exploringgod/www
/www3/exploringgod/www.old
(....)
How do I go about going back through the list and remove re-a curing
parent directories like(for example);
/www3/cramerfabrics/www
/www3/cramerfabrics/www/Test_Area
and end up with one element /www3/cramerfabrics/www?
Any and all input greatly appreciated.
Thank you for your time,
-Lance
------------------------------
Date: 10 Jan 2003 18:09:23 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: How does one Iterate an array to remove duplicates?
Message-Id: <u9lm1slvvw.fsf@wcl-l.bham.ac.uk>
ls@worldpath.net (Lance) writes:
> Subject: How does one Iterate an array to remove duplicates?
This looks very like FAQ: "How can I remove duplicate elements from a
list or array?"
> Before I start, it is important to note that I am not only new to
> Perl, but programming as a whole.
For a fuller list of things that it is important to note note before
you start, please see the Guidelines for the use of this newsgroup
that are regularly posted here.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 10 Jan 2003 12:22:29 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: How does one Iterate an array to remove duplicates?
Message-Id: <slrnb1u3r5.sch.tadmc@magna.augustmail.com>
Lance <ls@worldpath.net> wrote:
> Before I start, it is important to note that I am not only new to
> Perl, but programming as a whole.
> I need to audit a web server to find out which web dirs have cgi
> installed, and the same for FPX
What is FPX ?
> and which are using php. I have come
> up with a way to search out these directories and compile a list.
But you seem to be including files as well as directories in the list.
> However, I need a way to iterate through the list to remove duplicate
> parent directories.
If you put only directories in the list in the first place,
then you wouldn't need to do that.
You can use a "file test" to see if it is a directory.
perldoc -f -d
> #! /usr/bin/perl -w
Enabling warnings is good.
Enabling strictures, and declaring your variables would be better.
use strict;
> @cgi = `find /www3 -name cgi-bin`;
my @cgi = grep -d, `find /www3 -name cgi-bin`; # dirs only
> foreach $domain (@cgi) {
> if ($domain =~ m#^/#) {
> $domain =~ s#/cgi-bin##;
You don't need two pattern matches. One will do:
if ( $domain =~ s#/cgi-bin## ) {
> push(@newcgi, "${domain}");
^ ^ ^^
^ ^ ^^
None of those punctuation characters are needed:
push(@newcgi, $domain);
> open(WEBS, ">>/www2/kryhavok/websaudit");
You should always, yes *always*, check the return value from open():
open(WEBS, ">>/www2/kryhavok/websaudit") or
die "could not open '/www2/kryhavok/websaudit' $!";
> This returns a list that looks like this;
^^^^^^^^^^^^
No it doesn't.
It _outputs_ a list that looks like that.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 10 Jan 2003 16:24:15 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: I need to create a .zip file, as an attachment in email! is there an easy way to do this?
Message-Id: <1071594.fu5e9NZNaS@nyoga.dubu.de>
Grimbo wrote:
The process is:
> 1. Receive $string from form! (Remain in memory if pos small file)
CGI.pm
> 2. Convert $string to .zip file format (windows etc.)!
Archive::Zip
> 3. Convert .zip file into format $string2 acceptable as attachment to
> an email (text)
MIME::Lite
> 4. Send attachment $string2 and email content.
MIME::Lite
HTH,
Harald
--
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"Weinberg's Second Law"
If builders built buildings the way programmers wrote programs, then
the first woodpecker that came along would have destroyed civilization.
------------------------------
Date: Fri, 10 Jan 2003 16:10:37 +0000 (UTC)
From: Clay Irving <clay@panix.com>
Subject: Re: Isogest 3.1 New release
Message-Id: <slrnb1ts3t.cdq.clay@panix2.panix.com>
In article <avkuge$fuu$1@lacerta.tiscalinet.it>, Dante Ortolani wrote:
> Hi,
> you can try on line by a demo the last release of my software written in
> pure perl.
> Isogest is a Office Suite that work with Mysql or Postgres.
>
> Site http://isogest.sourceforge.net/
> Demo http://isogest.sourceforge.net/Demo/
Which gives:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable
to complete your request.
--
Clay Irving <clay@panix.com>
IMMODEST, adj. Having a strong sense of one's own merit, coupled with a
feeble conception of worth in others.
- Ambrose Bierce
------------------------------
Date: 10 Jan 2003 06:55:23 -0800
From: schandl@gmx.net (Bernd Schandl)
Subject: Matching entries in lists
Message-Id: <7b52b64e.0301100655.1a7035c3@posting.google.com>
I have the following problem: I have a list where each entry is a telephone
number, a name and a category. In a second list, I have telephone numbers
(in the same format) and I want to check whether they appear in the first
list and if yes, what the corresponding name and category is. As an
additional difficulty, the numbers in the first list are not necessarily
complete numbers but rather the beginning of a telephone number (think area
code or company). So for the entry 12345678 in the second list, I want to be
able to find the entry (123,"Some company","work") in the first list. I only
need to find the first match.
So my questions are:
- What is the best data structure for the first list? Some kind of array?
Two hashes?
- What is a good/efficient/elegant way to find an entry in the first list
matching a number from the second?
I would appreciate any help or hints!
Bernd
------------------------------
Date: 10 Jan 2003 15:21:34 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Matching entries in lists
Message-Id: <avmodu$qem$1@mamenchi.zrz.TU-Berlin.DE>
Bernd Schandl <schandl@gmx.net> wrote in comp.lang.perl.misc:
> I have the following problem: I have a list where each entry is a telephone
> number, a name and a category. In a second list, I have telephone numbers
> (in the same format) and I want to check whether they appear in the first
> list and if yes, what the corresponding name and category is. As an
> additional difficulty, the numbers in the first list are not necessarily
> complete numbers but rather the beginning of a telephone number (think area
> code or company). So for the entry 12345678 in the second list, I want to be
> able to find the entry (123,"Some company","work") in the first list. I only
> need to find the first match.
>
> So my questions are:
> - What is the best data structure for the first list? Some kind of array?
> Two hashes?
A hash (why two?) is the usual Perl answer to a lookup problem.
Out of the "first list", build a hash keyed on the phone numbers
with the values containing the other info (as a string, or as a
listref, it doesn't matter).
> - What is a good/efficient/elegant way to find an entry in the first list
> matching a number from the second?
sub lookup {
my ( $table, $number) = @_;
# find the longest prefix of $number that is in $table
while ( length $number ) {
last if exists $table->{ $number};
chop $number;
}
( $number, @{ $table->{ $number}} ) if length $number;
return;
}
This is untested. You may want to limit the length of the prefixes
you're testing to some minimal length or you'll get spurious matches.
Anno
------------------------------
Date: 10 Jan 2003 07:53:01 -0800
From: bl8n8r@yahoo.com (bad_knee)
Subject: muli-spaced fields and split()
Message-Id: <e817ca4d.0301100753.7e49400b@posting.google.com>
Hello,
I was in the middle of parsing a logfile when I realized some
fields have 2 spaces instead of one. This of couse screws up
my field order when i split(/ /, $_) with a 1-space delimiter.
Thu Jan 9 16:00:18 2003 1098 foo.bar.org 3333 bar.txt
Fri Jan 10 07:16:13 2003 9 foo.bar.org 7662600 foo.txt
note the double spaces between "Jan" and "9". Someone must
have a favourite way of getting rid of the extra spaces. Please
share.
Thanks
------------------------------
Date: Fri, 10 Jan 2003 16:59:39 +0100
From: Kåre Olai Lindbach <barbr-en@online.no>
Subject: Re: muli-spaced fields and split()
Message-Id: <j9rt1vs0bgkdrlphj4ekqsv8opnc2btmi2@4ax.com>
On 10 Jan 2003 07:53:01 -0800, bl8n8r@yahoo.com (bad_knee) wrote:
>Hello,
>
>I was in the middle of parsing a logfile when I realized some
>fields have 2 spaces instead of one. This of couse screws up
>my field order when i split(/ /, $_) with a 1-space delimiter.
multi-space ;-): split(' ',$_);
same as split(/\s+/,$_);
>
>Thu Jan 9 16:00:18 2003 1098 foo.bar.org 3333 bar.txt
>Fri Jan 10 07:16:13 2003 9 foo.bar.org 7662600 foo.txt
>
>note the double spaces between "Jan" and "9". Someone must
>have a favourite way of getting rid of the extra spaces. Please
>share.
maybe: tr/ / /sd;
--
mvh/Regards
Kåre Olai Lindbach
------------------------------
Date: Fri, 10 Jan 2003 17:03:43 +0100
From: Kåre Olai Lindbach <barbr-en@online.no>
Subject: Re: muli-spaced fields and split()
Message-Id: <qkrt1v8tm032c82d6lpsbqistc2icg3vpm@4ax.com>
On Fri, 10 Jan 2003 16:59:39 +0100, Kåre Olai Lindbach
<barbr-en@online.no> wrote:
>On 10 Jan 2003 07:53:01 -0800, bl8n8r@yahoo.com (bad_knee) wrote:
>>note the double spaces between "Jan" and "9". Someone must
>>have a favourite way of getting rid of the extra spaces. Please
>>share.
>
>maybe: tr/ / /sd;
Sorry, I've used d-option too much lately:
tr/ / /s;
--
mvh/Regards
Kåre Olai Lindbach
------------------------------
Date: Fri, 10 Jan 2003 11:51:11 -0500
From: Andre <alecler@sympatico.ca>
Subject: Re: muli-spaced fields and split()
Message-Id: <alecler-69A750.11511110012003@news1.qc.sympatico.ca>
In article <e817ca4d.0301100753.7e49400b@posting.google.com>,
bl8n8r@yahoo.com (bad_knee) wrote:
> I was in the middle of parsing a logfile when I realized some
> fields have 2 spaces instead of one. This of couse screws up
> my field order when i split(/ /, $_) with a 1-space delimiter.
split() with no arguments implicitely splits $_ on whitespace.
$_ = '1 2 3 4 5 6 7';
print join('*', split), "\n";
Result:
1*2*3*4*5*6*7
Have a look at the entry for split() in the perlfunc manual.
Andre
------------------------------
Date: Fri, 10 Jan 2003 15:25:18 -0000
From: "Richard S Beckett" <spikey-wan@bigfoot.com>
Subject: Re: My, our, etc.
Message-Id: <avmomh$mpd$1@newshost.mot.com>
"Koos Pol" <koos_pol@NO.nl.JUNK.compuware.MAIL.com> wrote in message
news:newscache$u43i8h$cb7
> What were the warnings/errors? Commenting on them without seeing them is
> hard to do.
<test.pl>
use strict;
use warnings;
use Tk;
my $entry = $frame -> Entry (
-validatecommand => \&valid_routine,
-invalidcommand => sub {
print "invalid entry, try again\n";
$entry -> focus;}
) -> pack ();
</test.pl>
Global symbol "$frame" requires explicit package name at test.pl line 4.
Global symbol "$entry" requires explicit package name at test.pl line 8.
Execution of test.pl aborted due to compilation errors.
<test1.pl>
use strict;
use warnings;
use Tk;
our $entry = $frame -> Entry (
-validatecommand => \&valid_routine,
-invalidcommand => sub {
print "invalid entry, try again\n";
$entry -> focus;}
) -> pack ();
</test1.pl>
Variable "$entry" is not imported at test1.pl line 8.
Global symbol "$frame" requires explicit package name at test1.pl line 4.
Global symbol "$entry" requires explicit package name at test1.pl line 8.
Execution of test1.pl aborted due to compilation errors.
<test2.pl>
use strict;
use warnings;
use Tk;
our ($frame, $entry);
$entry = $frame -> Entry (
-validatecommand => \&valid_routine,
-invalidcommand => sub {
print "invalid entry, try again\n";
$entry -> focus;}
) -> pack ();
<test2.pl>
Can't call method "Entry" on an undefined value at test2.pl line 10. [Which
we'll call success, in this instance.]
See what I mean?
R.
------------------------------
Date: Fri, 10 Jan 2003 15:31:59 -0000
From: "Richard S Beckett" <spikey-wan@bigfoot.com>
Subject: Re: My, our, etc.
Message-Id: <avmp31$n0h$1@newshost.mot.com>
"Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote
> Adding 'my $entry' on top would have also worked, I guess.
>
> If you have something like:
>
> my $var = ... $var ...;
>
> you actually have two different variables: the lexical $var on the left
> side and the global variable $var ($main::var actually) on the right
> hand side. Basically, you can't use the lexical in the same statement
> where you declare it using my(). The most obvious workaround for your
> code would be:
>
> my $entry;
> $entry = $frame -> Entry (
> -validatecommand => \&valid_routine,
> -invalidcommand => sub {
> print "invalid entry, try again\n";
> $entry->focus }
> },
> )->pack;
>
> thus turning the declaration and the assignment into two statements. By
> the time you assign something to $entry it has already been made a
> lexical and perl knows about it.
>
> > So, why should I stop using our to predifine variables, when I get no
> > trouble doing it that way, but other ways seem to get me into trouble
with
> > use strict?
>
> Because what you do is a fix that works without your knowing why. :-)
> With the above technique you wont have problems using strictures without
> relying on global variables (as they are declared by our()).
This is killing me!
As far as I can see you have the following types of variables...
1. Ones that are just used within the current subroutine, and not needed
anywhere else.
2. Ones that are used in the current subroutine, and passed to
sub-subroutines, but are not needed anywhere else.
3. Ones that are global to the script, and are all over the place.
4. Ones that go out of the script, like being saved to a text file, and come
back in, by reading the file.
I know that I will need to use our for type 4, but what about the others?
What's the harm in using our, if you have a type 3 variable? In fact, as
long as they're not duplicated, what's wrong with our anyway, just to be on
the safe side?
Thanks.
R.
------------------------------
Date: Fri, 10 Jan 2003 16:45:59 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: My, our, etc.
Message-Id: <avmtc7$fdj$1@wisteria.csv.warwick.ac.uk>
"Richard S Beckett" <spikey-wan@bigfoot.com> wrote:
>"Koos Pol" <koos_pol@NO.nl.JUNK.compuware.MAIL.com> wrote in message
>news:newscache$u43i8h$cb7
>
>> What were the warnings/errors? Commenting on them without seeing them is
>> hard to do.
>
><test2.pl>
>use strict;
>use warnings;
>use Tk;
>our ($frame, $entry);
Did you try using my here?
>$entry = $frame -> Entry (
> -validatecommand => \&valid_routine,
> -invalidcommand => sub {
> print "invalid entry, try again\n";
> $entry -> focus;}
> ) -> pack ();
><test2.pl>
>
>Can't call method "Entry" on an undefined value at test2.pl line 10. [Which
>we'll call success, in this instance.]
>
>See what I mean?
I think you need to understand better the differences between my() and our().
Read perldoc perlsub (Private Variables via my()) and perldoc -f our. The
distinction between lexical and dynamic variables in Perl is quite subtle...
Basically, our() declares a global, with all the bad things that implies. my()
declares something more like what most languages would call a local variable.
(Yes, I know that's simplified.)
Your problem in this instance is that the RHS of the assignment is evaluated
_before_ the LHS, so $entry has not yet been declared (either with my or our)
at that point, and you get an error. Putting an our() declaration on the
previous line means that it is declared, but equally so would using my.
I would strongly recommend reading a copy of 'Programming Perl' if you can.
HTH
Ben
------------------------------
Date: Fri, 10 Jan 2003 17:05:11 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: My, our, etc.
Message-Id: <avmug7$g3h$1@wisteria.csv.warwick.ac.uk>
"Richard S Beckett" <spikey-wan@bigfoot.com> wrote:
>This is killing me!
:) Variable scope is one of the Tricky Things to Learn when you're learning to
be a programmer. Perl's scoping rules are slightly more complex than other
languages', mostly for historical reasons. Don't worry: you'll get it in the
end!
>As far as I can see you have the following types of variables...
>
>1. Ones that are just used within the current subroutine, and not needed
>anywhere else.
These are my() variables.
>2. Ones that are used in the current subroutine, and passed to
>sub-subroutines, but are not needed anywhere else.
You don't need any of these. Pass them in as arguments. It's much better
practice. Ignore anyone who tells you can local() a global and it will have
this effect: local() is for _experts_ _only_.
>3. Ones that are global to the script, and are all over the place.
These are my() variables, declared at file scope (ie. at the top of the file,
outside any subs).
>4. Ones that go out of the script, like being saved to a text file, and come
>back in, by reading the file.
>
>I know that I will need to use our for type 4, but what about the others?
No, no, no, you don't. Variables are not saved to a text file: values are.
If you write
open my $FH, ">file";
my $text = "foo";
print $FH $text;
it's not $text which gets written to file, it's the string 'foo'. When you
read it back, it's read back into a different variable (it may happen to have
the same name, but if it's in a different invocation of the script then it's a
different variable).
>What's the harm in using our, if you have a type 3 variable? In fact, as
>long as they're not duplicated, what's wrong with our anyway, just to be on
>the safe side?
In general, you should avoid globals. Why? You've just answered that: because
you have to be sure they're not duplicated. Believe me, when a program gets any
size at all, this bcomes very hard. That is the whole reason why non-global
variables were invented in the first place: so you could be sure you weren't
accidentally getting the value of a variable from somewhere quite else in the
program. Remember that you don't only have a problem if you deliberately give
two variables the same name: you also have a problem if you misstype a
variable so you name a different variable instead. These sorts of bugs are
_really_ hard to find...
Apart from that, an our() global differs from a my() global at file scope only
if your program is split across several files. A my() variable is invisible
outside of it's enclosing lexical scope: in this case the file; an our()
variable is visible _everywhere_, including in any modules you use()[1]. You
need not worry about the distinction unless you split your program across
several files. Just declare all your globals at the top with my(), and then
consider _very_ hard whether you actually need them to be global.
Ben
[1] Subject of course to packages, but I on't think we'll go into that yet...
------------------------------
Date: Fri, 10 Jan 2003 11:40:22 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: My, our, etc.
Message-Id: <slrnb1u1c6.sch.tadmc@magna.augustmail.com>
Ben Morrow <mauzo@mimosa.csv.warwick.ac.uk> wrote:
> "Richard S Beckett" <spikey-wan@bigfoot.com> wrote:
>:) Variable scope is one of the Tricky Things to Learn when you're learning to
> be a programmer. Perl's scoping rules are slightly more complex than other
> languages', mostly for historical reasons.
Mostly because Perl has *two* separate systems of scoping.
This explains it nicely:
"Coping with Scoping":
http://perl.plover.com/FAQs/Namespaces.html
>>2. Ones that are used in the current subroutine, and passed to
>>sub-subroutines, but are not needed anywhere else.
>
> You don't need any of these. Pass them in as arguments. It's much better
> practice.
That is true.
> Ignore anyone who tells you can local() a global and it will have
> this effect: local() is for _experts_ _only_.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That is not true.
You should always prefer my() (lexical) variables over
local() (package) variables, except when you can't.
One place where you can't is for Perl's built-in variables.
Even a beginner should do:
{ local $/;
# do input here
}
rather than:
$/ = undef;
# do input here
>>3. Ones that are global to the script, and are all over the place.
>
> These are my() variables, declared at file scope (ie. at the top of the file,
> outside any subs).
>>What's the harm in using our, if you have a type 3 variable?
It opens you up to "action at a distance", a Bad Thing.
> In general, you should avoid globals.
True again.
Global variables expose you to action-at-a-distance problems too.
> Apart from that, an our() global differs from a my() global at file scope only
> if your program is split across several files.
An intermediate (ie. non-expert) Perl programmer might split their
program into several files.
So that is yet another "where you can't" case.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 10 Jan 2003 07:57:32 -0800
From: mike_solomon@lineone.net (Mike Solomon)
Subject: Net::Mysql - capture error messages
Message-Id: <56568be5.0301100757.7f118617@posting.google.com>
I am using Net::Mysql to connect to MySql and it works
The problem with it (or me) if that I can't capture any MySql error
messages
Is there a way to do this using Net::MySql ?
I originally intented to use DBI which I have used in the past, on a
Linux box, but when I use ActiveState's PPM (I am running on windows)
to find the correct module the only thing it gives me is DBD-mysqlPP
which instals Net:Mysql
Any help with either capturing error messages from Net:MySql or
getting DBI working would be much appreciated.
If I try the code I used to use:
use DBI ();
my $host = "localhost";
my $database = "test";
my $user = "mike";
my $password = "mike";
my $dbh;
# Connect to the database.
if ($dbh = DBI->connect("DBI:mysql:database=$database;host=$host","$user",
"$password")) {
print "VALID";
} else { print "INVALID"; }
I get the following error message
install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC
contains: C:/Perl/lib C:/Perl/site/lib .) at (eval 1) line 3.
Perhaps the DBD::mysql perl module hasn't been fully installed,
Any help will be gratefully received
Thanks
Mike Solomon
------------------------------
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 4377
***************************************