[17737] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5157 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 20 06:10:30 2000

Date: Wed, 20 Dec 2000 03:10:15 -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: <977310614-v9-i5157@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 20 Dec 2000     Volume: 9 Number: 5157

Today's topics:
    Re: possibly st00pid question regarding 'require' <bart.lateur@skynet.be>
    Re: Splitting record string into fields <bart.lateur@skynet.be>
    Re: Splitting text, but not individual words <Jerome.Abela@free.fr>
    Re: testing if $_ is equal to some string? <johngros@Spam.bigpond.net.au>
        Understanding interpolation <johnlin@chttl.com.tw>
    Re: Understanding interpolation (Rafael Garcia-Suarez)
    Re: Understanding interpolation <timallen449@coldmail.com>
    Re: Understanding interpolation <timallen449@coldmail.com>
        Unreserved apologies appsman1368@my-deja.com
    Re: what is perldoc? (Helgi Briem)
    Re: what's more efficient in turning an array into a li (Martien Verbruggen)
    Re: Wich module for download via http ? (Rafael Garcia-Suarez)
    Re: yet another question: is ' more efficient than "? <dennis_marti@yahoo.com>
    Re: yet another question: is ' more efficient than "? <Jerome.Abela@free.fr>
    Re: yet another question: is ' more efficient than "? (Eric Bohlman)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 20 Dec 2000 10:20:52 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: possibly st00pid question regarding 'require'
Message-Id: <m5114t4esaldg8qi93b2hq2fcoasbs9m8s@4ax.com>

The WebDragon wrote:

>so what you're saying is, I should pass the subroutine a *reference* to 
>the \%master_maps_list hash along with the file name to import from in 
>order to populate it, instead of aquiring the populated hash FROM the 
>other package.. 
>
>correct? 

The other way of getting your data, is populating a local hash, and
returning this through the func. Instead of

>|    read_and_load( 'filename', \%BoH );
 
this can be

	%BoH = read_and_load('filename');

with the disadvantage of time waste, and more importantly if this is a
big hash: a memory waste, because this requires flattening the original
hash to a list, and then populating an identical, second list. So, what
I sometimes do, is 

	$BoH = read_and_load('filename');

with

	sub read_and_load {
	    my %hash;
	    ...
	    return wantarray?%hash:\%hash;
	}

so depending on the context, you either get a flattened list version of
the hash, or a hash reference. In the latter case, you don't
unnecessarily copy the data, but access to the data in your main script
hash to go through the hassle of using the hash reference.

With a global hash, you can do

	*BoH = read_and_load('filename');

which will make %BoH an alias to the lexical %hash of within the sub.

I dont like Tad's original proposal of passing a reference of the "hash
to populate". That is too much like old Pascal's "pass the structure to
fill", which was its only way to "return" a structure as a function
value. I hate unnecessarily mucking with function parameters.

So: it's a compromise. None of the 3 solutions proposed here is THE
answer to all of your problems. I once thought of, and brought up the
syntax

	my \%BoH = read_and_load('filename');

after which you can simply access data through a access to the normal
lexical hash %BoH. It's would be a neater sibling of the "assign to
typeglob" idea. That would be rather nice. Since then, Larry even
occasionally mentioned this syntax (it could just be parallel
evolution), so one can only hope that this eventually may make it into
Perl 6, or maybe even in some future version of Perl 5.

-- 
	Bart.


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

Date: Wed, 20 Dec 2000 11:02:23 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Splitting record string into fields
Message-Id: <8c414tgq6jo22vmk7v67gr2g7bsr110c27@4ax.com>

appsman1368@my-deja.com wrote:

>I really don't have the time to learn perl at the
>moment

Then this newsgroup is not for you.

I'm pretty sure MS Access, and even MS Excel, are perfectly capable of
importing wixed width records, in a wizard-like fashion.

-- 
	Bart.


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

Date: Wed, 20 Dec 2000 09:47:29 GMT
From: Jerome Abela <Jerome.Abela@free.fr>
Subject: Re: Splitting text, but not individual words
Message-Id: <3A407F63.4CE8BF76@free.fr>

Josef Moellers a écrit :
> Dave T wrote:
> > I have a variable ($text) with a lot of text in it, and I want to split it
> > into lines of at most 80 characters, but not splitting the individual words.
> 
> $first=substr($text, 0, 81);
> ($prefix = $first) =~ s/(.*)\s\S*/$1/;
> $len = = length($prefix);
> substr($text, 0, $len+1, "");

