[31266] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2511 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 8 21:09:44 2009

Date: Wed, 8 Jul 2009 18:09: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           Wed, 8 Jul 2009     Volume: 11 Number: 2511

Today's topics:
        Creating a Zip file from HTTP data stream <yasir.sofdev@gmail.com>
    Re: Creating a Zip file from HTTP data stream <RedGrittyBrick@spamweary.invalid>
        Finding recent files and copy them into another folder <jorg_reyes@hotmail.com>
        hot bollywood news for stars <asim.ssat@gmail.com>
    Re: how to format output <cwilbur@chromatico.net>
    Re: Notationsfrage (Christian Kaufhold)
        OT: unix to Windows technology <xahlee@gmail.com>
    Re: OT: unix to Windows technology <kentilton@gmail.com>
    Re: OT: unix to Windows technology <xahlee@gmail.com>
        Perl Fails To List All The Multiple Matches In The Same <cibalo8@gmail.com>
    Re: Perl Fails To List All The Multiple Matches In The  <someone@example.com>
    Re: Question on a 'perlsub' statement. <ben@morrow.me.uk>
    Re: Question on a 'perlsub' statement. (Alan Curry)
    Re: Question on a 'perlsub' statement. <ben@morrow.me.uk>
        tk text editor for blind peole source code <robin1@cnsp.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 8 Jul 2009 04:11:56 -0700 (PDT)
From: Yasir Zaheer <yasir.sofdev@gmail.com>
Subject: Creating a Zip file from HTTP data stream
Message-Id: <bd12d2f8-1a85-422f-ba4b-0048bee39f72@m11g2000yqh.googlegroups.com>

Hi all,

I am facing some problems in creating a zip file coming as HTTP data
stream in perl running over Linux RedHat 4. The actual scenario is
that I am recieving a data stream send by a .Net application running
on windows XP that sends the stream by just reading and sending all
the bytes from a zip file. My logs displays all the data recieved in
the varialbe $$params{LOG} on console but i am unable to create a zip
file out of that. Any help in this regards will be appreciated.


use core qw ( CGI_USER ) ;
use utils::MailMime;
$core::dbh->ensureWebAlive();
my $osysconfig = sysconfig->new({MINIMAL => '1'});
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

my $params = CGI::Vars();
use Archive::Zip qw( :ERROR_CODES );
#print "Content-type:text/html\n\n";
print header;

my $MAILTO = 'abc@xyz.net';


my $MAILFROM = 'abc@xyz.net';

my $smtp = $osysconfig->GetSMTPServer();
my $port = $osysconfig->GetSMTPServerPort();
my $host = hostname();
my $omailer = MailMime->new( { SMTPSERVER=> $smtp, SMTPSERVERPORT =>
$port } );

# $$params{LOG} ---- this is the binary zipped file which i have to
attached with email.
if (($$params{LOG}) and (not $$params{REMOTEFILE}))
{
$file = '/tmp/tmp.zip' ;
#use Archive::Zip;

#my $zip = new Archive::Zip;

#my $data = "$$params{LOG}";

#$zip->addString($data,"LOG");
#$zip->writeToFileNamed($$params{LOG});
#print FILE $$params{LOG};
#close FILE;

#give read write permission to all
#`chmod 666 $file`;
my $fh;
#open ( $fh , '>', $file ) or $core::logger->Die( 'Error opening
file', $@);

#print $fh "$$params{LOG}";
#if (defined $$params{LOG});
#print $fh $$params{SCREENSHOT}
#if (defined $$params{SCREENSHOT});
close $fh;

}

my $text = "Application logs are attached\n\nDetails of the sender:\n
\nLogin:$$params{LOGIN} \nServer Address: $host";
$text .= "\n\n$$params{USERTEXT}" if ( $$params{USERTEXT} );
my %Ret;

