[10534] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4126 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 2 04:07:18 1998

Date: Mon, 2 Nov 98 01:00:25 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 2 Nov 1998     Volume: 8 Number: 4126

Today's topics:
        -w (was Re: It works , but why) (Ronald J Kimball)
    Re: Confused about objects in Perl (Tye McQueen)
        Converting HEX to ASCII (Louis Klopper)
    Re: Converting HEX to ASCII <rra@stanford.edu>
    Re: Cookie Rookie <gellyfish@btinternet.com>
    Re: CPU usage <Tony.Curtis+usenet@vcpc.univie.ac.at>
        Example of subclassing in Perl ?? <msaliers@home.com>
    Re: Example of subclassing in Perl ?? <tchrist@mox.perl.com>
        fuser command for Linux (was Re: How to tell if a file  <gellyfish@btinternet.com>
        Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)

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

Date: Sun, 1 Nov 1998 23:32:10 -0500
From: rjk@coos.dartmouth.edu (Ronald J Kimball)
Subject: -w (was Re: It works , but why)
Message-Id: <1dhu93e.13f8oxg11bfw70N@bos-ip-1-115.ziplink.net>

Jarle H Knudsen <no.unsolicited.mail.please@jarle.com> wrote:

> If this is so important, why isn't this on by default? Then there
> could be a switch to turn it of instead.

>From the perl manpage:

   BUGS
     The -w switch is not mandatory.


-- 
 _ / '  _      /         - aka -         rjk@coos.dartmouth.edu
( /)//)//)(//)/(     Ronald J Kimball      chipmunk@m-net.arbornet.org
    /                                  http://www.ziplink.net/~rjk/
        "It's funny 'cause it's true ... and vice versa."


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

Date: 2 Nov 1998 00:40:29 -0600
From: tye@fohnix.metronet.com (Tye McQueen)
Subject: Re: Confused about objects in Perl
Message-Id: <71jk4t$p5k@fohnix.metronet.com>

"Andrew Pollock" <apollock@bit.net.au> writes:
) I admit it, objects in Perl confuse me. I can use them in other languages,
) but when it comes to Perl I get really confused really quickly.
) 
) My confusion stems from methods vs properties. Basically, I can't figure out
) properties of objects under Perl. All the objects seem to have methods, but
) no properties.

Yep.  That is pretty much true.

However, you can have a reference that is an object at the same
time that it is a reference to a tied hash (for example) so that
you can do:

    $ref->Method( arg );

as well as

    $old= $ref->{Property};
    $ref->{Property}= $new;

But that isn't usually done (it is a bit tricky to implement).

) For example, I'm playing with Perl under Win32,

Then you can look at Win32::TieRegistry for an example of what I
described above.

[Attempt to modify a property in Perl OO:]
) $oRs->ActiveConnection = $oConn;
) 
) (Somewhat) understandably, this falls over, with Perl barfing about
) Can't modify subroutine entry in scalar assignment at callview.pl line 23,
) near "$oConn;"
) 
) I'm assuming this is because Perl is expecting me to be talking about a
) method, not a property. How do I reference object _properties_ under Perl?

So, since we don't have object properties, we simulate them
several ways.  The most common is probably having a method named
like a property that can be used two different ways:

    $oldValue= $obj->Property;		# "Get"
    $obj->Property( $newValue );	# "Set"

Note that the "Set" usage still can return a value, so the most
common case (it appears to me) is to have the "Set" usage return
the current (a.k.a. "previous") value for the property:

    $oldValue= $obj->Property( $newValue );

Another useful item to return is the original object so you can use:

    $obj->Property1( $newValue1 )->Property2( $newValue2 );

But there are lots of other ways to do this:

    $oldValue= $obj->GetProperty;
    $obj->SetProperty( $newValue );
or
    ( $oldValue1, $oldValue2 )= $obj->GetProperties( qw(Property1 Property1) );
    $obj->SetProperties( Property1=>$newValue1, Property2=>$newValue2 );
or even
    ( $oldVal1, $oldVal2 )= $obj->Properties( [qw(Prop1 Prop2)] );
    $obj->Properties( { Prop1=>$newVal1, Prop2=>$newVal2 } );

So you weren't missing much.  Just expecting something more, I guess.


And now, I'll consider some of what _could_ be...

