[17940] in Perl-Users-Digest
Perl-Users Digest, Issue: 100 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jan 19 14:10:32 2001
Date: Fri, 19 Jan 2001 11:10:14 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <979931414-v10-i100@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 19 Jan 2001 Volume: 10 Number: 100
Today's topics:
Perl OOP questions jamdagni@my-deja.com
Re: Perl OOP questions <aqumsieh@hyperchip.com>
Re: Perl OOP questions nobull@mail.com
Re: Perl OOP questions <olthoff@multiboard.com>
Re: Perl Style Guide - uncuddled elses nobull@mail.com
Question about a blessed glob koppum@my-deja.com
Re: Question about a blessed glob nobull@mail.com
Re: Question about domain users/groups nobull@mail.com
Suppressing stderr on `` commands <wgunderman@web.compuserve.com>
Testing for defined() gives me a warning (LMC)
Re: Testing for defined() gives me a warning nobull@mail.com
Text::CSV related question (Miguel Manso)
Re: Writing Unix-format files on NT nobull@mail.com
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 19 Jan 2001 15:59:12 GMT
From: jamdagni@my-deja.com
Subject: Perl OOP questions
Message-Id: <949o89$iu2$1@nnrp1.deja.com>
Hi:
I've got a couple of questions. Hope you can answer them.
1. How do I write a constructor for a class to create an object that is
an exact copy of another object of that same class? In other words,
assume I have object A of class C. Now, I want to create an object B
(also of class C) that has the exact same field values as object A. I
would like to copy over all the fields of object A to object B.
Remember that one or more fields (members) of this class may be arrays,
not just scalars.
2. How do I pass arrays to a function? More importantly, how do I use
the array in the function? Is it passed as a reference in the function?
For example, if I call a function F1 like this F1($a, @b, $c), do I
access it in F1 as $_[0], @$_[1] & $_[2]? Or, is there some other way
to do it?
Thanx in advance,
Swaroop (jamdagni@yahoo.com)
P.S.: If possible, please cc: your replies to me at the above email
address. Thanx, again.
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Fri, 19 Jan 2001 17:20:58 GMT
From: Ala Qumsieh <aqumsieh@hyperchip.com>
Subject: Re: Perl OOP questions
Message-Id: <7abst3qwzo.fsf@merlin.hyperchip.com>
jamdagni@my-deja.com writes:
> 1. How do I write a constructor for a class to create an object that is
> an exact copy of another object of that same class? In other words,
> assume I have object A of class C. Now, I want to create an object B
> (also of class C) that has the exact same field values as object A. I
> would like to copy over all the fields of object A to object B.
> Remember that one or more fields (members) of this class may be arrays,
> not just scalars.
You constructor might look like this:
sub new {
my $self = shift;
if (ref $self) {
# case 1
} else {
# case 2
}
Case 1 means that $self has already been bless()ed, which means that it
is an instance of the class. This means that the constructor has been
called as so:
$object->new();
Where $object is an object of the class. In there, you should do the
copying of all the attributes yourself into a new data structure (hash
or otherwise), bless() it into the correct package, and return it.
For case 2, the constructor has been called using the package name:
C->new();
(assuming C is your package name). In this case, you create a new
object, with the default settings (plus any variables you pass).
> 2. How do I pass arrays to a function? More importantly, how do I use
> the array in the function? Is it passed as a reference in the function?
IMHO, you shouldn't really be programming in OOPerl if you don't know
how to pass an array to a subroutine. Understanding how to handle Perl's
data structures is the key to understanding Perl's OO (that's my opinion
of course).
Anyway, usually you pass an array by reference to a subroutine. Have a
look at perlsub, perldsc, perllol. And do yourself a favour, and buy
Damian Conway's 'Object Oriented Perl'. It's one of the best books our
there.
--Ala
PS. comp.lang.perl is dead. Stop posting to it.
------------------------------
Date: 19 Jan 2001 17:53:58 +0000
From: nobull@mail.com
To: jamdagni@yahoo.com
Subject: Re: Perl OOP questions
Message-Id: <u9k87rctrz.fsf@wcl-l.bham.ac.uk>
jamdagni@my-deja.com writes:
> Newsgroups: comp.lang.perl,comp.lang.perl.misc
comp.lang.perl does not exist.
> I've got a couple of questions.
If you have two or more unrelated questions then do not post them in a
single article. One thread => one subject line => one subject! Your
second question is not OOP related.
> Hope you can answer them.
Homework? This definitely smells like homework to me.
Please check the FAQ _before_ you post. Particularly before you post
your homework exercises as homework exercises are often take from the
FAQ.
> 1. How do I write a constructor for a class to create an object that is
> an exact copy of another object of that same class? In other words,
> assume I have object A of class C. Now, I want to create an object B
> (also of class C) that has the exact same field values as object A. I
> would like to copy over all the fields of object A to object B.
> Remember that one or more fields (members) of this class may be arrays,
> not just scalars.
FAQ: "How do I print out or copy a recursive data structure?"
> 2. How do I pass arrays to a function?
FAQ: "How can I pass/return a {Function, FileHandle, Array, Hash,
Method, Regexp}?"
> More importantly, how do I use the array in the function? Is it
> passed as a reference in the function?
Oh, so you've heard of references? I that case you can skip the FAQ
as it simply tells you to read perlsub/"Pass by Reference" and perlref.
> For example, if I call a function F1 like this F1($a, @b, $c), do I
> access it in F1 as $_[0], @$_[1] & $_[2]?
@$_[1] parses as @{$_}[1] but you mean @{$_[1]} so you need to put the
{} in. Of course F1 will need a suitable prototype. See
perlsub/Prototypes.
> Or, is there some other way to do it?
This is Perl! There's always some other way to do it!
> P.S.: If possible, please cc: your replies to me at the above email
> address. Thanx, again.
Please do not ask _us_ to do things you could ask our computers to do.
Please use the Mail-Copies-To header.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 19 Jan 2001 13:51:54 -0500
From: "Darryl Olthoff" <olthoff@multiboard.com>
Subject: Re: Perl OOP questions
Message-Id: <94a2cc$s1k$1@panther.uwo.ca>
> 2. How do I pass arrays to a function? More importantly, how do I use
> the array in the function? Is it passed as a reference in the function?
> For example, if I call a function F1 like this F1($a, @b, $c), do I
> access it in F1 as $_[0], @$_[1] & $_[2]? Or, is there some other way
> to do it?
&Sample(@Array);
sub Sample {
my @Array = @_;
# use @Array as you wish
};
&Sample(\@Array);
sub Sample {
my $Ref = shift(@_);
# use @$Ref as you wish.
};
I don't know of a way to pass a scalar after an array and have the function
know that this is happening. That is not to say that it can't be done, I
just don't know how to do it. I pass references to the arrany anyways....
------------------------------
Date: 19 Jan 2001 18:01:54 +0000
From: nobull@mail.com
Subject: Re: Perl Style Guide - uncuddled elses
Message-Id: <u9ae8nctf1.fsf@wcl-l.bham.ac.uk>
birgitt@my-deja.com writes:
> I am reading the Perl Style Guide and don't understand what the meaning
> of "uncuddled elses" is. What is meant by "uncuddled" ?
if ( condition ) {
foo;
} else { # This is a cuddled else!
bar;
}
if ( condition ) {
foo;
}
else { # This is an uncuddled else!
bar;
}
if ( condition )
{
foo;
}
else # This is also an uncuddled else!
{
bar;
}
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 19 Jan 2001 16:57:08 GMT
From: koppum@my-deja.com
Subject: Question about a blessed glob
Message-Id: <949rku$m9s$1@nnrp1.deja.com>
Hi!
I appreciate if someone would help me on my problem:
I created a class based on a blessed typeglob as a class (actually, I
inherited IO::File).
Now, I want to add attributes to my class. I can not do this as I would
to an HASH. (like, $myObj->{myAttr} = $attrVal). Because $myObj is
infact a GLOB reference and not a hash reference.
How do I add attributes to a class that is blessed as a GLOB?
Thanks in advance.
Murali.
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 19 Jan 2001 18:03:28 +0000
From: nobull@mail.com
Subject: Re: Question about a blessed glob
Message-Id: <u966jbctcf.fsf@wcl-l.bham.ac.uk>
koppum@my-deja.com writes:
> I created a class based on a blessed typeglob as a class (actually, I
> inherited IO::File).
>
> Now, I want to add attributes to my class. I can not do this as I would
> to an HASH. (like, $myObj->{myAttr} = $attrVal). Because $myObj is
> infact a GLOB reference and not a hash reference.
${*$myObj}{mtAttr} = $attrVal;
There are numerous examples of this in the standard IO::* modules. Do
not be affraid to "use the source, Luke".
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 19 Jan 2001 18:09:46 +0000
From: nobull@mail.com
Subject: Re: Question about domain users/groups
Message-Id: <u9elxzcth2.fsf@wcl-l.bham.ac.uk>
"Arthuro" <hexorhaxor@vaxor.com> writes:
> Is there a way to extract a list of users and the groups they're in from a
> primary domain server (Windows NT)?
There are at least modules supplied with ActiveState Perl providing
interfaces to the relevant Win32 API:
Win32::NetAdmin
Win32API::Net
BTW: The documentation of these modules is full of trival mistakes but
mostly you can work out what's needed.
use Win32API::Net 'GetDCName', 'GroupEnum';
sub global_groups {
my $DomainController='';
GetDCName('','',$DomainController) or die "$! $^E";
my @groups;
GroupEnum($DomainController,\@groups) or die "$! $^E";
@groups;
}
Figuring out the one to list users is left as an exercise for the
reader.
You may also want to visit http://www.roth.net/perl
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 19 Jan 2001 13:01:45 -0500
From: "Billy G" <wgunderman@web.compuserve.com>
Subject: Suppressing stderr on `` commands
Message-Id: <949vh7$no2$1@sshuraaa-i-1.production.compuserve.com>
I want to suppress stderr output from `` commands because the output is not
serialized with output written by the perl script directly. I'm setting
autoflush on stdout and stderr in my perl script and redirecting output to a
log file. This results in serializing the stdout and stderr output lines
but the stderr coming from `` commands appears to get appended at the end
when my script exits rather than serialized with the other output.
How can I suppress stderr or serialize it with the perl script output
stream?
------------------------------
Date: Fri, 19 Jan 2001 12:56:11 -0500
From: "Antoine Beaupre (LMC)" <lmcabea@lmc.ericsson.se>
Subject: Testing for defined() gives me a warning
Message-Id: <3A687FBB.DEA6B43D@lmc.ericsson.se>
I have the following code:
sub epgetpath {
my $ent = shift;
return "" if ($ent eq "");
my ($path, undef) = `epgetpath $ent`;
if (defined($path)) {
chomp $path;
} else {
print "***** WARNING: can't find entity $ent (epgetpath returned:
\"$path\")\n";
}
return $path;
}
When run and the command in backticks fails and give me "undef", I get
the following:
====> Finding entities...
Use of uninitialized value at /home/lmcabea/bin/patch.pl line 138,
<STDIN> chunk 1.
***** WARNING: can't find entity SWI/DBACOMMON (epgetpath returned: "")
Use of uninitialized value at /home/lmcabea/bin/patch.pl line 138,
<STDIN> chunk 1.
***** WARNING: can't find entity SCC/DBACOMMON (epgetpath returned: "")
[line 138 is the "if(defined($path)) {" line]
Duh! Of course it's undefined! That's what I'm testing for! Why the
warning? Isn't that what defined() is made for? How can I remove it?
Even doing:
my ($path) = "";
doesn't help.
I don't get this.
A.
--
La sémantique est la gravité de l'abstraction.
------------------------------
Date: 19 Jan 2001 18:31:38 +0000
From: nobull@mail.com
Subject: Re: Testing for defined() gives me a warning
Message-Id: <u91ytzcs1h.fsf@wcl-l.bham.ac.uk>
"Antoine Beaupre (LMC)" <lmcabea@lmc.ericsson.se> writes:
> if (defined($path)) {
> chomp $path;
> } else {
> print "***** WARNING: can't find entity $ent (epgetpath returned:
> \"$path\")\n";
> }
> ***** WARNING: can't find entity SWI/DBACOMMON (epgetpath returned: "")
> Use of uninitialized value at /home/lmcabea/bin/patch.pl line 138,
> [line 138 is the "if(defined($path)) {" line]
No, that's line 135.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 19 Jan 2001 16:52:47 GMT
From: mmanso@localhost.localdomain (Miguel Manso)
Subject: Text::CSV related question
Message-Id: <slrn96gsf0.gou.mmanso@localhost.localdomain>
--
Miguel Lima Manso - <miguel.manso@prodigio.com>
Departamento Técnico
Prodigio - Produções Digitais Online, S.A.
Tel: +351 22 0106000 - Fax: +351 22 0106001
------------------------------
Date: 19 Jan 2001 17:59:16 +0000
From: nobull@mail.com
Subject: Re: Writing Unix-format files on NT
Message-Id: <u9g0ifctjf.fsf@wcl-l.bham.ac.uk>
"Iain Hosking" <iain_hosking@hotmail.com> writes:
> I'm trying to write a text file which must be used on a Silicon Graphics
> machine. If I set
>
> $\ = "\n";
>
> then the newline character is CR+LF,
$\ does _not_ redefine what the \n escape does. And if it did the
above would be a no-op anyhow! For a description of what $\ _does_ do
see its entry in perlvar.
> How can I create a file with a line delimiter of LF only?
binmode FILE;
print FILE "This is LF terminated\cJ";
Note: On NT \n will work in place of \cJ but it won't necessarily work
on all OSs.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 100
**************************************