if (($$params{LOG}) and (not $$params{REMOTEFILE}))
{
eval
{
%Ret = $omailer->SendFile({

From => $MAILFROM,
To => $MAILTO,
Subject => "Application Report from $$params{LOGIN} on $host",
Text => $text ,
Path => $file,
Type => 'binary'
}) or die "Could not send mail [$!]";
};
}
if ( $@ )
{
print "$@\n";
}
else
{
print "1";
}






regards,
Yasir.


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

Date: Wed, 08 Jul 2009 12:43:30 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Creating a Zip file from HTTP data stream
Message-Id: <4a548666$0$18248$da0feed9@news.zen.co.uk>


Yasir Zaheer wrote:
> Hi all,
> 
> I am facing some problems in creating a zip file coming as HTTP data
> stream in perl running over Linux RedHat 4. The actual scenario is
> that I am recieving a data stream send by a .Net application running
> on windows XP that sends the stream by just reading and sending all
> the bytes from a zip file. My logs displays all the data recieved in
> the varialbe $$params{LOG} on console but i am unable to create a zip
> file out of that. Any help in this regards will be appreciated.
> 
> 
 ...
> use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
 ...
> use Archive::Zip qw( :ERROR_CODES );
 ...

Is that real code?

If you have the all the bytes of a zip file in $$params{LOG}, can't you 
simply 'print' that variable to a binary file without using Archive::Zip?

By the way, wouldn't that be better written as $params->{'LOG'}?
Are you using the 'strict' and 'warnings' pragmas?

-- 
RGB


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

Date: Wed, 8 Jul 2009 17:14:14 -0700 (PDT)
From: Jorge Reyes <jorg_reyes@hotmail.com>
Subject: Finding recent files and copy them into another folder
Message-Id: <f45977c9-dd47-4698-893f-0577455ae170@r33g2000yqn.googlegroups.com>

Hi everyone,

I have this issue which is killing me, this is the scenario

if i make   ls -l     i get this:
-rw-r--r--   1 netrac   netrac      2928 jul   8 18:47 file1.pl
-rw-r--r--   1 netrac   netrac      2928 jul   8 19:07 file2.pl
-rw-r--r--   1 netrac   netrac      2928 jul   8 19:10 file3.pl

now what i need is only the files were accessed >= 19:00  i.e.
-rw-r--r--   1 netrac   netrac      2928 jul   8 19:07 file2.pl
-rw-r--r--   1 netrac   netrac      2928 jul   8 19:10 file3.pl

i found this example which create a rule to find only files (and not
directories or other things) that have been accessed at least 14 days
ago

use File::Find::Rule;
my $path = "/data";
my @results = File::Find::Rule->file->ctime('>14')->in($path);
foreach my $i (@results) {
	print "$i\n";
}

Question is, instead 14 days ago, a specific time i.e. >= current
system date ????

Thanks in advanced


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

Date: Wed, 8 Jul 2009 09:22:17 -0700 (PDT)
From: asim malik <asim.ssat@gmail.com>
Subject: hot bollywood news for stars
Message-Id: <183683c8-3469-4f70-9eba-4f3d78af5728@g7g2000prg.googlegroups.com>

hot bollywood news for stars very interesting
interviews,news,pics ,relationships and more n more visit :
www.enf786.blogspot.com


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

Date: Wed, 08 Jul 2009 12:42:13 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: how to format output
Message-Id: <86eisrceje.fsf@mithril.chromatico.net>

>>>>> "TJM" == Tad J McClellan <tadmc@seesig.invalid> writes:

    TJM> perl 4 is a wonderful language!

    TJM> Doesn't have all that confusing "software engineering" stuff
    TJM> that you boys like so much.

Don't *do* that.

Charlton



-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Wed, 8 Jul 2009 09:49:20 +0200
From: usenet@chka.de (Christian Kaufhold)
Subject: Re: Notationsfrage
Message-Id: <0lvdi6-96s.ln1@mokoscha.chka.de>

Cyrus Kriticos <cyrus.kriticos@googlemail.com> wrote:

