[24751] in Perl-Users-Digest
Perl-Users Digest, Issue: 6906 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 24 18:10:52 2004
Date: Tue, 24 Aug 2004 15:10:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 24 Aug 2004 Volume: 10 Number: 6906
Today's topics:
Simulating the open() command. (Chris Heller)
Re: Simulating the open() command. <tassilo.von.parseval@rwth-aachen.de>
Re: Simulating the open() command. (Greg Bacon)
Re: Simulating the open() command. (Walter Roberson)
Slide show: this should be fairly straightforward - a w <no-email@no-one.net>
Re: Slide show: this should be fairly straightforward - <1usa@llenroc.ude.invalid>
Re: Slide show: this should be fairly straightforward - <sbryce@scottbryce.com>
Re: split question <dwall@fastmail.fm>
Re: split question <noreply@gunnar.cc>
Re: split question <dwall@fastmail.fm>
Re: split question <someone@example.com>
Re: XY problem (was Re: editing perl script through TEX (anita)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 24 Aug 2004 09:03:00 -0700
From: tomaco@gmail.com (Chris Heller)
Subject: Simulating the open() command.
Message-Id: <f9630e2c.0408240803.3d114e04@posting.google.com>
Is it possible to write a subroutine in Perl that would act just like
a call to open()?
I have a project which currently uses NFS for file storage. This being
a web project, and NFS being what it is, the decision has been made to
remove dependancy on NFS. Of course this needs to be done in the
simplest possible manner, so I've devised a simple file-transfer
protocol and server and thought it would be great if I could simply
replace calls to open() and close() with myopen() and myclose().
Consider this:
sub myopen(*$$$){
my ($sh, $fileop, $ip, $port ) = @_;
socket($sh,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
...
print $sh "$fileop\n";
...
return 1;
}
myclose(*){
my $fh = shift;
...
close($fh);
}
The subroutines are declared using prototypes so that they will work
just like open() and close() do when called with no parens around
their arguments.
myopen() takes a bareword, which I wish to turn into a socket handle,
a string like ">/writetofile" just like open() does, and two
additional arguments an IP address and a port.
myclose() is the analog of close() but handles some extra bookkeeping
and shutsdown the socket if needed.
Now the trick is, I want myopen() to not have to explicitly return a
filehandle in the return statement, I want this to work like open().
So in my code you would see something like this:
myopen FH,">fileop","127.0.0.1",4556;
while(<FH>){
print $_;
}
myclose FH;
From what I've discovered in playing with this code is that the handle
that I create in myopen() does not exist once I return from the
subroutine.
So the question I am stumped with is how can I pass a bareword into a
sub routine and treat it like a reference so that and changes I make
to it in the subroutine will persist after I return from that
subroutine?
Regards,
C. Heller
------------------------------
Date: Tue, 24 Aug 2004 18:18:43 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: Simulating the open() command.
Message-Id: <2p1836Ffr4o9U1@uni-berlin.de>
Also sprach Chris Heller:
> Is it possible to write a subroutine in Perl that would act just like
> a call to open()?
Mostly, yes.
> I have a project which currently uses NFS for file storage. This being
> a web project, and NFS being what it is, the decision has been made to
> remove dependancy on NFS. Of course this needs to be done in the
> simplest possible manner, so I've devised a simple file-transfer
> protocol and server and thought it would be great if I could simply
> replace calls to open() and close() with myopen() and myclose().
>
> Consider this:
>
> sub myopen(*$$$){
> my ($sh, $fileop, $ip, $port ) = @_;
>
> socket($sh,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
> ...
> print $sh "$fileop\n";
> ...
> return 1;
> }
>
> myclose(*){
> my $fh = shift;
> ...
> close($fh);
> }
>
> The subroutines are declared using prototypes so that they will work
> just like open() and close() do when called with no parens around
> their arguments.
>
> myopen() takes a bareword, which I wish to turn into a socket handle,
> a string like ">/writetofile" just like open() does, and two
> additional arguments an IP address and a port.
[...]
> Now the trick is, I want myopen() to not have to explicitly return a
> filehandle in the return statement, I want this to work like open().
Using Symbol::qualify_to_ref() should work for you:
use Symbol;
...
sub myopen (*$$$) {
my ($sh, $fileop, $ip, $port) = @_;
my $ref = qualify_to_ref($sh);
socket $ref, ...;
...
}
> So in my code you would see something like this:
>
> myopen FH,">fileop","127.0.0.1",4556;
> while(<FH>){
> print $_;
> }
> myclose FH;
>
> From what I've discovered in playing with this code is that the handle
> that I create in myopen() does not exist once I return from the
> subroutine.
That is because $sh is a simple lexical variable. If you use such a
variable as a filehandle, it gets closed once it falls out of scope.
qualify_to_ref() on the other hand will turn it into a real
GLOB-reference.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Tue, 24 Aug 2004 17:03:34 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Simulating the open() command.
Message-Id: <10imt76fvjjqb7c@corp.supernews.com>
In article <f9630e2c.0408240803.3d114e04@posting.google.com>,
Chris Heller <tomaco@gmail.com> wrote:
: [...]
: So the question I am stumped with is how can I pass a bareword into a
: sub routine and treat it like a reference so that and changes I make
: to it in the subroutine will persist after I return from that
: subroutine?
Don't forget that you can scribble in other packages:
use IO::Socket::INET;
sub myopen(*$$$) {
my($fh,$fileop,$ip,$port) = @_;
no strict 'refs';
my($pkg) = caller;
my $sym = $pkg . "::" . $fh;
*$sym = IO::Socket::INET->new(
PeerAddr => "$ip:$port",
);
return unless *{$sym}{IO};
print { *$sym } "$fileop\n";
}
sub myclose(*) {
no strict 'refs';
my $fh = shift;
my($pkg) = caller;
my $sym = $pkg . "::" . $fh;
close *$sym;
}
Please keep in mind the following advice from the perlstyle manpage:
"Just because you CAN do something a particular way doesn't mean that
you SHOULD do it that way."
Hope this helps,
Greg
--
I write this on July 4, when we celebrate the ouster of a "tyrant" who
taxed his subjects at the rate of about three percent.
-- Doug Newman
------------------------------
Date: 24 Aug 2004 18:25:11 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: Simulating the open() command.
Message-Id: <cgg167$j0i$1@canopus.cc.umanitoba.ca>
In article <f9630e2c.0408240803.3d114e04@posting.google.com>,
Chris Heller <tomaco@gmail.com> wrote:
:Is it possible to write a subroutine in Perl that would act just like
:a call to open()?
Yes. In fact, instead of writing "myopen", you could override
open() itself within your package, with your open() calling
CORE::open() if you need to have the "real" open do something
for you.
:I have a project which currently uses NFS for file storage. This being
:a web project, and NFS being what it is, the decision has been made to
:remove dependancy on NFS. Of course this needs to be done in the
:simplest possible manner, so I've devised a simple file-transfer
:protocol and server and thought it would be great if I could simply
:replace calls to open() and close() with myopen() and myclose().
Consider using nget or curl instead of writing your own file
transport protocols. But it depends in part on whether your
application just needs to read the file, or needs to be able
to update it.
If it needs to be able to update the file, then consider looking at the
perltie documentation for information on how to tie a filehandle so
that behind the scenes you can substitute your own remote seek/read
seek/write routines.
--
Cottleston, Cottleston, Cottleston pie.
A bird can't whistle and neither can I. -- Pooh
------------------------------
Date: Tue, 24 Aug 2004 19:15:15 GMT
From: Al Davis <no-email@no-one.net>
Subject: Slide show: this should be fairly straightforward - a what language to use question
Message-Id: <ia5ni0pgnn2h1gkrgqig0mlkhhb64qkq4v@4ax.com>
Note: I tried cross-posting this message to several newsgoups,
including comp.lang.perl.misc, c.l.p.moderated,
comp.infosystems.www.authoring.cgi, comp.lang.javascript and
comp.lang.php. Nothing appeared on my news server, so I'm trying
again - this time posting a separate copy of the message to each
group.
I'm thinking this should be fairly easy to accomplish - a quick and
dirty ... what? ... script? program?
Background: I have a website - created using a html-generator
application called Dreamweaver. I have some limited knowledge of
html, and I sort of know what php does. I've read that Perl is cool,
powerful and free ... but beyond that ...???. I know next to nothing
about Javascript.
Here's the scenario:
o User brings up page with, say, 5 thumbnail images. The thumbnails
represent 5 sets of slides (jpegs), in, say, 5 separate subdirectories
living in my area of my website host's system. Each slide set
contains from 10 to 20 images, each with an associated caption. Jpegs
are named: s01.jpg, s02.jpg, s03.jpg, ... in each subdirectory (or
maybe they have unique names - but let's simplify). The slides are
different sizes, ranging from 400x300 to 1000x800 pixels
o Selecting a thumbnail (by clicking) from among the 5 takes the user
to a page where he sees the first slide and first caption associated
with that thumb. In additon to the jpeg and the caption, the screen
would contain some buttons that the user can click to go back a slide,
go forward a slide, or return home to thumbnail page. User can also
click on an image to enlarge it - meaning he would bring up the same
page, but it would show the file t01.jpg, which is, say, an un-cropped
version of s01.jpg. (Or perhaps, he would have specified at the home
page whether he wanted the high or the low bandwith version of the
slide show.)
o Subsequent clicking on the forward button brings up the subsequent
slides in sequence, returning to the first slide after the last is
displayed. Other than the different images and captions, the pages are
identical with regard to title, background, font, layout, etc.
I know how to accomplish this by the brute force method: For each of
the X number of images in a subdirectory, create X unique html
code-pages - slide1.htm, slide2.htm, etc. - that each reference a
particular image and particular caption, bringing up the next (or
previous) code-page in sequence when the user clicks to advance (or go
back). So for 5 sets of slides, each with 20 images, you'd have a
total of 100 unique .htm files. Actually, there would be 200 files,
given that there are a small and large version of each image.
Here's the more elegant approach:
One "routine" does everything. You ?pass? the name of the slide show
subdirectory to the routine as an ?argument?, and assign it to a
filename ?string variable? somehow. You might also pass the number of
slides in the subdirectory, unless there's a way for the routine to
determine what it is (like, by reading the number from another file,
or by "calling" some ?system function?, that reveals the number of
files in the directory.) The routine then loops through the list,
counting from 1 to X, and somehow opening the associated s0n.jpg file
(and associated caption text) to put up on the user's screen as he
clicks his way through the show.
This brings up a host (no pun intended) of basic questions:
o Where would this code or script or html actually run? On the site
host? It is downloaded to the user's machine? Is it intrepreted by
some entity - like the browser?
o What support is needed on the host side for something like this?
What about the user/client side? If it's just a browser he needs, will
any old browser do? (I do know that the host for my own website ...
catalog.com ... does support php. The support for it is free for the
sites they host.)
o Is this a question of "self-modifying" html? (I'm thinking that's
not possible.)
o What is the easiest way to get this done. Could you learn enough
php, perl, javascript, or whatever in, say, a day and a half, to write
this little routine together and get it working?
Thanks for your attention
-Al Gabis
Camp Springs, Maryland
www.SpiritualNeighborhood.org
------------------------------
Date: 24 Aug 2004 20:24:09 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Slide show: this should be fairly straightforward - a what language to use question
Message-Id: <Xns954FA6DCF1F93asu1cornelledu@132.236.56.8>
Al Davis <no-email@no-one.net> wrote in
news:ia5ni0pgnn2h1gkrgqig0mlkhhb64qkq4v@4ax.com:
> Note: I tried cross-posting this message to several newsgoups,
> including comp.lang.perl.misc, c.l.p.moderated,
> comp.infosystems.www.authoring.cgi, comp.lang.javascript and
> comp.lang.php. Nothing appeared on my news server, so I'm trying
> again - this time posting a separate copy of the message to each
> group.
Well, you should find the groups most appropriate group for your topic
and post there.
> I'm thinking this should be fairly easy to accomplish - a quick and
> dirty ... what? ... script? program?
>
> Background: I have a website - created using a html-generator
> application called Dreamweaver. I have some limited knowledge of
> html,
There is your first problem. You should really learn some HTML before
tackling any real programming language
> and I sort of know what php does. I've read that Perl is cool,
> powerful and free ... but beyond that ...???. I know next to nothing
> about Javascript.
Perl is a general purpose programming language that can be used to write
server side programs that can be used through CGI. For Perl resources,
checkout http://learn.perl.org/.
> Here's the scenario:
We do not write applications to specs here. You will need to show what
you have done and ask specific help.
For your purposes, I would recommend Perl with CGI.pm and HTML::Template.
My first Perl CGI script, which is not a work of art or anything, was
written so that I could drop a bunch of photos in a directory, and a
photo album page would be generated for that directory on the fly. You
can see it in action at:
http://www.unur.com/cgi-bin/photobrowser?loc=sinan/usa;tmpl=cols3;rnd=yes
and the source code is at:
http://www.unur.com/comp/photobrowser/index.html
<Long description snipped>
> o Where would this code or script or html actually run? On the site
> host? It is downloaded to the user's machine? Is it intrepreted by
> some entity - like the browser?
A CGI script running on the server would save you from dealing with all
sorts of javascript oddities.
> o What support is needed on the host side for something like this?
> What about the user/client side? If it's just a browser he needs, will
> any old browser do? (I do know that the host for my own website ...
> catalog.com ... does support php. The support for it is free for the
> sites they host.)
Any old browser will do so long as you send halfway reasonable HTML back
to the browser.
> o Is this a question of "self-modifying" html? (I'm thinking that's
> not possible.)
Perl's HTML::Template module is perfect for this.
http://search.cpan.org/~samtregar/HTML-Template-2.7/Template.pm
> o What is the easiest way to get this done. Could you learn enough
> php, perl, javascript, or whatever in, say, a day and a half, to write
> this little routine together and get it working?
Well, you would have to decide what you want to learn. However, if you do
not even know HTML, I doubt you could pull it off in a day.
Why not use something like
http://www.ricocheting.com/js/slide.html
Sinan.
------------------------------
Date: Tue, 24 Aug 2004 14:36:39 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Slide show: this should be fairly straightforward - a what language to use question
Message-Id: <10in9mgfgc44295@corp.supernews.com>
GGGGgggrrrrr!!!!
I spent a bunch of time writing a response. I was going to email it,
because this question is off topic here. Then I found out that the OPs
email address is munged.
OK, short answer, this question belongs in
comp.infosystems.www.authoring.cgi. I have more to tell you, but I don't
have your email address and my comments are of topic for this newsgroup.
------------------------------
Date: Tue, 24 Aug 2004 15:38:57 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: split question
Message-Id: <Xns954F7680D35ADdkwwashere@216.168.3.30>
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message
<news:2p100bFffgb9U1@uni-berlin.de>:
> Tore Aursand wrote:
>>
>> Untested:
>>
>> #!/usr/bin/perl
>> #
>> use strict;
>> use warnings;
>> use Data::Dumper;
>>
>> my $string = 'abc def ghi jkl';
>> my @parts = split( /\s+/, $string, 2 );
>>
>> print Dumper( \@parts );
>
> Why on earth do you use Data::Dumper to print a two elements array?
Habit, maybe? Or an editor customization to insert appropriate code?
I have a key programmed to insert "use strict;\nuse warnings;" so I
don't have to type the same two lines all the time.
My biggest problem with Data::Dumper is that half the time I type
Data::Dumperl and have to correct it. :-)
------------------------------
Date: Tue, 24 Aug 2004 17:40:36 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: split question
Message-Id: <2p15vsFfqojdU1@uni-berlin.de>
Tore Aursand wrote:
> Gunnar Hjalmarsson wrote:
>> Tore Aursand wrote:
>>>
>>> use Data::Dumper;
>>>
>>> my $string = 'abc def ghi jkl';
>>> my @parts = split( /\s+/, $string, 2 );
>>>
>>> print Dumper( \@parts );
>>
>> Why on earth do you use Data::Dumper to print a two elements
>> array?
>
> Why not? I always have Data::Dumper in my scripts (when I
> develop), and that way I don't have to care _ever_ what structure
> my data has.
Okay, that does make sense. With the important clarification "when I
develop", that is. ;-)
> It's one of my favourite modules, by the way. Along with CGI.pm,
> of course. :)
Yeah, yeah...
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 24 Aug 2004 15:56:57 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: split question
Message-Id: <Xns954F798E31CAAdkwwashere@216.168.3.30>
Rafal Konopka <rafalk@comcast.net> wrote in message
<news:OKOdnXxPSMH3zbbcRVn-rw@comcast.com>:
> "Mike Lepore" <lepor5e@bestweb.net> wrote in message
> news:10im66m6ks8qf3@corp.supernews.com...
>> Not sure whether this is a split question or a regex question
>> (newbie). Given a string variable which contains at least one
>> space character, how can I extract everything to the left of the
>> first space, and everything to the right of the first space?
>> The number of characters and fields can't be hardcoded.
>> Example:
>> $Variable = "abc def ghi jkl";
>> $Variable1 become "abc" and $Variable2 becomes "def ghi jkl"
>
> Yet another syntactic variant:
>
> ( $Variable1, $Variable2 ) = map {$`,$'} $Variable =~ /\s+/;
Eww. That's truly perverted. Not nearly as bad (or even preferred if
you're a radioactive lizard):
my $Variable = 'abc def ghi jkl';
my $position = index $Variable, ' ';
my $Variable1 = substr $Variable, 0, $position;
my $Variable2 = substr $Variable, $position + 1;
or maybe
my $Variable = 'abc def ghi jkl';
my ($Variable1, $Variable2) = $Variable =~ /^([^ ]+) (.*)/;
...but those are IMHO rather pointless; the three-argument form of
split() is the easiest and most obvious way to do it. Canonical,
even.
------------------------------
Date: Tue, 24 Aug 2004 21:28:22 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: split question
Message-Id: <WpOWc.3902$A8.3816@edtnps89>
Rafal Konopka wrote:
> "Mike Lepore" <lepor5e@bestweb.net> wrote in message
> news:10im66m6ks8qf3@corp.supernews.com...
>
>>Not sure whether this is a split question or a regex question (newbie).
>>Given a string variable which contains at least one space character,
>>how can I extract everything to the left of the first space,
>>and everything to the right of the first space?
>>The number of characters and fields can't be hardcoded.
>>Example:
>>$Variable = "abc def ghi jkl";
>>$Variable1 become "abc" and $Variable2 becomes "def ghi jkl"
>
> Yet another syntactic variant:
>
> ( $Variable1, $Variable2 ) = map {$`,$'} $Variable =~ /\s+/;
What if $Variable has leading whitespace?
John
--
use Perl;
program
fulfillment
------------------------------
Date: 24 Aug 2004 11:52:10 -0700
From: anita1766@yahoo.com (anita)
Subject: Re: XY problem (was Re: editing perl script through TEXTAREA)
Message-Id: <1a1fc02.0408241052.12906567@posting.google.com>
> An XY problem is when you want to do X, but you ask how to do Y
> instead, because you've decided that Y is the best way to accomplish X.
Ah.
>
> If you tell us why you think you want to edit programs in a browser,
I just want to be able to edit programs through a browser using plain
HTML, without writing plug-ins, or browser addons or mapping an
application through MIME types to edit my file...
> then maybe we could suggest a better or easier alternative...
The end in this case is the "how" not the end result itself. Or maybe
an explanation of why its not advisable to edit something through the
browser if I wanted to.
Thanks everybody for the different insights, in this thread.
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 6906
***************************************