[25355] in Perl-Users-Digest
Perl-Users Digest, Issue: 7600 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 4 00:05:43 2005
Date: Mon, 3 Jan 2005 21:05:13 -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 Mon, 3 Jan 2005 Volume: 10 Number: 7600
Today's topics:
file rename help <spedwards@x.qwest.net>
Re: file rename help <ahamm@mail.com>
Re: file rename help <ahamm@mail.com>
Re: file rename help <peter@invalid.invalid>
Re: file rename help <jkeen_via_google@yahoo.com>
Re: inheritance and order of packages <matthew.garrish@sympatico.ca>
Re: inheritance and order of packages ioneabu@yahoo.com
Language environment in perl; how to open a file with f (phallk)
Re: Perl / CGI Question / Apache <noreply@gunnar.cc>
Program optimisation advice for newbie <wolf_gore@yahoo.com.au>
Re: Program optimisation advice for newbie <amead@comcast.net>
Re: sort directory help <jgibson@mail.arc.nasa.gov>
Re: web based bibliography creation by users <goedicke@goedsole.com>
Re: Why does this work? <ahamm@mail.com>
Re: Why does this work? <ahamm@mail.com>
Re: Why does this work? <ahamm@mail.com>
Re: Why does this work? <ahamm@mail.com>
Re: Why does this work? <abigail@abigail.nl>
Re: Why does this work? <ahamm@mail.com>
Re: Why does this work? <matthew.garrish@sympatico.ca>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 3 Jan 2005 16:33:17 -0700
From: "Shawn" <spedwards@x.qwest.net>
Subject: file rename help
Message-Id: <iDkCd.32$od2.19979@news.uswest.net>
Hello,
I am very novice as you will see in my Perl skills. I thought I'd write a
simple script to rename a list of files in a directory
based on some validation checks. The first checking if the file is empty
and the 2nd checking if there is the character /
in a certain position in the file. The script is below
My first problem when I was testing this was the script seeing the file with
data even though I had created a zero byte file.
So, regardless of the file size it found it with data. Which leads me to
believe I'm not checking the file size correctly.
The second problem being the renaming of the file. I am receiving a
insufficient arguments error which has stumped me.
In this case, I'm guessing maybe I have a error in the FILE_LIST?
Any suggestions would be greatly appreciated.
Thanks.
#!/usr/local/bin/perl5.6
use lib "lib";
use MIME::Lite;
open(FILE_LIST,"ls f* | grep -v txt|" ) || die "Cannot do \n" ;
while ($cfile = <FILE_LIST>) {
if ( -z "$cfile" ) {
system("/bin/mv $cfile $cfile.zero.txt");
print "$cfile has zero data\n";
}
else {
print "$cfile has data\n";
}
$charcount = system("cut -c9 $cfile |sort -u |grep '/'
|wc -l");
if ( $charcount > 0 ) {
chomp($csv_client_id = `cut -c1-6 $cfile |sort -u`);
print "character found for $cfile"\n" ;
system("/bin/mv $cfile
${cfile}.char.txt");
else {
system("/bin/mv $cfile
${cfile}.nochar.txt");
}
}
------------------------------
Date: Tue, 4 Jan 2005 11:40:03 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: file rename help
Message-Id: <33u705F44rblcU1@individual.net>
Shawn wrote:
> Hello,
>
> I am very novice as you will see in my Perl skills.
Yes, there's a number of things you could improve, but I won't berate you
for that! Let's just deal with your current issues and trust that you will
learn in time to write nicer Perl code :-)
However, before you go much further, it is HIGHLY recommended that you
start all scripts with:
#!/usr/bin/perl.....
use strict;
use warnings;
## no warnings 'uninitialized';
The 3rd commented-out line is a personal favourite of mine, but that's
because I am not afraid of undef values, and the warnings about undef
values usually irritate me far more than coding for them. However, I do
not recommend you use my 3rd line until you are far more experienced. In
fact, you would get a valuable clue about your problem from "use
warnings", and my 3rd line would promptly take away that valuable warning.
So - you've been .... warned. Get tough on yourself with the first two
lines, and maybe (only maybe) consider the 3rd line when you have a lot of
experience.
> My first problem when I was testing this was the script seeing the
> file with data even though I had created a zero byte file.
> So, regardless of the file size it found it with data. Which leads
> me to believe I'm not checking the file size correctly.
There are better ways to get a list of files, but telling you this won't
explain why you are getting a failure right now, so I'll let you get away
with the "ls f* | grep -v |"
When you get the name of the file with
$cfile = <FILE_LIST>
The name of the file will contain a newline character at the end of the
file. So you may think that $cfile contains something like "f01234" but
really it contains "f01234\n". You need to clean up that trailing newline,
and the usual way is:
while($cfile = <FILE_LIST>) {
chomp $cfile;
So - your filename is currently wrong, and thus the -z test returns a
failure code. Specifically it returns undef. The return value for an
accessible empty file is 1, and the return code for an accessible
non-empty file is 0. In an if( ) test, undef basically behaves as if it
was 0. Therefore it's going to the else part.
NOTE: with "use warnings", you'd get a warning that undef has been
returned, and that would be your clue I mentioned above.
> The second problem being the renaming of the file. I am receiving a
> insufficient arguments error which has stumped me.
> In this case, I'm guessing maybe I have a error in the FILE_LIST?
I'm a bit stumped about this. I can't see where the "insufficient
arguments" might be comeing from; I expect that the /bin/mv is
complaining? Anyway, it's probably time to start discussing some of the
other things wrong with your first attempt; it will be easier in the long
run.
Instead of using the FILE_LIST handle, perl can do a lot of the work for
you:
while($cfile = <f*>) {
# no need to chomp; the <f*> creates a readable list of filenames
# and the names returned are "clean"
# skip filenames ending with .txt (my guess:-)
next if $cfile =~ /\.txt$/;
instead of using system, you can use perl's rename function:
rename $cfile, "$cfile.zero.txt";
Instead of using the system( ) to invoke cut and sort and grep and wc, you
could do it in perl. Apart from the efficiencies of not having to invoke
other processes, there will other performance benefits since it's quite
likely that the search for a / will be found much earlier than reading the
entire file. Therefore some perl code can terminate the search earlier
than the entire cut command line you are using
NOTE: even as a piece of shell code, your command is wasting at least two
processes. I'd be inclined to write
cut -c9 filename | grep -c '/'
if i was writing this in shell script. It would be sufficient. The sort
does not help, and the use of wc is pointless when grep can also do a
count.
a perlish search method:
open IFD, "<", $cfile or die ......
my $found = 0;
while(<IFD>) {
if(substr($_, 8, 1) eq "/") {
$found = 1;
last; # break out of the loop
}
}
close IFD;
if($found) .....
> chomp($csv_client_id = `cut -c1-6 $cfile |sort -u`);
I do not see where you are using the $csv_client_id, but I guess you are
building up a script. You should be able to extract this value in the loop
I have written above. If the csv_client_id could be anywhere in the file,
then perhaps the "last" command I have used in the loop should be removed.
> system("/bin/mv $cfile ${cfile}.char.txt");
> [SNIP]
> system("/bin/mv $cfile ${cfile}.nochar.txt");
once again, use the built-in rename function.
Also, you do not need ${cfile}.nochar.txt when $cfile.nochar.txt will do.
It's conventional practice not to overdo the use of { } unless it's really
necessary, for example "${cfile}blahblah"
------------------------------
Date: Tue, 4 Jan 2005 11:44:24 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: file rename help
Message-Id: <33u78aF451becU1@individual.net>
Andrew Hamm wrote:
>
>> The second problem being the renaming of the file. I am receiving a
>> insufficient arguments error which has stumped me.
>> In this case, I'm guessing maybe I have a error in the FILE_LIST?
>
> I'm a bit stumped about this. I can't see where the "insufficient
> arguments" might be comeing from; I expect that the /bin/mv is
> complaining?
Oh yes - I can see where the "insufficient arguments" is coming from, and
it is indeed coming from mv. The string you are passing to system is
effectively
/bin/mv f01234\n f01234\n.zero.txt"
so that's three lines of shell script:
/bin/mv f01234
f01234
.zero.txt
Clearly wrong. If you had used chomp to clean up the filename then the
system( ) call would have worked without errors.
------------------------------
Date: Tue, 4 Jan 2005 14:38:14 +1300
From: "Peter Sundstrom" <peter@invalid.invalid>
Subject: Re: file rename help
Message-Id: <33ua8tF428dnkU1@individual.net>
"Shawn" <spedwards@x.qwest.net> wrote in message
news:iDkCd.32$od2.19979@news.uswest.net...
> Hello,
>
> I am very novice as you will see in my Perl skills.
You're not joking. Obviously your Unix scripting skills and programming
logic are also at a very novice level.
Your Perl script does 11 unnecessary forks. Very wasteful and inefficient.
Either write Perl or write a shell script, but don't end up wrapping a shell
script in Perl.
> I thought I'd write a simple script to rename a list of files in a
> directory
> based on some validation checks. The first checking if the file is empty
> and the 2nd checking if there is the character /
> in a certain position in the file.
Given that a / is not a valid character for a Unix filename, then why are
you checking for it?
>The script is below
>
> #!/usr/local/bin/perl5.6
> use lib "lib";
> use MIME::Lite;
Why are you using MIME::Lite when you never reference it?
>
> open(FILE_LIST,"ls f* | grep -v txt|" ) || die "Cannot do \n" ;
Perl has internal globbing. How useful is an error message that says
"Cannot do"
> while ($cfile = <FILE_LIST>) {
> if ( -z "$cfile" ) {
No need for quotes.
> system("/bin/mv $cfile $cfile.zero.txt");
Perl has a rename function.
> print "$cfile has zero data\n";
> }
> else {
> print "$cfile has data\n";
> }
>
> $charcount = system("cut -c9 $cfile |sort -u |grep '/'
> |wc -l");
Now the above line is just plain bizarre.
1. No need to fork off a whole bunch of external processes for this.
2. If you'd read the documentation for system, you'd realise the return
result is not want you expected.
3. No Unix filename is going to have a / in it.
4. Why are you sorting a single digit? Do you know that if you sort '5',
the sorted result is '5'?
> if ( $charcount > 0 ) {
See comments about result results from system.
> chomp($csv_client_id = `cut -c1-6 $cfile
> |sort -u`);
No need for external processes to get the first six characters of a string.
What's this thing you have with sorting a single piece of data?
> print "character found for $cfile"\n" ;
Well, you'll never get a / character in a file, so you'll never match the
condition.
> system("/bin/mv $cfile
> ${cfile}.char.txt");
>
> else {
> system("/bin/mv $cfile
> ${cfile}.nochar.txt");
> }
> }
See rename comments.
------------------------------
Date: Tue, 04 Jan 2005 03:07:46 GMT
From: Jim Keenan <jkeen_via_google@yahoo.com>
Subject: Re: file rename help
Message-Id: <6MnCd.9967$PY6.1618@trndny02>
Shawn wrote:
> Hello,
>
> I am very novice as you will see in my Perl skills. I thought I'd write a
> simple script to rename a list of files in a directory
> based on some validation checks. The first checking if the file is empty
> and the 2nd checking if there is the character /
> in a certain position in the file. The script is below
>
Other posters have commented on specific errors in your Perl code and on
the inefficiencies of mixing Perl with system calls. Since you
obviously do know something about the shell, you're not a complete
novice. Given that fact, you would benefit from systematically working
your way through an introductory level Perl textbook. For example,
Randal L Schwartz's "Learning Perl" (3rd ed., O'Reilly) covers many of
the topics you had trouble with in Chapters 11-13. If you do that, you
will quickly build on your pre-existing technical skills.
Jim Keenan
------------------------------
Date: Mon, 3 Jan 2005 22:55:08 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: inheritance and order of packages
Message-Id: <vsoCd.11284$P%3.965588@news20.bellglobal.com>
<ioneabu@yahoo.com> wrote in message
news:1104784431.388677.90940@c13g2000cwb.googlegroups.com...
>I think I am getting the hang of the concept. This is another example
> that was in question, but I think it's the same idea. Does this look
> right? Before putting in the BEGIN block, @init was recognized as a
> lexical but the data was not there when the constructor made use of it.
> The BEGIN block solved the problem just like in the previous example.
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $a = one->new;
> print $a->to_string(),"\n";
>
> package one;
> my @init;
> BEGIN
> {
> @init = (one=>1,two=>2);
> }
> sub new
> {
> my $class = shift;
> my $self = { @init };
> bless($self, $class);
> return $self;
> }
> sub to_string
> {
> my $self = shift;
> join '', map {"$_ = ".$self->{$_}."\n"} keys %{$self};
> }
>
>
I suppose that's another way around the problem (although a very convoluted
way to make a hash ref, and not a particularly good use of a begin block).
You could just define $self when you create the object:
sub new
{
my $class = shift;
my $self = { one=>1, two=>2 };
bless($self, $class);
return $self;
}
or even:
sub new
{
my @init = (one=>1,two=>2);
my $class = shift;
my $self = { @init };
bless($self, $class);
return $self;
}
Although I personally don't like the latter. If you want to avoid these
problems altogether, I would suggest bundling the package code off into a
separate .pm file and "use" that (not with an all-lowercase name like you
have, however, as those are reserved for pragmatic modules). The code will
get compiled before your program is run, so you won't need to modify your
code depending on which package is defined first in the file.
Matt
------------------------------
Date: 3 Jan 2005 20:42:05 -0800
From: ioneabu@yahoo.com
Subject: Re: inheritance and order of packages
Message-Id: <1104813725.627616.31350@f14g2000cwb.googlegroups.com>
This was a simplified version of what I was playing around with. I am
trying out a class that generates a table of html widgets to be used
with CGI.pm and I thought that putting a large amount of initialization
data inside of new didn't look right. I am probably going to end up
using a separate sub to initialize either from user supplied arguments
or from a file. I'll definitely put my classes in separate files in
the end, but this is a good way to test them at first.
wana
------------------------------
Date: 3 Jan 2005 15:12:15 -0800
From: per.hallkvist@home.se (phallk)
Subject: Language environment in perl; how to open a file with filename coded in IBM850
Message-Id: <b1238796.0501031512.6a2d7aa5@posting.google.com>
Hi,
any character-set guru out there who can assist me???
I am accessing my Windows-XP-filesystem with samba from my Linux
machine. This works fine as long as there are no Swedish characters in
the filenames but becomes slightly complex (for me at least) when
international characters starts to pop up in the filenames. It seems
like ibm850 encoding is used because doing a "ls" as below works fine:
use encoding "Latin-1";
use open IO => ":encoding(ibm850)"
open (LS,"ls --show-control-chars /mnt/e|") or die $!;
while ($_=<LS>) {
chomp;
print "Fname: /mnt/e/".$_;
}
When reading the directory as above all filenames are displayed
correctly with international characters. But now to my problem:
How do I open a file. Assume the correct filename is stored in $_:
open (FILE,$_);
...will not work as the filename is encoded in UTF8.
This is probably simple, but I have spent quite some time on this.
Thanks in advance.
/Per Hallkvist
------------------------------
Date: Tue, 04 Jan 2005 01:16:37 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Perl / CGI Question / Apache
Message-Id: <33u5n2F42phu2U1@individual.net>
John D. Sanders wrote:
> I have an Apache Web Server running and I want to use Perl as my CGI
> language.
You are multi-posting AGAIN!!!
http://groups-beta.google.com/group/comp.lang.perl.misc/msg/ae2676687e87fa38
Even if ciwac is a more appropriate group for your problem than clpmisc
and clpmodules, how about following up the responses you got there
before you post in some other group?
You'd better start respecting the netiquette, or else many people will
become disinclined to help you.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
PLEASE NOTE: comp.infosystems.www.authoring.cgi is a
SELF-MODERATED newsgroup. aa.net and boutell.com are
NOT the originators of the articles and are NOT responsible
for their content.
HOW TO POST to comp.infosystems.www.authoring.cgi:
http://www.thinkspot.net/ciwac/howtopost.html
------------------------------
Date: 3 Jan 2005 19:55:33 -0800
From: "ChocMonk" <wolf_gore@yahoo.com.au>
Subject: Program optimisation advice for newbie
Message-Id: <1104810933.205335.146810@c13g2000cwb.googlegroups.com>
Hello,
I've got a program snippet that takes a multiline string for exapmple:
"asdasdasd@@\main\3
boohoo@@\main\6
toodle@@\main\9"
and forms a hash from it, where each key is the part of each line upto
the first "@" and the value is everything following the second "@". So
the hash key/value pairs for the above string look like:
asdasdasd => \main\3
boohoo => \main\6
toodle => \main\9
Here is the snippet I wrote:
----BEGIN CODE--
$descr = "
asdasdasd@@\main\3
boohoo@@\main\6
toodle@@\main\9";
#grab the filename list
(my $crap, my $cset) = split /names/,$descr,2;
#split each line into an array
my @temp1 = split /\n/,$cset;
#chuck the list into a hash
my %names;
foreach (@temp) {
(my $name, my $ver) = split /\@\@/,$_;
$names{$name} = $ver;
}
----END CODE--
Is this a good way to do it? Any comments on making it more
efficient/elegant would be much appreciated.
Cheers,
Choco
------------------------------
Date: Mon, 03 Jan 2005 22:19:08 -0600
From: Alan Mead <amead@comcast.net>
Subject: Re: Program optimisation advice for newbie
Message-Id: <pan.2005.01.04.04.19.06.582766@comcast.net>
Star date: Mon, 03 Jan 2005 19:55:33 -0800, ChocMonk's log:
> Here is the snippet I wrote:
> ----BEGIN CODE--
> $descr = "
> asdasdasd@@\main\3
> boohoo@@\main\6
> toodle@@\main\9";
>
> #grab the filename list
> (my $crap, my $cset) = split /names/,$descr,2;
> #split each line into an array
> my @temp1 = split /\n/,$cset;
>
> #chuck the list into a hash
> my %names;
> foreach (@temp) {
> (my $name, my $ver) = split /\@\@/,$_;
> $names{$name} = $ver;
> }
This is your actual code? I don't follow what you're doing in the second
chunk (where you "grab the filename list") and it doesn't look like it
makes sense... (where does the 'names' string appear in your input?)
Maybe this is immaterial but I'd encourage you to cut-and-paste rather
than re-type (if you did retype).
I'm also not seeing why you create a variable @temp1 and then loop over
@temp... If this is a typo, then a STRONGLY encourage you to 'use strict;'
at the top of your script. Also 'use warnings;'.
In fact, even if it isn't a typo, I'd have the same advice.
Ok, now on to answering your question slightly... I guess it's a good
enough way. If the list is long, then you might want to use a regular
expression with the ... IIRC 'g' and 'm' options to successively capture
the lines (from memory):
my %names=();
do {
$cset =~ /$(.+?)\@\@(.+?)$/gm;
$names{$1} = $2 if (defined($1));
} until (!defined($1));
This uses a regex rather than the split.. don't know which is faster but
as written, you use twice the memory needed (because you have the string
of lines and then store the lines in a list). Even better would be to
read the lines one at a time from a file but I assume you cannot do this...
Also, you shouldn't explicitly use variables like $_ (IMHO) like you do in
the last split, I would write
my($name,$ver) = split /\@\@/;
or
foreach my $t (@temp) {
my($name,$ver) = split(/\@\@/,$t);
...
-Alan
------------------------------
Date: Mon, 03 Jan 2005 17:52:57 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: sort directory help
Message-Id: <030120051752577832%jgibson@mail.arc.nasa.gov>
In article <Xns95D3A03BCE52Efrizopatcharterdotco@216.77.188.18>, Nathan
<frizop-manchu@charter.net> wrote:
> http://search.cpan.org/~muir/Time-modules-2003.1126/lib/Time/ParseDate.pm
>
> might help you, or you could parse it yourself.
>
> --Nathan
>
> "Jake Wiley" <OEKilla@msn.com> wrote in news:1104788305.272941.63060
> @c13g2000cwb.googlegroups.com:
>
> > I need to sort a directory which is all fine BUT my directory names are
> > all in date formats like Nov 18 2004, Dec 23, 2005 etc.. The simple
> > sort works BUT like this:
> > Aug 5 2004
> > Dec 1 2004
> > Dec 8 2004
> > Jan 2 2005
> > Nov 7 2004
Like this:
#!/usr/local/bin/perl
#
use strict;
use warnings;
use Time::ParseDate;
my @dates = <DATA>;
my @sorted =
map{ $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, parsedate($_)] } @dates;
print @sorted;
__DATA__
Aug 5 2004
Dec 1 2004
Dec 8 2004
Jan 2 2005
Nov 7 2004
__OUTPUT__
Aug 5 2004
Nov 7 2004
Dec 1 2004
Dec 8 2004
Jan 2 2005
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
------------------------------
Date: Tue, 04 Jan 2005 01:02:07 GMT
From: William Goedicke <goedicke@goedsole.com>
Subject: Re: web based bibliography creation by users
Message-Id: <m33bxiovlj.fsf@smtp.comcast.net>
Dear Ted -
>>>>> "Ted" == tpederse <tpederse@gmail.com> writes:
Ted> I would like to have a web page (prefer the use of cgi) where
Ted> users can enter information for a bibliography [...] and then
Ted> have that updated online at my web site so other users can
Ted> see it and access. I am sure I should have some ability to
Ted> approve or delete entries that are submitted. Is anyone
Ted> aware of a Perl module with these capabilities?
As Matt suggested (and you concurred) this is more an application than
a module. I suggest you look at the Content Management Systems like
plone (my personal favorite). Plone is Zope and therefore python based
while you'ld probably prefer perl but, the functionality is so high you
may find it adequate.
A quick google search (i.e. perl cms) found several options of which
http://www.callistocms.com/ looked the most viable to me (but nowhere near
as mature as plone).
You could also consider wiki or blog applications but, they're less
geared towards approval of content. See everything2 and livejournal.
- Billy
============================================================
William Goedicke goedicke@goedsole.com
Cell 617-510-7244 http://www.goedsole.com:8080
============================================================
Lest we forget:
We wouldn't need tax laws if everyone was like your
mother, because open barrals at town hall would be
adequate to collect the necessary funds.
- William Goedicke Sr.
------------------------------
Date: Tue, 4 Jan 2005 10:53:10 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Why does this work?
Message-Id: <33u488F450l74U1@individual.net>
Bart Lateur wrote:
>
> You're missing the point.
>
> use strict; print -force
>
> prints:
>
> -force
>
> both in 5.6.1 as in 5.8.3. For some reason, strict seems to be too
> relaxed here. Maybe that is by design too, to allow for emulation of
> named parameters more easily.
No, the point being missed is that
-identifier
is a string literal. Literally. It's just another form of "-identifier".
Strings do not have to be represented only by wrapping them in quotes.
------------------------------
Date: Tue, 4 Jan 2005 10:57:20 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Why does this work?
Message-Id: <33u4g1F43n7etU1@individual.net>
Bart Lateur wrote:
>>
>> Unary "-" performs arithmetic negation if the operand is
>> numeric. If the operand is an identifier, a string con
>> sisting of a minus sign concatenated with the identifier
>> is returned. Otherwise, if the string starts with a plus
>> or minus, a string starting with the opposite sign is
>> returned. One effect of these rules is that -bareword is
>> equivalent to "-bareword".
>
> I don't read it that way. For what I see,
I can see how the statement could be called ambiguous:
"if the operand is an identifier, a string consisting of a minus sign
concetenated with the identifier is returned"
'returned' implies to some minds that a runtime activity is going on. But
actually, it's really implying that the lexer is "returning" a string that
has value "-identifier".
Perhaps this paragraph could benefit from some extremely careful editing.
Still, -identifier is definitely by design, and very useful.
------------------------------
Date: Tue, 4 Jan 2005 11:06:52 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Why does this work?
Message-Id: <33u51vF43nu9uU1@individual.net>
Brian McCauley wrote:
>
> sub foo { 'bar' };
> print -foo;
>
> The above prints '-bar' not '-foo' and also emits a warning even
> without "use warnings" in effect.
yeah, it says:
Ambiguous use of -foo resolved as -&foo() at ......
because it is, unfortunately, ambiguous. Because the compiler makes a
decision on your behalf, it MUST issue a warning when it could so easily
be intended that a literal "-foo" is desired. This Is A Good Thing, this
un-asked-for warning.
Change that line to
print - foo;
and the ambiguity goes away, and so does the warning.
Just because you can ask for more warnings with -w or "use warnings" does
not imply that there should be a complete absence of warnings when you
don't ask for them.
------------------------------
Date: Tue, 4 Jan 2005 11:09:39 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Why does this work?
Message-Id: <33u574F430reeU1@individual.net>
Brian McCauley wrote:
>
> Now first let's be 100% sure we know what feature we are talking about
> here. The feature that is the pimary topic of this thread, and the
> one I am talking about is the fact that the -bareword construct is
> exempt from 'strict subs'. If you are talking about any other
> feature then you are in the wrong thread.
Yes - everyone is talking about that. All will agree.
The important point to note is that BY DESIGN,
-identifier
is another form of string literal. It's just as "by design" as "string"
'string' qq(string) and <<TOKEN ..... TOKEN
-identifier is a string literal. By Design.
Because it *can* be ambiguous IF a sub called identifier is designed, the
builders of the parser have very thoughtfully given us a free warning that
will probably save your bacon one day.
------------------------------
Date: 04 Jan 2005 00:34:21 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Why does this work?
Message-Id: <slrnctjp4d.h3.abigail@alexandra.abigail.nl>
Andrew Hamm (ahamm@mail.com) wrote on MMMMCXLIV September MCMXCIII in
<URL:news:33u51vF43nu9uU1@individual.net>:
%% Brian McCauley wrote:
%% >
%% > sub foo { 'bar' };
%% > print -foo;
%% >
%% > The above prints '-bar' not '-foo' and also emits a warning even
%% > without "use warnings" in effect.
%%
%% yeah, it says:
%%
%% Ambiguous use of -foo resolved as -&foo() at ......
%%
%% because it is, unfortunately, ambiguous. Because the compiler makes a
%% decision on your behalf, it MUST issue a warning when it could so easily
%% be intended that a literal "-foo" is desired. This Is A Good Thing, this
%% un-asked-for warning.
No, it's not. Unasked for warnings are as welcome as unasked for system
crashes.
%% Change that line to
%%
%% print - foo;
%%
%% and the ambiguity goes away, and so does the warning.
%%
%% Just because you can ask for more warnings with -w or "use warnings" does
%% not imply that there should be a complete absence of warnings when you
%% don't ask for them.
<rant>
Yes, there should, IMO. I really, really loathe warnings I can't turned
off. It's so unperl. *I*, the programmer, and only *I* should be in
control - and never ever the compiler/language.
If I don't want warnings, that damnit, I should not get warnings. Ever.
If I turn warnings off, then it's because Perl is wrong about the
warnings it issues. If Perl isn't getting instructions to talk, it
should keep silent. In the words of London.pm, Perl is my bitch -
not the other way around.
Mandatory warnings are evil. It takes choice away from the programmer.
</rant>
Abigail
--
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
------------------------------
Date: Tue, 4 Jan 2005 11:55:28 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Why does this work?
Message-Id: <33u7t2F45eu78U1@individual.net>
Abigail wrote:
> %%
> %% because it is, unfortunately, ambiguous. Because the compiler
> makes a %% decision on your behalf, it MUST issue a warning when it
> could so easily %% be intended that a literal "-foo" is desired.
> This Is A Good Thing, this %% un-asked-for warning.
>
> No, it's not. Unasked for warnings are as welcome as unasked for
> system crashes.
well, this made me chuckle. I won't try to convince you otherwise. Hell,
why not - this is c.l.p.misc and discussion descending into flames is par
for the course. Let's see if we can at least stay above flames even if we
don't end up agreeing...
I think this warning is borderline as an error. Perhaps you'd feel more
confident if this compiler error was promoted to a compilation-halting
error? That way there'd be one less "unwanted warning". I expect the
author/designer of this warning figured that the programmer could benefit
from allowing the script to proceed since they will be able to
disambiguate the statement the next time they edit. I for one would
appreciate the opportunity to find more than one error as I'm developing a
script.
the case
sub foo { .... }
print -foo;
unfortunately leads to an ambiguity, so the only ultimate answer is to
remove the ambiguity in the Perl syntax? but then, would it still be Perl?
I'm hard-pressed to think of any unwanted warnings from Perl that
persistently shit me.
> Yes, there should, IMO. I really, really loathe warnings I can't
> turned off. It's so unperl. *I*, the programmer, and only *I* should
> be in control - and never ever the compiler/language.
Sometimes the compiler needs your help to understand your code. In the
sample given,
sub foo { ... }
print -foo;
how pissed off would you be if you REALLY wanted it to mean
print "-foo";
But when you see this friendly warning, you can edit to correct. If you
really meant "-foo" then I suppose there are grounds to whinge about being
unable to use the -foo literally. I can agree with that...
Damn. A grey area in Perl. Who'd have thunk it?
------------------------------
Date: Mon, 3 Jan 2005 23:15:52 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Why does this work?
Message-Id: <XLoCd.11322$P%3.976052@news20.bellglobal.com>
"Andrew Hamm" <ahamm@mail.com> wrote in message
news:33u7t2F45eu78U1@individual.net...
>
> I'm hard-pressed to think of any unwanted warnings from Perl that
> persistently shit me.
>
If you want to sound like a number of other chuckle-heads that post here
feel free to continue swearing, but it doesn't reflect well on you when you
do.
Matt
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 7600
***************************************