> was verbirgt sich hinter der Notation bzw. wofür sind die geschweiften 
> Klammern?
> 
> $f = { 'e' => $e };

perldoc perlref


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

Date: Wed, 8 Jul 2009 09:19:23 -0700 (PDT)
From: Xah Lee <xahlee@gmail.com>
Subject: OT: unix to Windows technology
Message-Id: <db87562d-2c55-4f2f-8df0-5b3631235b93@i18g2000pro.googlegroups.com>

Dear unixers & lispers,

i've been using Mac for the past 19 years, and been a professional sys
admin or web app developers on the unix platform, since 1998 (maily
Solaris, Apache, Perl, Java, SQL, PHP). In june, i bought a PC (not
for the first time though), and made a switch to Windows, for the
first time, in the sense as a developer instead of just a casual PC
user i've been.

In the past month, i've spend about 5 hours a day digging into MS
Windows tech, in particluar, read over 200 Wikipedia articles in
detail related to Windows technology. (192 of them linked)

Here's a write up of the whole story, my experiences, including some
tech introduction to MS Windows from a sys admin or programer point of
view.

=E2=80=A2 Switching from Mac/Unix To PC/Windows
  http://xahlee.org/mswin/switch_to_windows.html

Some slightly noteworthy subsections are:

=E2=80=A2 Removing HP/Compaq Software
  http://xahlee.org/mswin/hp_bundled_apps.html

=E2=80=A2 Installing Cygwin Tutorial
  http://xahlee.org/mswin/installing_cygwin.html

=E2=80=A2 Mac and Windows File Conversion
  http://xahlee.org/mswin/mac_windows_file_conv.html

=E2=80=A2 Unix And Windows File Permission Systems
  http://xahlee.org/mswin/file_perm_systems.html

=E2=80=A2 Introduction to Windows Scripting
  http://xahlee.org/mswin/windows_scripting.html

Some articles (not shown above) are still work in progress, such as
VBScript tutorial and PowerShell tutorial. Hoping to complete in the
coming months or years.

comment & feedback welcome, esp if you are a Windows expert and answer
some of my unanswered questions on the page.

  Xah
=E2=88=91 http://xahlee.org/

=E2=98=84


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

Date: Wed, 08 Jul 2009 12:51:57 -0400
From: Kenneth Tilton <kentilton@gmail.com>
Subject: Re: OT: unix to Windows technology
Message-Id: <4a54ceb0$0$5898$607ed4bc@cv.net>

Xah Lee wrote:
> Dear unixers & lispers,
> 
> i've been using Mac for the past 19 years, and been a professional sys
> admin or web app developers on the unix platform, since 1998 (maily
> Solaris, Apache, Perl, Java, SQL, PHP). In june, i bought a PC (not
> for the first time though), and made a switch to Windows, for the
> first time, in the sense as a developer instead of just a casual PC
> user i've been.
> 
> In the past month, i've spend about 5 hours a day digging into MS
> Windows tech, in particluar, read over 200 Wikipedia articles in
> detail related to Windows technology. (192 of them linked)
> 
> Here's a write up of the whole story, my experiences, including some
> tech introduction to MS Windows from a sys admin or programer point of
> view.
> 
> • Switching from Mac/Unix To PC/Windows
>   http://xahlee.org/mswin/switch_to_windows.html
> 
> Some slightly noteworthy subsections are:
> 
> • Removing HP/Compaq Software
>   http://xahlee.org/mswin/hp_bundled_apps.html
> 
> • Installing Cygwin Tutorial
>   http://xahlee.org/mswin/installing_cygwin.html
> 
> • Mac and Windows File Conversion
>   http://xahlee.org/mswin/mac_windows_file_conv.html
> 
> • Unix And Windows File Permission Systems
>   http://xahlee.org/mswin/file_perm_systems.html
> 
> • Introduction to Windows Scripting
>   http://xahlee.org/mswin/windows_scripting.html
> 
> Some articles (not shown above) are still work in progress, such as
> VBScript tutorial and PowerShell tutorial. Hoping to complete in the
> coming months or years.
> 
> comment & feedback welcome, esp if you are a Windows expert and answer
> some of my unanswered questions on the page.
> 
>   Xah
> ∑ http://xahlee.org/
> 
> ☄

