[22598] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4819 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Apr 5 09:05:45 2003

Date: Sat, 5 Apr 2003 06:05:06 -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           Sat, 5 Apr 2003     Volume: 10 Number: 4819

Today's topics:
    Re: <script language=PerlScript> <founder@pege.org>
    Re: Bless equivalent in PHP? <bart.lateur@pandora.be>
    Re: Can Perl mimic the awk '/start/,/end/' syntax <goldbb2@earthlink.net>
        LWP post problem (Chacrint Charinthorn)
        NEWBIE best linux for perl? <"goodrow"@opencity. com>
    Re: NEWBIE best linux for perl? <me@privacy.net>
    Re: NEWBIE best linux for perl? <dorward@yahoo.com>
    Re: Newbie Needs Good Advice (Tad McClellan)
    Re: Newbie Needs Good Advice (Tad McClellan)
    Re: Newbie Needs Good Advice <nospam@spam.no>
    Re: Newbie Needs Good Advice <idont@thinkso.net>
    Re: Reformated Why won't this work. <jurgenex@hotmail.com>
    Re: Reformated Why won't this work. (Tad McClellan)
    Re: Reformated Why won't this work. (Tad McClellan)
    Re: regular expressions: newbie question <goldbb2@earthlink.net>
        Stubbing FileHandle.pm (Stephen Deken)
        Substitution won't match for me <daNOSPAM_beano@hotmail.com>
    Re: Substitution won't match for me <noreply@gunnar.cc>
    Re: Which is better - hashes or subroutines <tassilo.parseval@rwth-aachen.de>
    Re: Which is better - hashes or subroutines <goldbb2@earthlink.net>
    Re: Which is better - hashes or subroutines <tassilo.parseval@rwth-aachen.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 5 Apr 2003 07:24:28 +0200
From: =?iso-8859-1?Q?Roland_M=F6sl?= <founder@pege.org>
Subject: Re: <script language=PerlScript>
Message-Id: <3e8e6dd5$0$24818$91cee783@newsreader02.highway.telekom.at>

"Jürgen Exner" <jurgenex@hotmail.com> wrote in message
news:DUoja.8856$aQ3.7023@nwrddc02.gnilink.net...
> Roland Mösl wrote:
> > Who else uses Perl as scripting language
> > inside HTML pages?
> >
> > Is practicall for program development,
> > because the browser can be used as
> > the GUI
>
> However rather limited because (at least AFAIK) PerlScript is available
for
> IE only.

That's the only disadvantage.

But if enough users found, it should be possible to have this feature also
in Mozilla implemented.

I have set up an solution that depending on the start file,
the system works as <script language=PerlScript> on my Windows computer
and as Perl on my Linux / Apache server


--
Roland Mösl
http://www.pege.org Clear targets for a confused civilization
http://web-design-suite.com Web Design starts at the search engine



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

Date: Sat, 05 Apr 2003 09:27:17 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Bless equivalent in PHP?
Message-Id: <mt7t8vg9hb991f6ohegi68t1iqcev2knoc@4ax.com>

Eric Arnold wrote:

> Is there any sort of way to inline perl with
>something similar to PHP or ASP style tags?

I think you're really looking for a template mechanism.  Something like
EmbPerl does indeed embed perl into literal text (html) files. Or, for a
real template mechanism like Template-Toolkit, you separate the HTML and
the code into separate files. There are many templating mechanisms,
covering the whole spectrum. See, for a comparison, this article:

	<http://www.perl.com/pub/a/2001/08/21/templating.html?page=1>

and also chapter 1 of the Mason book, which is available online for free
on

	<http://www.masonbook.com/book/>

-- 
	Bart.


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

Date: Fri, 04 Apr 2003 22:42:58 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Can Perl mimic the awk '/start/,/end/' syntax
Message-Id: <3E8E50C2.D114E4A3@earthlink.net>