You can have your "property" method return a reference so you can
use it more like a property:

Using a scalar reference:
    $oldValue= ${$obj->Property};
    ${$obj->Property}= $newValue;

Using an array reference:
    $oldValue= $obj->Property->[0];
    $obj->Property->[0]= $newValue;

Using a hash reference:
    $oldValue= $obj->Property->{""};
    $obj->Property->{""}= $newValue;

So it'd be nice if Perl had some syntactic sugar for the scalar
reference case, a way to say that a subroutine will be returning
a reference to a scalar and to treat calls to that subroutine
as lvalues by implicitly dereferencing the return value.  Then
Perl object _users_ would have first class properties (while
object writers still need help implementing robust properties --
but modules are cropping up to provide this help).

Perl's OO is pretty new and the implementors have chosen (IMHO
rightly) to "take their time" adding some features.

Yeah, how about a way to "prototype" subroutine return values:

    sub($) ScalarProperty { ... }
    sub(@) ArrayProperty { ... }
    sub(%) HashProperty { ... }

    $obj->ScalarProperty= $val;
    $obj->ArrayProperty= @list;
    $obj->ArrayProperty[5]= $ent;
    @keys= keys $obj->HashProperty;

Seems like not a minor change to the parser.  But it's just a way
to imply the ${...}, @{...}, %{...}, or ->, so shouldn't require
much change outside the parser.  Yeah, that's the ticket.
-- 
Tye McQueen    Nothing is obvious unless you are overlooking something
         http://www.metronet.com/~tye/ (scripts, links, nothing fancy)


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

Date: Mon, 02 Nov 1998 07:23:59 GMT
From: lklopper@mweb.com (Louis Klopper)
Subject: Converting HEX to ASCII
Message-Id: <363d5dc0.3947426@news.mweb.co.za>

How do I convert HEX to ASCII so that I can print the HEX  ..... I
want to see the actual HEX i.e. I want to print something like
C0A1CC5A to the screen ?

Regards
Louis


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

Date: 01 Nov 1998 23:47:11 -0800
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Converting HEX to ASCII
Message-Id: <ylhfwik03k.fsf@windlord.stanford.edu>

Louis Klopper <lklopper@mweb.com> writes:

> How do I convert HEX to ASCII so that I can print the HEX ..... I want
> to see the actual HEX i.e. I want to print something like C0A1CC5A to
> the screen ?

printf ("%x", $number);

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 1 Nov 1998 18:40:57 -0000
From: Jonathan Stowe <gellyfish@btinternet.com>
Subject: Re: Cookie Rookie
Message-Id: <71i9vp$36p$1@gellyfish.btinternet.com>

On Sat, 31 Oct 1998 08:29:23 -0800 Robert Long <rgl34@hotmail.com> wrote:
> I have been programming in Perl for over a year, but I have never had the
> need or opportunity to deal with cookies.  I need to construct a cookie and
> deliver it to the users browser, later check for the cookie.
> 
> The question I have, is how is the delivery of the cookie done?  I believe
> the construction and retrieval of this cookie to be pretty basic.  But for
> some silly reason, I do not know how to deliver it.
> 

Beyond suggesting that you should use the CGI.pm module and read its
very good documentation there is nothing particularly Perlish to this.

> Any help in this is appreciated.  If you know of some resources I can check
> on line through a News group, that would also be great.
> 

The comp.infosystems.www.* newsgroups would be the places to ask most probably
after you had looked at the documentation about cookies on the Netscape
web site.

/J\
-- 
Jonathan Stowe <jns@btinternet.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>


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

Date: 01 Nov 1998 22:47:27 +0100
From: Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at>
Subject: Re: CPU usage
Message-Id: <83ogqrnl00.fsf@vcpc.univie.ac.at>

Re: CPU usage, E-swap <webmaster@eswap.co.uk> said:

E-swap> Hi What are the main contributors in Perl scripts
E-swap> which affect the CPU usage of a server.

Errrr...code? :-)

E-swap> I have a script which generates a lot of dynamic
E-swap> HTML pages, is this the main factor, or are there
E-swap> other things I should be trying to tidy up.

Without seeing your source code it is totally impossible to
say.