If you are not willing to use Text::Wrap, here is a single-line,
pattern-only
solution:

    s/(.{0,80})( |)/\1\n/g;


Jerome.


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

Date: Wed, 20 Dec 2000 11:08:44 GMT
From: "John Boy Walton" <johngros@Spam.bigpond.net.au>
Subject: Re: testing if $_ is equal to some string?
Message-Id: <0r006.26419$xW4.204772@news-server.bigpond.net.au>


> if($_ eq "xxx\n"){ # Foo! }
or if ( /xxx\\n/ )




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

Date: Wed, 20 Dec 2000 10:13:50 +0800
From: "John Lin" <johnlin@chttl.com.tw>
Subject: Understanding interpolation
Message-Id: <91p4p6$nqq@netnews.hinet.net>

Dear all,

I saw this string interpolation and tried to understand it.

my $row = 1;
print <<EOF;
    <TD BGCOLOR = "${\($row%2? 'Green': 'Yellow')}">
EOF

__END__
    <TD BGCOLOR = "Green">

Hmm...  What does it do?   I tried to analyze by substitution.

my $row = 1;
my $arrayref = ['Green'];  # substitute \($row%2? 'Green': 'Yellow')
print <<EOF;
    <TD BGCOLOR = "${$arrayref}">
EOF

Buzz... wrong.  I got error message:

Not a SCALAR reference at line 3.

Anything wrong with my logic?

Thank you very much.

John Lin






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

Date: Wed, 20 Dec 2000 10:01:08 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Understanding interpolation
Message-Id: <slrn9410qf.nt3.rgarciasuarez@rafael.kazibao.net>

John Lin wrote in comp.lang.perl.misc:
> 
> I saw this string interpolation and tried to understand it.
> 
> my $row = 1;
> print <<EOF;
>     <TD BGCOLOR = "${\($row%2? 'Green': 'Yellow')}">
> EOF
> 
> Hmm...  What does it do?   I tried to analyze by substitution.
> 
> my $row = 1;
> my $arrayref = ['Green'];  # substitute \($row%2? 'Green': 'Yellow')

The original code doesn't use an array reference, but a scalar
reference. The parenthesis are here for grouping.

  my $ref = \'Green';  # substitute \($row%2? 'Green': 'Yellow')
  print <<EOF;
      <TD BGCOLOR = "$$ref">
  EOF


-- 
# Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
print map substr($_,7).&$_, grep defined &$_, sort values %::;
sub Just {" another "}; sub Perl {" hacker,\n"};


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

Date: Wed, 20 Dec 2000 11:37:35 +0100
From: "tim allen" <timallen449@coldmail.com>
Subject: Re: Understanding interpolation
Message-Id: <91q1ho$6a5$1@diana.bcn.ttd.net>

"John Lin" <johnlin@chttl.com.tw> wrote in message
news:91p4p6$nqq@netnews.hinet.net...
>I saw this string interpolation and tried to understand it.
>my $row = 1;
>print <<EOF;
>    <TD BGCOLOR = "${\($row%2? 'Green': 'Yellow')}">
>EOF
>__END__
>    <TD BGCOLOR = "Green">

How cool!  I was looking for something like this earlier.  This code is for
alternating the colors of cells in an HTML table.  What is happening (if I
understand correctly) is this:

in this bit: "${\($row%2? 'Green': 'Yellow')}"
1) the $ tells print to print the value of a scalar...
2) but the scalar isn't a declared variable (like "my $foo")...
3) ...it's a reference, created by the "\".  This makes a reference to the
following scalar...
4) but that's not a declared variable, either.  Instead...
5) it's the temporary variable returned by ($row%2? 'Green': 'Yellow')

or read from the inside out:
1) ($row%2? 'Green': 'Yellow') returns a temporary scalar with 'Green' or
'Yellow' in it
2) "\" makes a reference to this temporary variable
3) ${...} dereferences that reference
4) and print, um, prints it!

I hope that's right... even more than that, I hope that helps you!  Take
care, and bon nadal. -tim
--
unmunge?cold=hot







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

Date: Wed, 20 Dec 2000 11:53:28 +0100
From: "tim allen" <timallen449@coldmail.com>
Subject: Re: Understanding interpolation
Message-Id: <91q2ec$7ms$1@diana.bcn.ttd.net>

someone wrote in message
news:91p4p6$nqq@netnews.hinet.net...
>I saw this string interpolation and tried to understand it.
>my $row = 1;
>print <<EOF;
>    <TD BGCOLOR = "${\($row%2? 'Green': 'Yellow')}">
>EOF
>__END__
>    <TD BGCOLOR = "Green">