ctcgag@hotmail.com wrote:
> 
> Benjamin Goldberg <goldbb2@earthlink.net> wrote:
> > Demetrius wrote:
> > >
> > > What is the Perl way to do:
> > >
> > > cat myfile.txt | awk '/start/,/end/'
> >
> > UUOC: This could have been written as:
> >    awk '/start/,/end/' < myfile.txt
> > Without using 'cat'.
> 
> UUOLT:  You could write it as:
>       awk '/start/,/end/' myfile.txt
> 
> If course, it's not like there is a shortage of cats or redirections
> such that they require conservation.

However, running "cat" starts another process, whereas redirection (or
handing awk the filename on the commandline) does not.

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: 5 Apr 2003 02:53:36 -0800
From: chacrint@hotmail.com (Chacrint Charinthorn)
Subject: LWP post problem
Message-Id: <bfe227db.0304050253.559dd87b@posting.google.com>

I tried to post using LWP but I got the following error:

500 (Internal Server Error) Can't read entity body: Connection reset by peer

is it the problem with the problem I tried to post to?


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

Date: Sat, 05 Apr 2003 07:02:57 -0500
From: Jason Goodrow <"goodrow"@opencity. com>
Subject: NEWBIE best linux for perl?
Message-Id: <b6mg2c$9sh$1@reader2.panix.com>

Hey NG -

I intend to install linux on a box mainly to run perl.

What's the most perl/CPAN friendly distro?
I know beginner Unix but don't mind getting my hands dirty, and perl is
my top (only?) priority. This is for fun, not $

Thanks in adv

goodrow@panix.com



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

Date: Sat, 5 Apr 2003 22:04:46 +1000
From: "Tintin" <me@privacy.net>
Subject: Re: NEWBIE best linux for perl?
Message-Id: <b6mgos$6luvc$1@ID-172104.news.dfncis.de>


"Jason Goodrow" <"goodrow"@opencity. com> wrote in message
news:b6mg2c$9sh$1@reader2.panix.com...
> Hey NG -
>
> I intend to install linux on a box mainly to run perl.
>
> What's the most perl/CPAN friendly distro?
> I know beginner Unix but don't mind getting my hands dirty, and perl is
> my top (only?) priority. This is for fun, not $

Pretty all the Linux distributions are equally good for Perl.  Mostly comes
down to what features you want from Linux.




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

Date: Sat, 05 Apr 2003 14:12:12 +0100
From: David Dorward <dorward@yahoo.com>
Subject: Re: NEWBIE best linux for perl?
Message-Id: <b6mkns$d1g$1$8300dec7@news.demon.co.uk>

Jason Goodrow wrote:

> I intend to install linux on a box mainly to run perl.