You just discovered PCs are cheaper?

The funny thing is that that is Microsoft's answer to the Apple Mac-PC 
ads, they show people shopping for computers and just comparing hardware 
and price as if this is some kind of breakthrough. But I understand: 
they have no answer to Windows being such a nightmare and the Mac being 
such a joy.

kt


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

Date: Wed, 8 Jul 2009 10:34:50 -0700 (PDT)
From: Xah Lee <xahlee@gmail.com>
Subject: Re: OT: unix to Windows technology
Message-Id: <16430ee7-888e-42ef-9c84-14df4b78ee04@p36g2000prn.googlegroups.com>

Xah Lee wrote:
> =E2=80=A2 Switching from Mac/Unix To PC/Windows
>  http://xahlee.org/mswin/switch_to_windows.html

Kenneth Tilton wrote:
> You just discovered PCs are cheaper?
>
> The funny thing is that that is Microsoft's answer to the Apple Mac-PC
> ads, they show people shopping for computers and just comparing hardware
> and price as if this is some kind of breakthrough. But I understand:
> they have no answer to Windows being such a nightmare and the Mac being
> such a joy.

well, i've been owning Macs for over the past 19 years. From a
dedicated fan thru-out the 1990s, to still fan in early of 2000s with
debut of OS X.

Mac prices in comparison to PC has gone up and downs. In the early
1990s for example, it is maybe 4 times more. Lowest is probably in the
mid 1990s, where 3rd party companies are licensed to producing clones,
and that happens to also be a period that Mac OS is the worst,
crashing few times per day, similar to Win 95 and 98. I think in the
early 2000s the price gap came closer, and since maybe 2005 it start
to increase again.

for someone like me, i've read all the pros and cons of Mac vs PC.
I've read, for instance, basically all MacWorld and MacUser mags in
the early 1990s until they become defunct. (and often MacWeek too) Not
to mention the huge amount of websites, especially in the late 1990s
where there are a number of high profile dedicated mac fan sites. I
also have often attended the yearly Mac World Expo, eagerly antipating
Steve Job's =E2=80=9CO, one more thing...=E2=80=9D  etc... and etc and etc.=
 As a
hyperbole, i've prob read more Mac vs PC argument from newsgroup users
combined. LOL

as to the price comparison... a better metric is value/price ratio. I
think, over the past 20 years, the value/price ratio of Mac compared
to PC, namely:

(MacValue/MacPrice)/(PC_value/PC_price)

is going down, starting with Windows NT 4, and going downhill quickly
with Windows XP, and Vista, .NET.

as far as SOFTWARE technology goes, Apple's falling lamer and lamer
from its 1990s mountain top of lisps and hypercards and desktop
publishing stuff. Microsoft Windows, starting from MS-DOS moronicity,
today, with its .NET, F#, PowerShell, is quite beyond Apple's
perpetual diddling with its prettification of OS X.

  Xah
=E2=88=91 http://xahlee.org/

=E2=98=84


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

Date: Wed, 8 Jul 2009 00:32:50 -0700 (PDT)
From: Cibalo <cibalo8@gmail.com>
Subject: Perl Fails To List All The Multiple Matches In The Same Line?
Message-Id: <1b04ee72-4e90-4147-9fbe-4f4c781ea16f@u16g2000pru.googlegroups.com>

Hello,

I would like to list all the 5-digit zip codes in my database, of
which a line may contain more than one zip codes.  Then, I create a
test database, testdb, for testing as follows.