I forgot to add the program I used to see how this worked:

@cell_contents = qw(First Second Third Fourth Fifth);
for (my $row=0;$row<=$#cell_contents;++$row) {
  # if this row number is odd, the cell is Green, otherwise it is Yellow
  print <<EOF;
      <TD BGCOLOR = "${\($row%2? 'Green':
'Yellow')}">$cell_contents[$row]</TD>
EOF
}

OUTPUTS:
<TABLE>
      <TR><TD BGCOLOR = "Yellow">First</TD></TR>
      <TR><TD BGCOLOR = "Green">Second</TD></TR>
      <TR><TD BGCOLOR = "Yellow">Third</TD></TR>
      <TR><TD BGCOLOR = "Green">Fourth</TD></TR>
      <TR><TD BGCOLOR = "Yellow">Fifth</TD></TR>
</TABLE>

cheers - tim
--
unmunge?cold=hot






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

Date: Wed, 20 Dec 2000 08:33:54 GMT
From: appsman1368@my-deja.com
Subject: Unreserved apologies
Message-Id: <91pqti$hgv$1@nnrp1.deja.com>

My sincere apologies to Tad McClellan and the group in general for my
unforgivable outburst. Occasionally I get dumped on by my employer
(usually due to unexpected absence of colleagues) and put into a
position that I have very little experience of and expected to produce
the goods, however, it is inexcusable to vent my frustration in this
way.

Once again, my sincere apologies.


Sent via Deja.com
http://www.deja.com/


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

Date: Wed, 20 Dec 2000 10:03:34 GMT
From: helgi@NOSPAMdecode.is (Helgi Briem)
Subject: Re: what is perldoc?
Message-Id: <3a408227.1118886273@news.itn.is>

On Tue, 19 Dec 2000 17:43:46 GMT, alazarev1981@my-deja.com
wrote:

>Some people keep on putting the following text in their messages
>without any explaination as to where it is:
>
>perldoc perldsc
>perldoc perllol
>
>I am assuming it's some type of perl documentation on the web? Can
>someone please tell me where I can find it? I'm trying to fake a 2d
>array and I can't get it, I need more sources....ack!
>
It is perl documentation, but on your machine,
not on the web.  If you have installed perl
properly you have perldoc.  You can run it
from the command line with various options
to find what you need.

Some useful options are:
perldoc perltoc   # table of contents
perldoc perlfaq   # frequently asked questions
perldoc perlre    # regular expressions
perldoc perlfunc # perl functions
perldoc -f FUNCTION # info on FUNCTION
perldoc -X Module # info on Module
perldoc -q "Where can I get information on Perl?" # question

On an Activestate Perl installation on
Windows, there should be documentation in 
HTML format in C:\Perl\html\index.html or
a similar place.

Regards,
Helgi Briem


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

Date: Wed, 20 Dec 2000 16:44:51 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: what's more efficient in turning an array into a list?
Message-Id: <slrn940hqj.8i7.mgjv@martien.heliotrope.home>

On Tue, 19 Dec 2000 22:40:19 -0600,
    AP <alex@hoopsie2.com> wrote:
> Which of these methods is more efficient (as in the performance sense):
> 
> #------------------------
> my @array = qw(foo bar);
> my $str;
> 
> ## this way?
> 
> foreach ( @array ) {
>   $str .= $_;
> }
> 
> ## or this way?
> 
> $str = join('',@array)
> #------------------------

#!/usr/local/bin/perl -w
use strict;
use Benchmark;

my @a = qw(foo bar baz banana);
print "\$#a = $#a\n";

timethese(-2, {
    concat => sub { my $str = ""; $str .= $_ for @a },
    join   => sub { my $str = ""; $str = join '', @a },
    empty  => sub { my $str = "" }
});

@a = (@a) x 100;
print "\$#a = $#a\n";

timethese(-2, {
    concat => sub { my $str = ""; $str .= $_ for @a },
    join   => sub { my $str = ""; $str = join '', @a },
    empty  => sub { my $str = "" }
});


$#a = 3
Benchmark: running concat, empty, join, each for at least 2 CPU
seconds...
    concat:  2 wallclock secs ( 2.13 usr +  0.00 sys =  2.13 CPU) @
            102790.61/s (n=218944)
     empty:  2 wallclock secs ( 2.19 usr +  0.01 sys =  2.20 CPU) @
            1545523.18/s (n=3400151)
      join:  4 wallclock secs ( 2.09 usr +  0.00 sys =  2.09 CPU) @
            329247.37/s (n=688127)