-- 
Tony Curtis, Systems Manager, VCPC,    | Tel +43 1 310 93 96 - 12; Fax - 13
Liechtensteinstrasse 22, A-1090 Wien,  | <URI:http://www.vcpc.univie.ac.at/>
"You see? You see? Your stupid minds!  | private email:
    Stupid! Stupid!" ~ Eros, Plan9 fOS.| <URI:mailto:tony_curtis32@hotmail.com>


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

Date: Mon, 02 Nov 1998 00:01:38 GMT
From: "Mark S" <msaliers@home.com>
Subject: Example of subclassing in Perl ??
Message-Id: <CD6%1.12841$yb5.10274198@news.rdc1.sdca.home.com>

Having read over the Camel book, and scanned through the www.perl.com site,
I still can't find anyplace where there's a simple example of how to
subclass in Perl.  Could someone kindly post an example?

Thanks in advance!





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

Date: 2 Nov 1998 00:19:33 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Example of subclassing in Perl ??
Message-Id: <71itql$82n$1@csnews.cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, "Mark S" <msaliers@home.com> writes:
:Having read over the Camel book, and scanned through the www.perl.com site,
:I still can't find anyplace where there's a simple example of how to
:subclass in Perl.  Could someone kindly post an example?

I guess that Chapter 5 the Camel and the perlobj, perltoot, and perlbot
manages were to no avail?  Sigh.

Here's how to make a Carnivore subclassed from a mammal:

    package Carnivore;
    use Mammal;
    @ISA = qw(Mammal);

--tom
-- 
There are still some other things to do, so don't think if I didn't fix
your favorite bug that your bug report is in the bit bucket.  (It may be,
but don't think it.  :-)  Larry Wall in <7238@jpl-devvax.JPL.NASA.GOV>


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

Date: 1 Nov 1998 16:54:00 -0000
From: Jonathan Stowe <gellyfish@btinternet.com>
Subject: fuser command for Linux (was Re: How to tell if a file is already open without flock()?)
Message-Id: <71i3n8$34u$1@gellyfish.btinternet.com>

On 1 Nov 1998 00:28:37 -0000 Jonathan Stowe <gellyfish@btinternet.com> wrote:
> On Fri, 23 Oct 1998 10:45:43 -0400 Kevin Ternes <jkternes@domain-tech.com> wrote:

>> 
>> Does anyone know a means for determining if another process has
>> a file open without making a system() call?
>> 
> 
> I would suggest that the simplest way would be to execute the 'fuser'
> command with the backticks - I think that different systems will have
> different ways to determine the openers of a file.
> 

After having made that post I got curious as to how one might achieve this
so whilst watching Oliver! I started playing around and came up with the
script that follows - of course it depends heavily on the structure of
the /proc directory on ones system and is considerably slower than the
real 'fuser' which probably uses some completely different method.

The basic idea is that the /proc/<PID>/fd directory contains links to the
files that the process has open - so by comparing the device and inode
numbers of these with the target file one is able to determine if they are
the same file - by using the lstat function it is possible to determine the
owner of this link and thus the user who owns the process that has the file
open - as shown with the '-u' option.

So here you go - it aint big and it aint clever - you'll probably need to be
'root' for it to work properly and it will probably only work on Linux
2.0.34 but there you go :


#!/usr/bin/perl -w

use strict;
use Getopt::Std;

my %opt;

getopts('u',\%opt) || die "Invalid argument\n";

$ARGV[0] || die "No file specified \n";

my $rc = 0;

while(my $file = shift @ARGV)
  {

    my @procinfo = ();

    my ( $dev,
         $ino,
         @ostuff ) = stat($file);

    while(</proc/*/fd/*>)
      {
          my @statinfo = stat;
          if (@statinfo)
            {
             if (($dev == $statinfo[0]) && ($ino == $statinfo[1]) )
               {
                 my $user;
                 m{/proc/(\d+)/fd/\d+} && (my $pid = $1);
                 if ($opt{u})
                   {
                    $user = '(' . getpwuid((lstat($_))[4]) . ')';
                   }
                 push @procinfo, "${pid}${user}";
                 $rc++;
               }
             }
       }
   print "${file}:\t",join( "\t",@procinfo),"\n" if @procinfo;
  }

exit $rc;
__END__

Have fun

/J\
-- 
Jonathan Stowe <jns@btinternet.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>


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

Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>


Administrivia:

Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.

If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu. 


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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 4126
**************************************

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