# echo -e 'zip1 10036; zip2 48226; zip3 94128\nzip4 V8Y 1L1; zip5
400069\nzip6 \nzip7 12345' > testdb
# cat testdb
zip1 10036; zip2 48226; zip3 94128
zip4 V8Y 1L1; zip5 400069
zip6
zip7 12345
# perl -wnl -e '/\b[0-9]{5}\b/g and print "$.: $&";' testdb
1: 10036
4: 12345
# grep -now -e '[0-9]\{5\}' testdb
1:10036
48226
94128
4:12345
#

Even with the global modifier, the above perl script lists only the
first pattern match with multiple matches in the same line.  But I can
make it worked with grep as listed above.

What's wrong with my perl script?  What am I missing?

# perl --version; grep --version
This is perl, v5.10.0 built for i386-linux-thread-multi
Copyright 1987-2007, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source
kit.
Complete documentation for Perl, including FAQ lists, should be found
on
this system using "man perl" or "perldoc perl".  If you have access to
the
Internet, point your browser at http://www.perl.org/, the Perl Home
Page.

grep (GNU grep) 2.5.1
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
#

Thank you very much for your assistance.

Best Regards,

cibalo


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

Date: Wed, 08 Jul 2009 01:27:10 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Perl Fails To List All The Multiple Matches In The Same Line?
Message-Id: <zLY4m.22119$Wx7.8204@newsfe04.iad>

Cibalo wrote:
> 
> I would like to list all the 5-digit zip codes in my database, of
> which a line may contain more than one zip codes.  Then, I create a
> test database, testdb, for testing as follows.
> 
> # echo -e 'zip1 10036; zip2 48226; zip3 94128\nzip4 V8Y 1L1; zip5
> 400069\nzip6 \nzip7 12345' > testdb
> # cat testdb
> zip1 10036; zip2 48226; zip3 94128
> zip4 V8Y 1L1; zip5 400069
> zip6
> zip7 12345
> # perl -wnl -e '/\b[0-9]{5}\b/g and print "$.: $&";' testdb
> 1: 10036
> 4: 12345
> # grep -now -e '[0-9]\{5\}' testdb
> 1:10036
> 48226
> 94128
> 4:12345
> #
> 
> Even with the global modifier, the above perl script lists only the
> first pattern match with multiple matches in the same line.  But I can
> make it worked with grep as listed above.

The problem is that even with the global option the pattern is evaluated 
in scalar context and so will only match once.  You need to either match 
in list context:

$ echo "zip1 10036; zip2 48226; zip3 94128
zip4 V8Y 1L1; zip5 400069
zip6
zip7 12345
" | perl -lne'print "$.: $_" for /\b[0-9]{5}\b/g'
1: 10036
1: 48226
1: 94128
4: 12345


Or match all patterns in scalar context:

$ echo "zip1 10036; zip2 48226; zip3 94128
zip4 V8Y 1L1; zip5 400069
zip6
zip7 12345
" | perl -lne'print "$.: $1" while /\b([0-9]{5})\b/g'
1: 10036
1: 48226
1: 94128
4: 12345



John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


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

Date: Wed, 8 Jul 2009 14:07:44 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Question on a 'perlsub' statement.
Message-Id: <0aiei6-45p2.ln1@osiris.mauzo.dyndns.org>


Quoth SomeDeveloper <somedeveloper@gmail.com>:
> 
> Consider this example.
> 
>   #!/usr/bin/perl
>   use warnings;
>   use strict 'vars';
> 
>   my $x;
>   print "list context...\n";
>   ($x) = <STDIN>; # Here, I must press Ctrl-D explicitly.
>   print "value = '$x'\n";
> 
>   print "----\n";
>   print "scalar context...\n";
>   $x = <STDIN>; # Here, control returns after my first <ENTER>.
>   print "value = '$x'\n";
> 
> Here's the run...
>   $ ./misc.pl
>   list context...
>     This is line 1 of input.
>     This is line 2 of input!!!!
>   value = '  This is line 1 of input.
>   '
>   ----
>   scalar context...
>     This is line 1 of input.
>   value = '  This is line 1 of input.
>   '
> 
> I fully understand that 'my' has no effect on scalar vs list context
> being provided to the rhs. What I did (and do) not understand is the
> semantics. Here's how I thought (and still think) how Perl should've
> worked:
>  1. Perl encounters the statement:
>         ($x) = <STDIN>
>  2. Perl provides a list context to the rhs.
>  3. The <> operator becomes aware of its list context, and begins
> expecting/reading (possibly) multiple lines of input until Ctrl-D is
> seen.
>  4. Perl holds the input read so far in some internal temporary.