$#a = 399
Benchmark: running concat, empty, join, each for at least 2 CPU
seconds...
    concat:  3 wallclock secs ( 2.07 usr +  0.00 sys =  2.07 CPU) @
            2475.85/s (n=5125)
     empty:  2 wallclock secs ( 2.14 usr +  0.00 sys =  2.14 CPU) @
            1534694.86/s (n=3284247)
      join:  2 wallclock secs ( 2.03 usr +  0.00 sys =  2.03 CPU) @
            8426.11/s (n=17105)


Looks like the join method is some 3 to 3.5 times faster than the concat
method.  Next time, I hope you'll be able to find the Benchmark module
yourself.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | If at first you don't succeed, try
Commercial Dynamics Pty. Ltd.   | again. Then quit; there's no use
NSW, Australia                  | being a damn fool about it.


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

Date: Wed, 20 Dec 2000 07:45:52 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Wich module for download via http ?
Message-Id: <slrn940osr.nkt.rgarciasuarez@rafael.kazibao.net>

L.Willms@LINK-F.frankfurt.org wrote in comp.lang.perl.misc:
>    I have a web site where people can among other things download
> ZIPped files. Instead of an HREF to the location of the file, I want to
> call a PERL script which hides the actual location of the file, since I
> may have to move the file to a different server for cost reasons.

Your problem is not a Perl problem, it's a CGI problem.

If you want to hide the location of the file to be downloaded, the
better thing to do is to put it somewhere in the filesystem where it's
not accessible via http, and to use a CGI program (not necessarily
written in Perl) to open this file and to send its contents to the
browser with the appropriate content-type header.

Obviously, this is not possible if the files to be downloaded are on a
different webserver.

To write CGI programs, the first step is to learn the basics of web
servers and clients. To write CGI programs with Perl, you'll need also
to learn Perl. To discuss CGI programming, the right newgroups is
comp.infosystems.www.authoring.cgi (followups set).

-- 
# Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Wed, 20 Dec 2000 02:05:45 -0500
From: Dennis Marti <dennis_marti@yahoo.com>
Subject: Re: yet another question: is ' more efficient than "?
Message-Id: <dennis_marti-B61312.02054520122000@news.starpower.net>

In article <3A403B49.A748999E@hoopsie2.com>, AP <alex@hoopsie2.com> 
wrote:

> Is using single quotes more efficent than using double?

> Am I wasting my time single quoting things?

By not reaching for the shift key you're saving energy, and maybe time.

After reading your four posts, I'm wondering what you are trying to 
optimize?

Dennis Marti


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

Date: Wed, 20 Dec 2000 10:10:33 GMT
From: Jerome Abela <Jerome.Abela@free.fr>
Subject: Re: yet another question: is ' more efficient than "?
Message-Id: <3A4084C7.E1AD2C59@free.fr>

AP wrote:
> print 'my name is joe';
> print "my name is joe";
> 
> Notice that nothing is being interpolated.  I would guess that it has to be
> more efficient since perl doesn't have to look for anything to interpolate.
> The only thing it would look for are escaped single quotes and/or forward
> slashes.

Note that the very light difference between the two lines is only at
compile time. Both lines will result in the same precompiled code,
and execution time will be exactly the same.

> Am I wasting my time single quoting things?

Definitely.


Jerome.


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

Date: 20 Dec 2000 10:51:59 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: yet another question: is ' more efficient than "?
Message-Id: <91q30f$kmb$2@bob.news.rcn.net>

Jerome Abela <Jerome.Abela@free.fr> wrote:
> AP wrote:
>> print 'my name is joe';
>> print "my name is joe";
>> 
>> Notice that nothing is being interpolated.  I would guess that it has to be
>> more efficient since perl doesn't have to look for anything to interpolate.
>> The only thing it would look for are escaped single quotes and/or forward
>> slashes.

> Note that the very light difference between the two lines is only at
> compile time. Both lines will result in the same precompiled code,
> and execution time will be exactly the same.

>> Am I wasting my time single quoting things?

> Definitely.

While you're certainly correct that there's no speed or space efficiency
gained by using single quotes where no interpolation is needed, some,
including yours truly, would argue that there's a maintainability
gain: when you're reading through that program you wrote 6 months ago and
that needs to be modified yesterday, you can look at single quotes and
immediately say to yourself "nothing I can do short of changing this
literal will change the value of this string," and look at double quotes
and think "Ah!  This might change if I change something else."



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

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 V9 Issue 5157
**************************************


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