You can run Perl under Windows. http://activestate.com/products/activeperl/
(Although I'd use Linux anyway [for other resons])

> What's the most perl/CPAN friendly distro?
> I know beginner Unix but don't mind getting my hands dirty, and perl is
> my top (only?) priority. This is for fun, not $

It really doesn't make much difference. Perl mature enough, and common
enough, to be fairly well standardised across all (non-task-specific)
distros.

-- 
David Dorward                                   http://david.us-lot.org/
"You cannot rewrite history, not one line."
                                      - The Doctor (Dr. Who: The Aztecs)


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

Date: Fri, 4 Apr 2003 21:44:08 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Newbie Needs Good Advice
Message-Id: <slrnb8sk88.4ql.tadmc@magna.augustmail.com>

James E Keenan <jkeen@concentric.net> wrote:
> "Mike" <flyby@webescape.com> wrote in message
> news:290e4b32.0304041558.94777b9@posting.google.com...

>> I'm considering trying to learn
>> programming myself. I'm a complete beginner to PERL.


> 2.  Learn Perl!  Here's a plug for the text I use for an intro to Perl
> course:  http://www.oreilly.com/catalog/lperl3/


Have you read the first section of the Preface?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 4 Apr 2003 21:40:42 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Newbie Needs Good Advice
Message-Id: <slrnb8sk1q.4ql.tadmc@magna.augustmail.com>

Mike <flyby@webescape.com> wrote:

> Subject: Newbie Needs Good Advice


Here's some:

   Don't include "newbie" in your Subject.

   Please read the Posting Guidelines that are posted here frequently.


> I must first download a half dozen or so data files 
> from two different webpages. 


You cannot download from webpages.

You can download from web servers though.

You would use the LWP module for that:

   http://search.cpan.org/author/GAAS/libwww-perl-5.69/lib/LWP.pm


> Both pages require username/password 
> logins when a link is clicked to get a file manually. 


The LWP module can handle authentication for you.


> Also, the 
> filenames that I need from those webpages change daily (by date) for 
> example today being April 4th I might need ABC0405.zip, DEF0405.zip 
> where 0405 is tomorrow's date as part of the filename. In other words 
> I need "ABC" and "DEF" files every day, but the 0405.zip part will be 
> 0406.zip tomorrow.


Use the localtime() and sprintf() functions to build up the expected
filename:

   perldoc -f localtime

   perldoc -f sprintf


> so after much frustration I'm considering trying to learn 
> programming myself. 


What you want to do is much much too hard as a first 
programming project.

You basically want to enter a race when you don't even know
how to drive on surface streets yet.

If you have a few months, then you could eventually write
what you want in Perl.

If you don't have a few months, hire someone who already knows
Perl to write it for you.


> I'm a complete beginner to PERL. 


The name of the language is "Perl", not "PERL".


> My question to 
> you all is, should I even bother to learn PERL for this task 


Yes.


> or is a 
> VB6 application (or other language) more suited for what I need. 


That depends on if Bill's brainwashing has worked on you or not...


> Also, I keep seeing all of these programming languages like VB6, PERL, 
> ASP, VBScript, Python, C++, etc. etc. 


ASP is not a programming language.


> but to be honest I have no clue 
> what's best suited to my needs. 


Me either. Only you can truly assess your needs.

But I can say the Perl can do all of that.

If you already knew Perl, it could be written in much less than one day.


> As far as my programming experience 
> goes, I did write some decent little programs in BASIC about 15-20 
> years ago but that's it, nothing recent. Your advice is greatly 
> appreciated. Please point me in the right direction.


The usual Perl tutorial book recommendation for non-programmers is:

   "Elements of Programming with Perl" by Andrew Johnson


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 05 Apr 2003 03:08:26 -0600
From: rd <nospam@spam.no>
Subject: Re: Newbie Needs Good Advice
Message-Id: <3E8E9D0A.B25AD917@spam.no>

Mike wrote:
> 
> Hello, as part of a job which requires me to generate PDF files for
> upload to the web, I must first download a half dozen or so data files
> from two different webpages. Both pages require username/password
> logins when a link is clicked to get a file manually. Also, the
> filenames that I need from those webpages change daily (by date) for
> example today being April 4th I might need ABC0405.zip, DEF0405.zip
> where 0405 is tomorrow's date as part of the filename. In other words
> I need "ABC" and "DEF" files every day, but the 0405.zip part will be
> 0406.zip tomorrow. The files also become available throughout the day
> at somewhat random intervals which makes it especially tedious because
> I have to manually keep checking the pages for updates for hours on
> end.
> 
> Now, ideally what I'd like to have is either a script of some kind, or
> a custom application that will automate the whole download/upload
> process for me. I've yet to find any retail application that fits the
> bill, so after much frustration I'm considering trying to learn
> programming myself. I'm a complete beginner to PERL. My question to
> you all is, should I even bother to learn PERL for this task or is a
> VB6 application (or other language) more suited for what I need.
> Thinking I had a "solution", last week I purchased a VBScript
> reference book by Wrox press and after the 4th chapter I became
> completely lost with it. I see no way of downloading the required
> files using standalone VBScript, at least as this book presents it.
> Also, I keep seeing all of these programming languages like VB6, PERL,
> ASP, VBScript, Python, C++, etc. etc. but to be honest I have no clue
> what's best suited to my needs. As far as my programming experience
> goes, I did write some decent little programs in BASIC about 15-20
> years ago but that's it, nothing recent. Your advice is greatly
> appreciated. Please point me in the right direction.

You can write a Perl script that will do all of this.  It might be
easier to use a shell script though.  For example, do a bash script
that would use wget to download all the datafiles (easy since you know
the names and the websites).  Then use Perl (or perhaps awk) to format
data into a useful format and save it to a file.  Finish with pdflatex
to make the PDF files.  If you haven't got access to anything but a
Windows machine, Cygwin is capable of all of this if you install the
right packages.

For wget, look at the --http-user and --http-passwd options.  Come up
with a tex template to insert the data into, then use Perl or awk to
put the data into the template.  Finish with pdflatex to turn the tex
files into PDFs.  This approach would probably be quicker to implement,
and it would involve learning less.


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

Date: Sat, 5 Apr 2003 07:20:28 -0500
From: "Chris W" <idont@thinkso.net>
Subject: Re: Newbie Needs Good Advice
Message-Id: <f0CdnTRBCcx-VBOjXTWc2Q@comcast.com>


"James E Keenan" <jkeen@concentric.net> wrote in message
news:b6l8da$fls@dispatch.concentric.net...
> 1.  On this list, please put a relevant description in the Subject line of
> your posting.  Many readers will immediately pass over one with your
> subject.
> 2.  Learn Perl!  Here's a plug for the text I use for an intro to Perl
> course:  http://www.oreilly.com/catalog/lperl3/


#1 - Agreed!

#2 - my real reason for replying.. Great book!  I learned a lot that other
books don't teach to beginners, and in logical, easy to understand steps.
Contrast this with Perl by Example, for example ;-), which was a struggle to
read, and I kept having to forward reference to understand the current
topic!  (In it's defense, it starts out teaching Perl from the command line
(i.e. perl -ne 'print if /me too/' sumfile.txt), and I couldn't do that
until I found Active State Perl for (yes, I know, ugh!) Windows.

I ended up buying Programming Perl, and then found The Perl CD Bookshelf on
sale for a Really Good Price.  Now I'm waiting for the extra cash to get CGI
Programming, (all of these are in the O'Reilly series, if anyone isn't
familiar..), and I'll be set!




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

Date: Sat, 05 Apr 2003 02:07:11 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Reformated Why won't this work.
Message-Id: <jTqja.2079$kr2.1886@nwrddc04.gnilink.net>

Josh Morrison wrote:
> Http 5000 Internal Server Error

perldoc -q 500





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

Date: Fri, 4 Apr 2003 21:31:10 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Reformated Why won't this work.
Message-Id: <slrnb8sjfu.4ql.tadmc@magna.augustmail.com>

Josh Morrison <Verbalx2@hotmail.com> wrote:

> I offer my apologies, 


Too late (in my case anyway).


> my rush 


Has hurt your chances of getting help with every Perl question
that you have from now on.

Rushing instead of Doing It Right was a suboptimal decision...


> and frustration with this script did get the
> better of me.
> 
> 
> #!/usr/local/bin/perl
> # weather.pl
> use strict;
> 
> # Define Variables
> 
> #What's the path to the 5-day accuweather data file?
> $five_data="5day.txt";


You should run the program again after adding "use strict",
and fix any error messages that it generates.


> # LET'S ROLL
> ####################################################################
> "et_time;


syntax error.


> if (uc($show) eq "CURRENT"){
>  $count=0;
> 
>   &open_file("CURRENT","",$current_data);
    ^
    ^

You don't want that ampersand. See:

   perldoc perlsub


>  $station=$tabledata[0];
>     $sky = $tabledata[1];
>     $current_temp = $tabledata[2];
>     $feel_temp = $tabledata[3];
>     $humidity= $tabledata[4];
>     $wind_dir = $tabledata[5];
>     $wind_speed= $tabledata[6];
>     $pressure = $tabledata[7];
>     $visibility = $tabledata[8];
>     $icon = $tabledata[9];
>     $redundant = $tabledata[10];


Consider using one hash containing 11 elements instead of
11 individual scalars.

   my %td;
   @td{ qw/ station sky current_temp feel_temp humidity wind_dir 
            wind_speed pressure visibility icon redundant / } = @tabledata;

>     chop($tabledata[10]);


You should use chomp() instead.

chop() was how it was done 8 years ago.



>    while(<OUTPUT>)


"OUTPUT" is an awfully strange name to use for an *input* filehandle.

Are you _trying_ to confuse yourself?


>  $time2=localtime($time);
>  ($wday, $month, $day, $time, $year) = split(" ",$time2,5);
>  $year = substr($year,2,2);


If you want only the last 2 digits of the year, then you should
get the last 2 digits of the year:

   $year = substr($year+1900, -2);


>  $month = uc($month);
>  if ($month eq "DEC"){
>   $month="12";}
>  elsif ($month eq "NOV"){
>   $month="11";}
>  elsif ($month eq "OCT"){
>   $month="10";}
>  elsif ($month eq "SEP"){
>   $month="9";}
>  elsif ($month eq "AUG"){
>   $month="8";}
>  elsif ($month eq "JUL"){
>   $month="7";}
>  elsif ($month eq "JUN"){
>   $month="6";}
>  elsif ($month eq "MAY"){
>   $month="5";}
>  elsif ($month eq "APR"){
>   $month="4";}
>  elsif ($month eq "MAR"){
>   $month="3";}
>  elsif ($month eq "FEB"){
>   $month="2";}
>    elsif ($month eq "JAN"){
>   $month="1";}


That would be much simpler if you used a hash instead:

   my %months = (
      DEC  =>  12,
      NOV  =>  11,
      OCT  =>  10,
      # ...
   );

   $month = $months{ uc $month };


> sub read_file
> {
>  local($file_var) = @_;


You should use my() instead of local() whenever possible.

It is possible here.

Using local() was how it was done 8 years ago.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 4 Apr 2003 21:14:34 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Reformated Why won't this work.
Message-Id: <slrnb8sigq.4ql.tadmc@magna.augustmail.com>

Josh Morrison <Verbalx2@hotmail.com> wrote:

> Http 5000 Internal Server Error


What about it?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 04 Apr 2003 22:34:02 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: regular expressions: newbie question
Message-Id: <3E8E4EAA.AD9F069A@earthlink.net>

MJL wrote:
> 
> if I have a sentence:
> cats like to eat cat food with other cats.
> and I want to change it to:
> dogs like to eat dog food with other dogs.
> 
> why can't I do this?
> 
> while ((s/cat/dog/) );

Read perldoc perlsyn.  You will see that "while" can be used in one of
two ways:

   while ( EXPR ) BLOCK
Or:
   STATEMENT while EXPR;

So, for your code to work, you need to change it to either:

   while( s/cat/dog/ ) {}

Or:

   1 while s/cat/dog/;

Of course, it would be significantly faster to simply write:

   s/cat/dog/g;

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: 4 Apr 2003 22:34:43 -0800
From: google@awdang.com (Stephen Deken)
Subject: Stubbing FileHandle.pm
Message-Id: <d6ccc926.0304042234.16a90fd1@posting.google.com>

O Font of Perl Wisdom,

I have a collection of modules that makes extensive use of
FileHandle.pm.  I'm in the process of embedding these modules into
another application written in C, by linking in Perl with my C
application, futzing with @INC, calling Perl_require_pv(), all that
happy jazz outlined more or less in perlembed.1.  I'm doing this
primarily so that I don't have to reimplement my code in C, and then
maintain both copies (C for client-side, Perl for server side with
Apache/mod_perl).

I'm trying to keep my application as lean and as free of cruft as
possible (while still including Perl :), but while it's a simple
measure to put strict.pm someplace in my limited @INC, I ran into huge
problems with FileHandle.pm, which apparently is indirectly dependent
on every other perl module that has been written, ever, since the dawn
of time, and some of those modules need DynaLoader, which kind of
negates the 'simple' aspect of what I'm trying to accomplish.

So what I did was create a stub FileHandle.pm that looks like this:

     package FileHandle;
     use Symbol;
     sub new { return gensym; }
     1;

Although actually I'll probably just copy the innards of gensym from
Symbol and eliminate that dependency as well.  This 'works for me'
because all I ever do with FileHandle is this:

     my $fh = new FileHandle;
     open( $fh, $filename );
     while (<$fh>) { ... }
     seek( $fh, 0, 0 );
     close( $fh );

In other words, I don't really care that it's a FileHandle, I just
want it to be a generic symbol.  Is there anything intrisically wrong
with doing this, or am I really just chopping out cruft that I don't
need?

Am I setting myself up for a world of hurt, but I'm just too stupid to
realize it?

Thanks for any help you can give me.

--sjd;


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

Date: Sat, 5 Apr 2003 13:32:07 -0000
From: "Steve Ferson" <daNOSPAM_beano@hotmail.com>
Subject: Substitution won't match for me
Message-Id: <m1Aja.173$Kc3.26@newsfep3-gui.server.ntli.net>

Just curious as to why this bit of code doesn't work. I've set myself up a
text file for holding messages of a message board.  It's really simple but
it's just for playing with.

What  I want to do is convert all the newlines between <message> and
</message> tags into <br/> tags for output as HTML.  I can't figure out why
the following substitution isn't working:

$input =~ s/(<message>.*)\n(.*<\/message>)/$1<br\/>$2/gsx;

can anyone help me?  It doesn't seem to be matching anything at all, the
code is output to HTML as a big long string. There are newlines in the
source but not <br/> tags.

Thanks,
Steve




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

Date: Sat, 05 Apr 2003 15:44:28 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Substitution won't match for me
Message-Id: <b6mn7v$6t317$1@ID-184292.news.dfncis.de>

Steve Ferson wrote:
> Just curious as to why this bit of code doesn't work. I've set
> myself up a text file for holding messages of a message board.
> It's really simple but it's just for playing with.
> 
> What  I want to do is convert all the newlines between <message>
> and </message> tags into <br/> tags for output as HTML.  I can't
> figure out why the following substitution isn't working:
> 
> $input =~ s/(<message>.*)\n(.*<\/message>)/$1<br\/>$2/gsx;

Can't tell why it doesn't match at all, but even if it had matched, I
think that only one occurrence of \n between <message> and </message>
would have been replaced.

This should do what you want:

     $input =~ s[<message>(.*?)<\/message>][
         (my $msg = $1) =~ s/\n/<br\/>/g;
         "<message>$msg</message>";
     ]egs;

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: 5 Apr 2003 04:27:35 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Which is better - hashes or subroutines
Message-Id: <b6llvn$m5i$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Benjamin Goldberg:

> "Tassilo v. Parseval" wrote:
> [snip]
>> Perhaps. When I wrote it I was more concerned with letting perl inline
>> the above. The shift() should make this impossible. I am no longer sure
>> whether mine can be inlined, but this should allow it:
>> 
>>     *$AUTOLOAD = sub () { "$self->{$var}" };
> 
> Sorry, that wouldn't work either, for getting perl to inline it.  You'd
> need to do:
> 
>    my $thevar = $self->{$var};
>    *$AUTOLOAD = sub () { $thevar };

That puzzles me. I would expect that '"$self->{$var}"' is a simple
string and can therefore be inlined. Hmmh, playing around a little with
B::Terse suggests that neither your nor my subroutine gets inlined. With
this code:

    my $self = bless { foo => "bar" } => "foobar";
    my $var = $self->{var};

    sub test  () { "$self->{var}" }
    sub test1 () { $var }
    sub test2 () { "foobar" }
    sub test3 () { eval '"$self->{var}"' }

    print test;
    print test1;
    print test2;
    print test3;

I get:

    [...]
    LISTOP (0x812d3d8) print
        OP (0x812d400) pushmark
        UNOP (0x812d380) entersub [4]
            UNOP (0x812d338) null [141]
                OP (0x812d360) pushmark
                UNOP (0x8182cd8) null [17]
                    SVOP (0x812d200) gv  GV (0x81848a4) *test
    COP (0x81b70d8) nextstate
    LISTOP (0x81b7090) print
        OP (0x81b70b8) pushmark
        UNOP (0x81b7070) entersub [5]
            UNOP (0x81b7028) null [141]
                OP (0x81b7050) pushmark
                UNOP (0x81b7008) null [17]
                    SVOP (0x812d420) gv  GV (0x8184904) *test1
    COP (0x81b7180) nextstate
    LISTOP (0x81b7138) print
        OP (0x81b7160) pushmark
        SVOP (0x81b7118) const  PV (0x8184964) "foobar"
    COP (0x81b72b0) nextstate
    LISTOP (0x81b7268) print
        OP (0x81b7290) pushmark
        UNOP (0x81b7248) entersub [6]
            UNOP (0x81b7200) null [141]
                OP (0x81b7228) pushmark
                UNOP (0x81b71e0) null [17]
                    SVOP (0x81b71c0) gv  GV (0x8184988) *test3

If I read the above correctly, only test2() has been turned into a real
constant.

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: Sat, 05 Apr 2003 00:16:06 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Which is better - hashes or subroutines
Message-Id: <3E8E6696.F413B471@earthlink.net>



"Tassilo v. Parseval" wrote:
> 
> Also sprach Benjamin Goldberg:
> 
> > "Tassilo v. Parseval" wrote:
> > [snip]
> >> Perhaps. When I wrote it I was more concerned with letting perl inline
> >> the above. The shift() should make this impossible. I am no longer sure
> >> whether mine can be inlined, but this should allow it:
> >>
> >>     *$AUTOLOAD = sub () { "$self->{$var}" };
> >
> > Sorry, that wouldn't work either, for getting perl to inline it.  You'd
> > need to do:
> >
> >    my $thevar = $self->{$var};
> >    *$AUTOLOAD = sub () { $thevar };
> 
> That puzzles me. I would expect that '"$self->{$var}"' is a simple
> string and can therefore be inlined. Hmmh, playing around a little with
> B::Terse suggests that neither your nor my subroutine gets inlined. With
> this code:
> 
>     my $self = bless { foo => "bar" } => "foobar";
>     my $var = $self->{var};
> 
>     sub test  () { "$self->{var}" }
>     sub test1 () { $var }
>     sub test2 () { "foobar" }
>     sub test3 () { eval '"$self->{var}"' }
> 
>     print test;
>     print test1;
>     print test2;
>     print test3;
>
> I get:
> 
>     [...]
>     LISTOP (0x812d3d8) print
>         OP (0x812d400) pushmark
>         UNOP (0x812d380) entersub [4]
>             UNOP (0x812d338) null [141]
>                 OP (0x812d360) pushmark
>                 UNOP (0x8182cd8) null [17]
>                     SVOP (0x812d200) gv  GV (0x81848a4) *test
>     COP (0x81b70d8) nextstate
>     LISTOP (0x81b7090) print
>         OP (0x81b70b8) pushmark
>         UNOP (0x81b7070) entersub [5]
>             UNOP (0x81b7028) null [141]
>                 OP (0x81b7050) pushmark
>                 UNOP (0x81b7008) null [17]
>                     SVOP (0x812d420) gv  GV (0x8184904) *test1
>     COP (0x81b7180) nextstate
>     LISTOP (0x81b7138) print
>         OP (0x81b7160) pushmark
>         SVOP (0x81b7118) const  PV (0x8184964) "foobar"
>     COP (0x81b72b0) nextstate
>     LISTOP (0x81b7268) print
>         OP (0x81b7290) pushmark
>         UNOP (0x81b7248) entersub [6]
>             UNOP (0x81b7200) null [141]
>                 OP (0x81b7228) pushmark
>                 UNOP (0x81b71e0) null [17]
>                     SVOP (0x81b71c0) gv  GV (0x8184988) *test3
> 
> If I read the above correctly, only test2() has been turned into a real
> constant.

My code, with the test4, produces:

perl -MO=Terse -
   use constant test1 => "foobar";
   sub test2 () { "foobar" }
   BEGIN { my $var = "foobar"; *test3 = sub () { $var } }
   print test1; print test2; print test3;
__END__
LISTOP (0x1784ba0) leave [1]
    OP (0x179446c) enter
    COP (0x1786ec4) nextstate
    LISTOP (0x1786f30) print
        OP (0x1786f10) pushmark
        SVOP (0x17863f8) const  SPECIAL #0 Nullsv
    COP (0x1c9b0d4) nextstate
    LISTOP (0x1786e9c) print
        OP (0x1786e7c) pushmark
        SVOP (0x1ca1d4c) const  SPECIAL #0 Nullsv
    COP (0x1772edc) nextstate
    LISTOP (0x1781428) print
        OP (0x1cbeee8) pushmark
        SVOP (0x1cbef70) const  SPECIAL #0 Nullsv
- syntax OK

Note that $var has gone out of scope by the time that the print
expression is compiled.

The "const  SPECIAL #0", instead of a "const PV (...) "foobar"" is
because I'm running a perl compiled with threads enabled.

If I use Deparse instead of Terse, I get:

sub test1 () {
    package constant;
    $scalar;
}
sub test2 () {
    'foobar';
}
sub test3 () {
    $var;
}
print 'foobar';
print 'foobar';
print 'foobar';
- syntax OK

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: 5 Apr 2003 07:28:43 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Which is better - hashes or subroutines
Message-Id: <b6m0jb$pui$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Benjamin Goldberg:

> "Tassilo v. Parseval" wrote:

>> That puzzles me. I would expect that '"$self->{$var}"' is a simple
>> string and can therefore be inlined. Hmmh, playing around a little with
>> B::Terse suggests that neither your nor my subroutine gets inlined. With
>> this code:
>> 
>>     my $self = bless { foo => "bar" } => "foobar";
>>     my $var = $self->{var};
>> 
>>     sub test  () { "$self->{var}" }
>>     sub test1 () { $var }
>>     sub test2 () { "foobar" }
>>     sub test3 () { eval '"$self->{var}"' }
>> 
>>     print test;
>>     print test1;
>>     print test2;
>>     print test3;
>>
>> I get:
>> 
>>     [...]
>>     LISTOP (0x812d3d8) print
[...]
>>                     SVOP (0x812d200) gv  GV (0x81848a4) *test
>>     COP (0x81b70d8) nextstate
>>     LISTOP (0x81b7090) print
[...]
>>                     SVOP (0x812d420) gv  GV (0x8184904) *test1
>>     COP (0x81b7180) nextstate
>>     LISTOP (0x81b7138) print
>>         OP (0x81b7160) pushmark
>>         SVOP (0x81b7118) const  PV (0x8184964) "foobar"
>>     COP (0x81b72b0) nextstate
>>     LISTOP (0x81b7268) print
[...]
>>                     SVOP (0x81b71c0) gv  GV (0x8184988) *test3
>> 
>> If I read the above correctly, only test2() has been turned into a real
>> constant.
> 
> My code, with the test4, produces:
> 
> perl -MO=Terse -
>    use constant test1 => "foobar";
>    sub test2 () { "foobar" }
>    BEGIN { my $var = "foobar"; *test3 = sub () { $var } }
>    print test1; print test2; print test3;
> __END__
> LISTOP (0x1784ba0) leave [1]
>     OP (0x179446c) enter
>     COP (0x1786ec4) nextstate
>     LISTOP (0x1786f30) print
>         OP (0x1786f10) pushmark
>         SVOP (0x17863f8) const  SPECIAL #0 Nullsv
>     COP (0x1c9b0d4) nextstate
>     LISTOP (0x1786e9c) print
>         OP (0x1786e7c) pushmark
>         SVOP (0x1ca1d4c) const  SPECIAL #0 Nullsv
>     COP (0x1772edc) nextstate
>     LISTOP (0x1781428) print
>         OP (0x1cbeee8) pushmark
>         SVOP (0x1cbef70) const  SPECIAL #0 Nullsv
> - syntax OK
> 
> Note that $var has gone out of scope by the time that the print
> expression is compiled.

Yes, indeed. However, the above also indicates that one needs to force
such a construct into compile-time to have perl inline it. That should
mean that we loose when it comes to AUTOLOAD because we can't make that
happen at compile-time.

Probably not a big deal since the OP talked about configuration
variables. I don't think you ever retrieve such variables in a tight
loop where inlining could matter. But at least it shows that my anxiety
to optimize it (and therefore not making it work per-object) was rather
futile.

> The "const  SPECIAL #0", instead of a "const PV (...) "foobar"" is
> because I'm running a perl compiled with threads enabled.
> 
> If I use Deparse instead of Terse, I get:

[...]

I didn't know that B::Deparse can also show handle subtleties. Good,
Deparse is definitely more friendly on the eyes than B::Terse.

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: 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 4819
***************************************


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