So far correct. (The 'internal temporary' is the Perl stack.)

>  5. Perl sees that there is only 1 variable -- $x -- in the list on
> the lhs, and so assigns the entire input text to $x, much like the
> behavior of join('', <STDIN>).

What happens next is nothing to do with <>. <> has finished and
returned, and perl has moved on to the next operator, in this case the
assignment. So the next question is 'what happens when you assign a long
list into a short list?'. The answer is 'any extra elements on the RHS
are discarded'.

Your expectation that the lines would be joined is unreasonable. What
would you expect

    ($x, $y) = <STDIN>;

to do? The way it does work is pretty much the only way list assignment
*could* work.

> But, apparently, what is happening in step 5 above is that Perl picks
> up only the first line from the text input and discards the rest!
> 
> This appears to me to be more than the semantics of an assignment
> operator: after the r-value (a multi-line text input) is ready to be
> assigned to the l-value (the $x var), Perl kind of intervenes and
> parses the first line out of the r-value.

The assignment operator does no parsing. The parsing was done by <>: the
string of bytes in the file was split up into a list of lines. The
assignment operator just says 'oh look, I've got more values than I have
space to assign' and throws the rest away. It's the same as

    ($x, $y) = (1, 2, 3, 4, 5, 6);

Ben



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

Date: Wed, 8 Jul 2009 23:42:25 +0000 (UTC)
From: pacman@kosh.dhis.org (Alan Curry)
Subject: Re: Question on a 'perlsub' statement.
Message-Id: <h33at1$fub$1@aioe.org>

In article <6b8di6-6mj2.ln1@osiris.mauzo.dyndns.org>,
Ben Morrow  <ben@morrow.me.uk> wrote:
>
>Quoth pacman@kosh.dhis.org (Alan Curry):
>> To understand the comparison made above, you have to see similarity between
>> this strange pair:
>>     ($foo) = <STDIN>;
>>     my($foo) = <STDIN>;
>> 
>> In the first, the left operand of the assignment operator is a parenthesized
>> expression. In the second, the left operand of the assignment operator is a
>> function call (by the rule "if it looks like a function, it is a function")
>
>No. my (and local) are among the few exceptions to 'if it looks like a
>function',

Exceptions make language learning harder. And it still looks like a function
to me, long after I learned that it doesn't behave the way I thought it
should.

