[30867] in Perl-Users-Digest
Perl-Users Digest, Issue: 2112 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 10 09:09:50 2009
Date: Sat, 10 Jan 2009 06:09:09 -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, 10 Jan 2009 Volume: 11 Number: 2112
Today's topics:
Re: Circular lists <gamo@telecable.es>
Re: I am needing a gentle introduction to accessing a p <lvirden@gmail.com>
Re: I am needing a gentle introduction to accessing a p <lvirden@gmail.com>
Re: I am needing a gentle introduction to accessing a p <hjp-usenet2@hjp.at>
new CPAN modules on Sat Jan 10 2009 (Randal Schwartz)
Re: opening a file <kkeller-usenet@wombat.san-francisco.ca.us>
Re: opening a file <hjp-usenet2@hjp.at>
Re: opening a file <george@example.invalid>
Re: opening a file <hjp-usenet2@hjp.at>
Re: opening a file <tadmc@seesig.invalid>
Re: please help me to understand this code? <pilcrow6@gmail.com>
Re: Syntactic sugar for scope closing hook? <ced@blv-sam-01.ca.boeing.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 10 Jan 2009 12:09:33 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Circular lists
Message-Id: <alpine.LNX.2.00.0901101202080.1613@jvz.es>
On Fri, 9 Jan 2009, xhoster@gmail.com wrote:
> Jim Gibson <jimsgibson@gmail.com> wrote:
> >
> > I think you want to know how many unique permutations may be generated
> > that are invariant under rotation (moving the last element to the front
> > of the list any number of times).
> >
> > You can use the advice given in 'perldoc -q permute' to generate all
> > possible permutations. Because the elements in your example are not
> > unique, you will generate some permutations which are also not unique.
> > If this is actually the case, then you want an efficient way of
> > ignoring redundant permutations.
> >
> > One way would be to attach a unique key to each member of your list,
> > e.g. qw( a1 a2 b3 b4 b5 c6 c7 c8 c9 ). Then, you only accept a
> > permutation that has each repeated element in order. For example ( a1,
> > a2, ... ) would be accepted but ( a2, a1, ... ) would be rejected. You
> > apply this test for all the a's, b's, and c's in the permutation. If
> > any are out of order, reject the permutation. To use the list, strip
> > off the keys.
>
> It might be possible to integrate that technique into the Fischer-Kause
> algorithm so that only the unique ones are generated, rather than having
> to generate, test, and discard. Now that would be some subtle code.
>
> >
> > For the circularity problem, each permutation can be rotated to produce
> > all of the equivalent cases. For N elements (9 in your sample case),
> > there will be N equivalent cases. To avoid generating the redundant
> > cases, select one element (e.g. a1 from your list), place it in the
> > first location, and generate all possible permutations of the remaining
> > N-1 elements, using the method described above if you have non-unique
> > elements. The result should be all possible circular lists (if I
> > understand your definition correctly).
>
> I think this is a good start, but it still produces redundant results.
>
> For example, if @set = qw/A A C/; # and we number the elements as you did
> above, then this method will return both:
>
> A1 A2 C1
> A1 C1 A2
>
> Which are circularly the same once the digits are removed.
>
> Xho
>
Thanks to all the responders, but I think now that the problem is
unsolvable. Since you need to compare a new candidate with those of
the past, the problem is more or less O(n^2). No matter if the algo-
rithm is random or deterministic in the enumeration.
Best regards,
> --
> -------------------- http://NewsReader.Com/ --------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
>
--
http://www.telecable.es/personales/gamo/
"Was it a car or a cat I saw?"
perl -E 'say 111_111_111**2;'
------------------------------
Date: Sat, 10 Jan 2009 05:17:21 -0800 (PST)
From: "Larry W. Virden" <lvirden@gmail.com>
Subject: Re: I am needing a gentle introduction to accessing a perl array from a reference
Message-Id: <c14ddbb0-566a-4d8a-82bd-54a7e55de764@m16g2000vbp.googlegroups.com>
On Jan 10, 4:17 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> Since the rest of the code looks like it expects
> an array of hashrefs I have to ask whether you are sure that this is
> really the code which is called. Or is there a very similar method which
> differs only in calling fetchrow_hashref instead of fetchrow_array, and
> you are calling that instead?
Well
$ grep fetchrow_hash user_info.pl
$ getmanlist.pl
------------------------------
Date: Sat, 10 Jan 2009 05:25:04 -0800 (PST)
From: "Larry W. Virden" <lvirden@gmail.com>
Subject: Re: I am needing a gentle introduction to accessing a perl array from a reference
Message-Id: <920696c9-5af8-4b65-bcbd-a0e9e3bf38d9@e3g2000vbe.googlegroups.com>
On Jan 10, 8:17 am, "Larry W. Virden" <lvir...@gmail.com> wrote:
> On Jan 10, 4:17 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
>
> > Since the rest of the code looks like it expects
> > an array of hashrefs I have to ask whether you are sure that this is
> > really the code which is called. Or is there a very similar method which
> > differs only in calling fetchrow_hashref instead of fetchrow_array, and
> > you are calling that instead?
>
> Well
>
> $ grep fetchrow_hash user_info.pl
> $ getmanlist.pl
whoops - that Enter key just sent the posting, instead of moving to
the next line. Sigh.
anyways, when I run getmanlist.pl (the program that treats the value
returned as a hash) I get values back from the line that uses the
hash. Ah - I see the reason for the descrepancy. As I mentioned, the
web service has a lot of methods, and the one I was thinking was the
one being called was not.
I have problems with focusing with my trifocals. Here's the code the
function being called is using:
sub GetManagerList{
my (@next_row);
my @retval; #this will be array of hashes
#query db to find information
my ($dbconn) = DBI->connect("DBI:Oracle:$DBHOST",$DBUSER,$DBPASS) or
return \@retval;
my ($query) = $dbconn->prepare(
"SELECT h.*, c.UNIX_UID from CSI_HR
h, CSI_CORE c WHERE h.ALT_EMP_NO = c.HR_ALT_EMP_NO and EXISTS
(Select * FROM CSI_HR h2 where
h.Alt_Emp_No = h2.Supervisor_Emp_No) order by Last_Name");
$query->execute() or return \@retval; #problem return empty array
@next_row= $query->fetchrow_array();
while (@next_row) #get rows and add to return array of hashes
{
#get UID and Name
push(@retval,{Alt_Emp_No => $next_row[0], Job_Class => $next_row
[1], Title => $next_row[2],Dept_Code => $next_row[3],
Dept_No => $next_row[4],Last_Name => $next_row
[5],First_Name => $next_row[6],Middle_Name => $next_row[7],
Supervisor_Emp_No => $next_row[8], HR_Status =>
$next_row[9],
HR_Create => $next_row[10],
HR_Mod => $next_row[11], User_Name => scalar getpwuid
($next_row[12])});
@next_row= $query->fetchrow_array();
}
$query->finish;
$dbconn->disconnect();
return \@retval;
}
Anow now that I found the right function, I see the code manufactures
its own hash in the return value from the flat arrays. That makes so
much more sense. I kept trying to figure out how the other code was
doing what it was doing, and it was causing me to misunderstand how
perl in general worked. Now things are much more clear.
Thank you all for your help!
------------------------------
Date: Sat, 10 Jan 2009 10:17:31 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: I am needing a gentle introduction to accessing a perl array from a reference
Message-Id: <slrngmgptc.g3i.hjp-usenet2@hrunkner.hjp.at>
On 2009-01-09 12:35, Larry W. Virden <lvirden@gmail.com> wrote:
> On Jan 8, 4:56 pm, Tad J McClellan <ta...@seesig.invalid> wrote:
>> Larry W. Virden <lvir...@gmail.com> wrote:
>> > # put results of query into single array and return
>> > while (@next_row= $query->fetchrow_array()){
>>
>> > push(@array_result,@next_row);
>> > }
>>
>> > $query->finish;
>> > $dbconn->disconnect();
>> > return \@array_result;
>>
>> That is an awfully strange way to do it, unless your query is selecting
>> only a single column.
>
> I didn't write the code, and haven't used arrays, references, and DBI
> enough to know about it. In the particularly select I was looking at,
> I think the select might very well be one that only returns a single
> row. I'm still trying to learn about the database and its
> characteristics.
>
>>
>> Is this your real code?
>>
>> The data structure here is nothing more than (a reference to) a flat array.
>
> That's the real code from the program I'm trying to understand. So,
> when the fetchrow_array returns @next_row, this is a "flat array".
> It's still a hash, right?
No, fetchrow_array returns an array, not a hash. If you want a hash(ref),
use fetchrow_hashref. Since the rest of the code looks like it expects
an array of hashrefs I have to ask whether you are sure that this is
really the code which is called. Or is there a very similar method which
differs only in calling fetchrow_hashref instead of fetchrow_array, and
you are calling that instead?
hp
------------------------------
Date: Sat, 10 Jan 2009 05:42:25 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Sat Jan 10 2009
Message-Id: <KD8qIp.2Kx@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
Apache2-Response-FileMerge-0.37
http://search.cpan.org/~wazzuteke/Apache2-Response-FileMerge-0.37/
Merge and include static client-side style and code
----
Apache2-WURFLFilter-1.21
http://search.cpan.org/~ifuschini/Apache2-WURFLFilter-1.21/
is a Apache Mobile Filter that manage content (text & image) to the correct mobile device
----
Astro-satpass-0.020
http://search.cpan.org/~wyant/Astro-satpass-0.020/
----
CGI-Application-Plugin-I18N-0.03
http://search.cpan.org/~cosmicnet/CGI-Application-Plugin-I18N-0.03/
I18N and L10N methods for CGI::App
----
CGI-Application-Plugin-RunmodeDeclare-0.07
http://search.cpan.org/~rhesa/CGI-Application-Plugin-RunmodeDeclare-0.07/
Declare runmodes with keywords
----
CGI-ContactForm-1.43
http://search.cpan.org/~gunnar/CGI-ContactForm-1.43/
Generate a web contact form
----
CPAN-WWW-Testers-0.42
http://search.cpan.org/~barbie/CPAN-WWW-Testers-0.42/
Present CPAN Testers data
----
Catalyst-Controller-WrapCGI-0.0025
http://search.cpan.org/~rkitover/Catalyst-Controller-WrapCGI-0.0025/
Run CGIs in Catalyst
----
Catalyst-View-Text-Template-0.000
http://search.cpan.org/~rjbs/Catalyst-View-Text-Template-0.000/
Text::Template views for Catalyst
----
Config-AutoConf-0.11
http://search.cpan.org/~ambs/Config-AutoConf-0.11/
A module to implement some of AutoConf macros in pure perl.
----
Config-Model-Itself-1.208
http://search.cpan.org/~ddumont/Config-Model-Itself-1.208/
Model editor for Config::Model
----
Convert-Color-0.02
http://search.cpan.org/~pevans/Convert-Color-0.02/
color space conversions and named lookups
----
DBIx-Perlish-0.51
http://search.cpan.org/~gruber/DBIx-Perlish-0.51/
a perlish interface to SQL databases
----
Darcs-Inventory-1.1
http://search.cpan.org/~david/Darcs-Inventory-1.1/
Read and parse a darcs version 1 or 2 inventory file
----
Darcs-Notify-2.0
http://search.cpan.org/~david/Darcs-Notify-2.0/
Do something cool when a Darcs repository has patches added or removed
----
Date-Tie-0.19
http://search.cpan.org/~fglock/Date-Tie-0.19/
ISO dates made easy
----
Devel-Pragma-0.32
http://search.cpan.org/~chocolate/Devel-Pragma-0.32/
helper functions for developers of lexical pragmas
----
File-Find-Object-0.1.6
http://search.cpan.org/~shlomif/File-Find-Object-0.1.6/
An object oriented File::Find replacement
----
Finance-FINRA-File-PostBranchIndividualReport-0.001000
http://search.cpan.org/~groditi/Finance-FINRA-File-PostBranchIndividualReport-0.001000/
Parse the Branch CRD Report
----
Math-MPC-0.52
http://search.cpan.org/~sisyphus/Math-MPC-0.52/
perl interface to the MPC (multi precision complex) library.
----
Module-Starter-Plugin-CGIApp-0.08
http://search.cpan.org/~jaldhar/Module-Starter-Plugin-CGIApp-0.08/
template based module starter for CGI apps.
----
Muldis-D-0.57.0
http://search.cpan.org/~duncand/Muldis-D-0.57.0/
Formal spec of Muldis D relational DBMS lang
----
Net-Amazon-S3-ACL-0.1.0
http://search.cpan.org/~polettix/Net-Amazon-S3-ACL-0.1.0/
work with Amazon S3 Access Control Lists
----
Net-OpenSSH-0.15
http://search.cpan.org/~salva/Net-OpenSSH-0.15/
Perl SSH client package implemented on top of OpenSSH
----
Net-SMS-Optimus-0.05
http://search.cpan.org/~root/Net-SMS-Optimus-0.05/
Send SMS through www.optimus.pt
----
Net-Whois-Gateway-Client-0.08
http://search.cpan.org/~davinchi/Net-Whois-Gateway-Client-0.08/
Interface to Net::Whois::Gateway::Server
----
Padre-0.25
http://search.cpan.org/~szabgab/Padre-0.25/
Perl Application Development and Refactoring Environment
----
RDF-Trine-0.110_01
http://search.cpan.org/~gwilliams/RDF-Trine-0.110_01/
An RDF Framework for Perl.
----
Return-DataButBool-0.0.3
http://search.cpan.org/~dmuey/Return-DataButBool-0.0.3/
Return a boolean value that also has arbitrary numeric and string values
----
Rose-HTML-Objects-0.601
http://search.cpan.org/~jsiracusa/Rose-HTML-Objects-0.601/
Object-oriented interfaces for HTML.
----
SVG-2.48
http://search.cpan.org/~ronan/SVG-2.48/
Perl extension for generating Scalable Vector Graphics (SVG) documents
----
Spreadsheet-ParseExcel-0.44
http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.44/
Extract information from an Excel file.
----
String-ZeroButTrue-0.1
http://search.cpan.org/~dmuey/String-ZeroButTrue-0.1/
utils for consistent zero-but-true useage
----
String-ZeroButTrue-0.2
http://search.cpan.org/~dmuey/String-ZeroButTrue-0.2/
utils for consistent zero-but-true usage
----
Sub-Alias-0.03
http://search.cpan.org/~gugod/Sub-Alias-0.03/
Simple subroutine alias.
----
Sys-Mmap-Simple-0.04
http://search.cpan.org/~leont/Sys-Mmap-Simple-0.04/
Memory mapping made simple and safe.
----
Sys-Mmap-Simple-0.05
http://search.cpan.org/~leont/Sys-Mmap-Simple-0.05/
Memory mapping made simple and safe.
----
TAP-Formatter-JUnit-0.02
http://search.cpan.org/~gtermars/TAP-Formatter-JUnit-0.02/
Harness output delegate for JUnit output
----
UMLS-Interface-0.05
http://search.cpan.org/~btmcinnes/UMLS-Interface-0.05/
README
----
UltraDNS-0.02
http://search.cpan.org/~timb/UltraDNS-0.02/
Client API for the NeuStar UltraDNS Transaction Protocol
----
UltraDNS-0.03
http://search.cpan.org/~timb/UltraDNS-0.03/
Client API for the NeuStar UltraDNS Transaction Protocol
----
XML-FeedLite-1.008
http://search.cpan.org/~rpettett/XML-FeedLite-1.008/
Perl extension for fetching Atom and RSS feeds with minimal outlay
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: Fri, 9 Jan 2009 21:48:57 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: opening a file
Message-Id: <9fpl36x8d.ln2@goaway.wombat.san-francisco.ca.us>
On 2009-01-10, George <george@example.invalid> wrote:
>
> I seem completely befuddled by bonehead stuff here. An example is that dos
> gives me nothing for perldoc; what's more, I find options on the net that
> don't seem to be as simple as this task must be.
>
> http://perldoc.perl.org/File/Find.html
That module won't help you open a file. The FAQ entry I mentioned is
also on the web:
http://perldoc.perl.org/perlfaq5.html#How-can-I-read-in-an-entire-file-all-at-once%3f
In the future if someone refers you to perldoc -q, it's a reference to
the FAQ, which is here:
http://perldoc.perl.org/perlfaq.html
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
------------------------------
Date: Sat, 10 Jan 2009 10:56:02 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: opening a file
Message-Id: <slrngmgs5i.g3i.hjp-usenet2@hrunkner.hjp.at>
On 2009-01-10 03:11, George <george@example.invalid> wrote:
> On Fri, 09 Jan 2009 18:24:38 -0800, Jürgen Exner wrote:
>> George <george@example.invalid> wrote:
>>>Shoot, I get nothing for output here. I added asymbol to yours, but I
>>>still get nothing. I have to believe that USER ERROR is rearing its head
>>>and laughing at me.
>>>
>>> open(50, '<ehp3.txt>');
>>> DO:
>>> { my $line = readline(*50);
>>> if(eof != 0) { exit }
>>> print $line; # no 'write' here
>>> redo DO } # no 'end' possible
>>> close(50)
>>
>> Ouch! This hurts!
I'm sure Mirco meant that as a joke.
>> I suppose some people manage to program in Fortran no
>> matter what programming language they are using.
>
> That's what they say about fortran, is that you do it in any syntax. If
> you don't eventually "give in" to the ethos of a scripting language, you
> miss the better part of it.
Right. See below.
> Wir meinen damit daß das abwertend ist, fortran sonstwo zu üben.
I'm not sure whether this sentence means what you think it means ;-)
> For example, there's one line in a perl script. Mirko's script does well
> to entertain my previous notions here. In particular, the unit number in
> fortran looks like it works as a file handle for perl. Am I correct that
> 50 is a perfectly kosher fh here.
Not quite. A slight hint that it isn't perfectly kosher is that a "*" is
needed in the argument list to readline to distinguish it from the
number 50.
Use lexical file handles:
open(my $fh, '<ehp3.txt>');
DO:
{ my $line = readline($fh);
if(eof != 0) { exit }
print $line;
redo DO }
close($fh)
Use the three argument form of open:
open(my $fh, '<', 'ehp3.txt>');
DO:
{ my $line = readline($fh);
if(eof != 0) { exit }
print $line;
redo DO }
close($fh)
(which btw, makes it clear that the file name is wrong. You don't want
to open 'ehp3.txt>', but 'ehp3.txt', or rather 'eph3.txt')
Use proper loops instead using redo to jump back to the
beginning of a block.
open(my $fh, '<', 'eph3.txt');
while (my $line = readline($fh)) {
print $line;
}
close($fh)
Aquaint yourself with the operator form of readline.
open(my $fh, '<', 'eph3.txt');
while (my $line = <$fh>) {
print $line;
}
close($fh)
And you might want to use the $_ variable:
open(my $fh, '<', 'eph3.txt');
while (<$fh>) {
print $_;
}
close($fh)
In short: Write Perl, not Perl-disguised-as-Fortran.
And of course you should check for errors:
open(my $fh, '<', 'eph3.txt') or die "cannot open eph3.txt: $!";
while (<$fh>) {
print $_;
}
close($fh)
And you should use variables if you use the same string more than once:
my $filename = 'eph3.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
while (<$fh>) {
print $_;
}
close($fh)
(this is especially important for error messages. If you misspell a
filename in the call to open, but not in the error message, the error
message is extremely confusing)
hp
------------------------------
Date: Sat, 10 Jan 2009 03:08:41 -0700
From: George <george@example.invalid>
Subject: Re: opening a file
Message-Id: <fhx06zajzefc$.1xg4dakh48zw5.dlg@40tude.net>
On Thu, 08 Jan 2009 10:52:23 -0800, Jürgen Exner wrote:
> cartercc <cartercc@gmail.com> wrote:
>>On Jan 7, 9:50Â pm, George <geo...@example.invalid> wrote:
>>> I thought I would use perl instead of fortran to parse a text file, what
>>> with the new m// s/// capabilities at my fingertips.
>>
>>Easy, easy, easy ...
>>
>>open INFILE, "<nameoffile.txt";
>
> Most people would suggest to use the three-argument form of open:
> open INFILE, '<', 'nameoffile.txt';
>
> Almost all people would strongly suggest to test for failure:
> open INFILE, '<', 'nameoffile.txt' or
> die "Cannot open nameoffile.txt because $!\n";
>
> jue
Apparently, going out and getting drunk was exactly what this script
needed, as well as comments that helped.
open(50, '<eph4.txt');
DO:
{ my $line = readline(*50);
if(eof != 0) { exit }
print $line; # no 'write' here
redo DO } # no 'end' possible
close(50)
# perl faulk4.pl
C:\MinGW\source>perl faulk4.pl
Sun 18h 41m 55s -23â–‘ 5.4' 0.983 10.215 52.155 Up
Mercury 20h 2m 16s -22â–‘ 12.5' 1.102 22.537 37.668 Up
Venus 21h 55m 33s -14â–‘ 16.3' 0.795 39.872 11.703 Up
Moon 21h 17m 19s -15â–‘ 2.4' 62.4 ER 36.796 22.871 Up
Mars 18h 11m 59s -24â–‘ 6.1' 2.431 4.552 56.184 Up
Jupiter 20h 3m 35s -20â–‘ 49.4' 6.034 23.867 38.203 Up
Saturn 11h 32m 59s +5â–‘ 8.6' 9.018 -47.333 157.471 Set
Uranus 23h 21m 30s -4â–‘ 57.9' 20.421 48.328 -18.527 Up
Neptune 21h 39m 30s -14â–‘ 22.8' 30.748 38.963 16.599 Up
C:\MinGW\source>
Tja.
--
George
It's clearly a budget. It's got a lot of numbers in it.
George W. Bush
Picture of the Day http://apod.nasa.gov/apod/
------------------------------
Date: Sat, 10 Jan 2009 12:47:20 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: opening a file
Message-Id: <slrngmh2m8.nr8.hjp-usenet2@hrunkner.hjp.at>
On 2009-01-10 01:28, George <george@example.invalid> wrote:
> On Thu, 08 Jan 2009 21:25:17 +1300, Ryan McCoskrie wrote:
>> #read out one line at a time until the file ends
>> while( <FILE> ){
>> print $_; #If you don't specify a file it goes to STDOUT (STandard OUTput)
>> #The $_ variable is created by the while statement
>
> I didn't know that.
Good for you. It's wrong.
The $_ variable isn't created by the while statement. It's a predefined
variable which always exists.
However, if you use the <> operator (or the readline function) inside
the condition of a while loop without explicitely assigning it to a
variable, the return value is automatically assigned to $_.
Thus
while (<FILE>) { .... }
and
while (readline(FILE)) { ... }
are just abbreviations for
while ($_ = <FILE>) { .... }
And this in turn is an abbreviation for
while (defined ($_ = <FILE>)) { .... }
because of another special case.
hp
------------------------------
Date: Sat, 10 Jan 2009 07:07:42 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: opening a file
Message-Id: <slrngmh7cu.206.tadmc@tadmc30.sbcglobal.net>
George <george@example.invalid> wrote:
> open(50, '<eph4.txt');
You should always, yes *always*, check the return value from open():
open(50, '<eph4.txt') or die "could not open 'eph4.txt' $!";
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 09 Jan 2009 21:51:39 -0800
From: Pilcrow <pilcrow6@gmail.com>
Subject: Re: please help me to understand this code?
Message-Id: <9qdgm41h6h23p2qv5kifa0j2gl034b4uaj@4ax.com>
On Fri, 9 Jan 2009 22:19:15 +0000 (UTC), tmcd@panix.com (Tim McDaniel)
wrote:
>In article <eegfm4lkf3ikacp4ddcdrlveuo0l8n2ec2@4ax.com>,
>
>I too perceived you as being insulting.
I have an image, now, of a princess, atop 100 mattresses, atop a pea.
Sorry about that pea, princess. But you have to make allowances for a
swine like me. Oink, oink.
------------------------------
Date: Fri, 9 Jan 2009 21:33:46 -0800 (PST)
From: "C.DeRykus" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: Syntactic sugar for scope closing hook?
Message-Id: <d4cdb05e-d5ec-4f91-a3f2-ea6aad7d262d@g39g2000pri.googlegroups.com>
On Jan 9, 2:08 pm, Steve Roscio <steve.ros...@hp.com> wrote:
> Howdy -
>
> I have a module where its functions work in pairs: an opening and
> closing pairs. Because it's likely that these functions will be used
> where the programmer also uses exceptions (eval/die), or just wants to
> be lazy, it's important that the close function is always called when
> exiting the enclosing scope. I find it tedious to track down every
> possible exit path and include the closure.
>
> The simple way to achieve this is to create and object and have a
> DESTROY method for it:
> {
> my $trak = function_returning_some_object (args...);
> . . .
> } # DESTROY() method called here, invokes close function
>
> That's OK, but I'd like to take it to the next level and make it
> syntactically look nicer. Some sugar, if you will. I want to just do this:
>
> {
> blah args...; # blah creates object in *current* scope
> . . .
> } # Closure for 'blah' object called
>
> How can I do this?
>
> I've had some success by stuffing a closure (with a DESTROY) into the
> caller(1)'s symbol glob. But that only handles the case when the scope
> is the enclosing sub. How do I do this for local scopes created simply
> by pairs of { }'s ?
>
> I've also done source filters that simply convert the sugar-coated form
> into the primitive form. But that feels evil.
IIUC Scope::Guard may be able to help:
use Scope::Guard;
{
my $cleanup = sub { print "cleaning up...\n"; };
my $guard = Scope::Guard->new( $cleanup );
}
--
Charles DeRykus
------------------------------
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 V11 Issue 2112
***************************************