>           which is one reason why I *always* put a space between the
>'my' and the open paren. For example, a sub called 'my' will always be
>simply ignored, without even a warning. The other syntax keywords like
>'if' and 'while' are the same: presumably you don't interpret
>
>    if ($x == 1) {
>
>as a function call?

Interesting question. The answer is no, obviously, but I don't know exactly
why. Because "if" is so deeply ingrained as being a keyword that it's
impossible to read it as a function name, or maybe because it doesn't parse
as a term in an expression.

>    ($x) = ...;
>
>makes the LHS into a list of one element, one of the two cases (the
>other being the empty list ()) where parens *are* necessary to construct
>a list.

All right, by now we all know now that
  ($x) = something_returning_a_list();
  my ($x) = something_returning_a_list();
both discard all but the first element of the generated list. New questions:

If you saw a statement of that form in perl code, without knowing anything
about the author, would you consider it more likely to be a mistake similar
to the one that started this thread, or a deliberate use of single-element
list assignment?

In your own code, would you actually use a single-element list assignment to
take the first element of a list and discard the rest, or would you make the
discard more explicit, as a favor to future readers of your code?

If it can be agreed that the slightly more verbose forms
  $x = (something_returning_a_list())[0];
  my $x = (something_returning_a_list())[0];
are easier to read, might we get a warning for the "too subtle" form?

-- 
Alan Curry


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

Date: Thu, 9 Jul 2009 01:18:49 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Question on a 'perlsub' statement.
Message-Id: <9kpfi6-lfr2.ln1@osiris.mauzo.dyndns.org>


Quoth pacman@kosh.dhis.org (Alan Curry):
> In article <6b8di6-6mj2.ln1@osiris.mauzo.dyndns.org>,
> Ben Morrow  <ben@morrow.me.uk> wrote:
> >
> >    ($x) = ...;
> >
> >makes the LHS into a list of one element, one of the two cases (the
> >other being the empty list ()) where parens *are* necessary to construct
> >a list.
> 
> All right, by now we all know now that
>   ($x) = something_returning_a_list();
>   my ($x) = something_returning_a_list();
> both discard all but the first element of the generated list. New questions:
> 
> If you saw a statement of that form in perl code, without knowing anything
> about the author, would you consider it more likely to be a mistake similar
> to the one that started this thread, or a deliberate use of single-element
> list assignment?

The latter, obviously, unless I had other reasons for thinking the
original author was stupid :).

> In your own code, would you actually use a single-element list assignment to
> take the first element of a list and discard the rest, or would you make the
> discard more explicit, as a favor to future readers of your code?

I frequently use both

    my ($self) = @_;

and constructions of the form

    my ($rx) =
        map qr/$_/,
        join "|",
        map quotemeta,
        @words;

though in both these cases I am expecting a single-element list on the
RHS. I don't know about cases where I am expecting to discard elements.

> If it can be agreed that the slightly more verbose forms
>   $x = (something_returning_a_list())[0];
>   my $x = (something_returning_a_list())[0];
> are easier to read, might we get a warning for the "too subtle" form?

Yuck, what a mess. No, I would be strongly opposed to such a warning.

Ben



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

Date: Wed, 8 Jul 2009 01:33:34 -0700 (PDT)
From: Robin <robin1@cnsp.com>
Subject: tk text editor for blind peole source code
Message-Id: <ac1cecb4-2b9b-4d95-bf14-a63234df7fd8@i6g2000yqj.googlegroups.com>

#!/usr/bin/perl
use strict;
use Tk;


my $m = new MainWindow ();
my $z = $m -> Text (-width=>'50', -height=>'30', -background=>'black',
-foreground=>'yellow', -font=>'Verdana') -> pack ();
#        my $q1 = $m -> Label (-text=>"ENTER THE FILE NAME OF THE FILE
YOU WANT TO SAVE") -> pack ();-
my $e = $m -> Entry (-background=>"black", -foreground=>"yellow", -
font=>"Verdana");
my $l1 = $m -> Label (-text=>"Enter the file name of the file you want
to save: ", -foreground=>'yellow', -background=>"black") -> pack ();
$m -> configure (-background=>'black', -foreground=>"yellow", -
title=>"Text editor for blind people");
my $e = $m -> Entry (-background=>"black", -foreground=>"yellow") ->
pack ();

$e -> pack ();

my $b1 = $m -> Button (-command=>\&save, -background=>"black", -
foreground=>'yellow', -font=>'yellow', -text=>"Save File") -> pack ();


MainLoop ();


my (@o);
my $flag = '';
sub save () {

        my $c = $z -> Contents ();
        my $l = $e -> get ();
        foreach (@o) {
                if ($_ and $_ eq $l) {
                        $flag = $_;
                }
                else {
                        next;
                }

        }
        if (! -e $l and $l) {
                $flag = 1;
        }
        if ($flag) {
                open (A, ">$l");
                print A $c;
                $m -> Dialog (-text=>"File saved successfully") ->
Show ();



        }
        else {
               $m -> Dialog (-text=>"That file already exists. Try
another file name.") -> Show ();

        }

}


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

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 2511
***************************************


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