[23696] in Perl-Users-Digest
Perl-Users Digest, Issue: 5903 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 6 11:05:49 2003
Date: Sat, 6 Dec 2003 08:05:07 -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, 6 Dec 2003 Volume: 10 Number: 5903
Today's topics:
Re: any idea how to optimize this regex? <abigail@abigail.nl>
Re: gui in perl <stjm2@cam.ac.uk.remove>
Re: How to open a file from the end and read the last 1 (Sara)
Re: How to write to drive A:\ from CGI Perl <jurgenex@hotmail.com>
Re: Idiom for array index that I'm foreach'ing over? <usenet@morrow.me.uk>
Re: Idiom for array index that I'm foreach'ing over? <tassilo.parseval@rwth-aachen.de>
Re: Idiom for array index that I'm foreach'ing over? <dmcbride@naboo.to.org.no.spam.for.me>
Overloading <usenet@morrow.me.uk>
q on 'record', 'field' separators in text files <nertz@numb.no>
Re: reading binary files (Greg Bacon)
Re: reading binary files <krahnj@acm.org>
Re: reading binary files <krahnj@acm.org>
Re: reading binary files (Zoran)
Re: reading binary files <usenet@morrow.me.uk>
Re: reading binary files (Greg Bacon)
Re: What is anonymous sub? Why is it better? <tassilo.parseval@rwth-aachen.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 06 Dec 2003 15:41:54 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: any idea how to optimize this regex?
Message-Id: <slrnbt3u62.eu.abigail@alexandra.abigail.nl>
Ben Morrow (usenet@morrow.me.uk) wrote on MMMDCCXLIX September MCMXCIII
in <URL:news:bqrikr$dqs$1@wisteria.csv.warwick.ac.uk>:
:}
:} abigail@abigail.nl wrote:
:} >
:} > That fails on:
:} >
:} > <!--> Comment one <!----> Comment two <!-->
:}
:} Ouch! I had to think *quite* hard to convince myself that '<!-->
:} Comment one <!---->' isn't a valid comment... it's obvious once you
:} see the symmetry of it, of course.
:}
:} OK, making a small modification and applying the grammar fragment Tad
:} gave:
:}
:} my $com = qr/-- (?: (?!--) . )* --/x;
:} /<! (?: $com (?: \s* | $com )* )? >/x;
Why not just:
/<! (?: $com \s*)* >/x;
Abigail
--
INIT {print "Perl " }
BEGIN {print "Just " }
CHECK {print "another "}
END {print "Hacker\n"}
------------------------------
Date: Sat, 06 Dec 2003 12:21:34 +0000
From: Stuart Moore <stjm2@cam.ac.uk.remove>
Subject: Re: gui in perl
Message-Id: <bqshke$1nd$2@pegasus.csx.cam.ac.uk>
Having used wxWindows/perl for a not too complicated GUI, we found it so
slow and memory hogging that we ended up scrapping it, learning C++ from
scratch, doing the GUI in wxWindows (using sockets to communicate with
perl scripts where we needed to) and were very happy with our decision.
Maybe the other bindings were better, but our conclusion was Perl just
wasn't up to the task of getting something sensibly sized and not
requiring a state of the art machine to be responsive.
Stuart
------------------------------
Date: 6 Dec 2003 07:13:55 -0800
From: genericax@hotmail.com (Sara)
Subject: Re: How to open a file from the end and read the last 100 lines
Message-Id: <776e0325.0312060713.3d33eb35@posting.google.com>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<bqs8md$hfo$1@mamenchi.zrz.TU-Berlin.DE>...
> Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
> > rui.vilao@rocketmail.com (Rui Vilao) wrote in message
> > news:<385a715f.0312041004.71e2d777@posting.google.com>...
> > > Greetings,
> > >
> > > I am writing some Perl script to check some patterns errors in an
> > > alert log (Oracle alert log). The file can't get quite large.
> > > Therefore I would like write a Perl script that runs every day via at
> > > and opens the file from the end to read the last 100 lines. How can I
> > > do this?
> > > Any help/suggestion is highly appreciated.
> > >
> > > Thanks in advance for your help,
> > >
> > > Kind Regards,
> > >
> > > Rui Vilao
> >
> >
> > Good Day Rui:
> >
> > There are some modules in CPAN that would be useful - but it's more
> > fun to roll your own! You DO say that the file CAN'T get quite large,
> > which is curious because my logs DO get quite large here on thie RH8
> > server. But if it's small why not just:
> >
> > die "I hate MONDAYS!\n" unless open F, 'log';
> > my @l = <F>;
> > close F;
> > @l = splice @l, @l-100;
> >
> > and botta bing you have your last 100 lines!
>
> ...except when the file has fewer than 100 lines, in which case a fatal
> run-time error results. Uri's File::ReadBackwards deals correctly with
> that case.
>
> This is a good demonstration why "rolling your own" is a bad idea, even
> if the problem looks trivial.
>
> Anno
Oh yes a small mod for that trivial case:
splice @l, @l-100 if @l > 100;
Oh no NEVER roll your own- never think about solving a problem for
yourself; always look for someone else's solution. You'll really get
to be a great programmer that way..
G
------------------------------
Date: Sat, 06 Dec 2003 11:34:59 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How to write to drive A:\ from CGI Perl
Message-Id: <D9jAb.2618$lR3.704@nwrddc01.gnilink.net>
Cle wrote:
Maybe I don't understand your question, but it seems as if you are
contradicting yourself (see below).
> Please show me how to write an output.txt file from CGI perl sript to
> drive A or C?
Here you are asking how to write to a file using Perl.
> I could access drive A by HTML codes,
Side note: Not that this has anything to do with Perl, but how do you do
that? I was under the impression that HTML is a Text Markup Language and I
wasn't aware that a markup language can 'access' floppy drives. Well, maybe
as a hyperlink that is pointing to another file on the floppy, but you are
talking about writing to files.
> and I know how to write to a
> file from CGI Perl script.
And here you are saying you know how to write to a file using Perl. So why
are you asking about how to write to a file?
> However, a Perl book shown the same thing
> for CGI Perl script that doesn't work.
The meaning of this sentence eludes me
jue
------------------------------
Date: Sat, 6 Dec 2003 12:53:52 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <bqsjh0$t02$1@wisteria.csv.warwick.ac.uk>
Michele Dondi <bik.mido@tiscalinet.it> wrote:
> >What's so unbelievable about it?
>
> Hmmm... maybe that I didn't think it was possible?!?
As a general rule, Perl *completely* ignores whitespace. The only
exception I know of is when you have two sequences of \w next to other
which should be interpreted as separate identifiers: ws is required
then.
> On 5 Dec 2003 11:02:11 GMT, "Tassilo v. Parseval"
> <tassilo.parseval@rwth-aachen.de> wrote:
>
> >Well, you can do funny things in Perl, particularly with whitespaces:
> >
> > ethan@ethan:~$ perl
> > $
> > # comment
> > bla
> >
> >
> > = 5
> > ;
> >
> > print $bla;
> > __END__
> > 5
>
> I didn't expect this to be possible either. Funny!
Comments are whitespace.
> The trick doesn't always work, anyway:
>
> # perl -le 'print $i ++ for 1..3'
> syntax error at -e line 1, near "++ for "
> Execution of -e aborted due to compilation errors.
perl -le'print STDOUT $i ++ for 1..3'
> # perl -le 'print($i ++) for 1..3'
> syntax error at -e line 1, near "++ for "
> Execution of -e aborted due to compilation errors.
perl -le'print (STDOUT $i ++) for 1..3'
perl -le'print STDOUT ($i ++) for 1..3'
The problem here is that the rule for parsing an indirect object is
very greedy: as soon as it has something that could be interpreted as
one, it grabs it. So 'print $i ++' is interpreted trying to print '++'
to the filehandle '$i': the '++' is then a syntax error. Adding the
brackets makes no difference, *even* if they have whitespace before
them (I keep getting bitten by this :).
> # perl -le 'print +($i ++) for 1..3'
perl -le'print +$i ++ for 1..3'
> # perl -le 'print do{$i ++} for 1..3'
An indirect object cannot start with '+', or with 'do', so there's no
problem here. See perlobj/"Indirect Object Syntax".
> Could you please be so kind and explain me what perl understands in
> the respective cases? In particular I am not puzzled by the fact that
> the first one fails and the last one succeeds, but I can't understand
> why the second one does not.
I was initially puzzled as to why *any* of them failed...
> As a side (minor) note, by the examples above one sees that even if
> the postfix form of the auto increment operator should *increment* the
> variable after returning its value, in practice it *modifies* its
> value before returning it:
>
> # perl -le "print $i; print $i++"
>
> 0
>
> Also, it doesn't treat explicitly of the case of undefined
> variables.
Err...
| "undef" is always treated as numeric, and in particular is changed
| to 0 before incrementing (so that a post-increment of an undef value
| will return 0 rather than "undef").
This is from perlop in 5.8.2.
Ben
--
For the last month, a large number of PSNs in the Arpa[Inter-]net have been
reporting symptoms of congestion ... These reports have been accompanied by an
increasing number of user complaints ... As of June,... the Arpanet contained
47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] * ben@morrow.me.uk
------------------------------
Date: 6 Dec 2003 13:45:20 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <bqsmhg$lso$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Ben Morrow:
> Michele Dondi <bik.mido@tiscalinet.it> wrote:
>> >What's so unbelievable about it?
>>
>> Hmmm... maybe that I didn't think it was possible?!?
>
> As a general rule, Perl *completely* ignores whitespace. The only
> exception I know of is when you have two sequences of \w next to other
> which should be interpreted as separate identifiers: ws is required
> then.
It's a little more complicated in fact. Here's one that Abigail
simply adores:
ethan@ethan:~$ perl -lw
print(1+2)*3;
Useless use of multiplication (*) in void context at - line 1.
3
ethan@ethan:~$ perl -lw
print (1+2)*3;
print (...) interpreted as function at - line 1.
Useless use of multiplication (*) in void context at - line 1.
3
The additional whitespace in the second example will trigger an
additional warning.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Sat, 06 Dec 2003 15:39:36 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <YKmAb.576666$6C4.147743@pd7tw1no>
Tim Shoppa wrote:
> In article <x7u14gz73n.fsf@mail.sysarch.com>, Uri Guttman says...
>>perl data structures use references which are not the same as
>>objects. to do a 1-1 mapping of two arrays is simple without
>>indexes. depending on where the data cam from you can just create a
>>array of arrays ( [ x, y ] coordinates ) or even add the time value as
>>the third array element. or you could do a simple hash like:
>>
>>{ x => 1.2,
>>y => 3.4,
>>time => 12335,
>>}
>>
>>and have an array or hash of those (not sure what the hash key would
>>be).
>
> All those are workable but IMHO a bit ugly compared to creating a
As I once heard, you can write Fortran in any language. The point is:
don't. The hash above is exactly what one should do in perl. If you
were in C, I would say put everything in a struct, and have an array of
those. In Perl, put everything in a hash, and have an array of refs to
those.
> "point" object that has x,y, and time methods. Your last suggestion
> comes close but I don't like all those curly brackets (again, my
> personal taste).
Sorry - if you don't like braces, perl is not for you. Perl is all
about simple, short, if someone obtuse, ways of saying things. Most of
the punctuation has meaning, and if you don't like it, you'll need to
find another language. Perhaps REXX may be more to your liking.
>> TS> If the program was bigger, I'd go the full object-oriented route
>> TS> with modules etc., but it seems silly to do this for such a tiny
>> TS> quick little thing.
>>
>>perl works fine with or without objects. get your mind out of that
>>fortran gutter where all you have are individual arrays that can only be
>>linked by their indexes. that is such a poor design and paradigm.
>
> But it's where my mind goes for any quick-and-dirty program.
In other words, you still are thinking Fortran rather than thinking
perl. To be honest, very few languages would encourage that type of
programming, so if you want to spread out past Fortran, I would
encourage you to embrace it.
------------------------------
Date: Sat, 6 Dec 2003 13:38:15 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Overloading
Message-Id: <bqsm47$149$1@wisteria.csv.warwick.ac.uk>
I've been playing with overloading, and the following is puzzling
me... the script
#!/usr/bin/perl -l
use warnings;
use strict;
{{
package I;
use overload
"0+" => sub {
my $s = shift;
my $v = $$s;
print "numify";
0+$v;
},
q{""} => sub {
my $s = shift;
my $v = $$s;
print "stringify";
"$v";
},
"=" => sub {
my $s = shift;
my $v = $$s;
print "copy";
my $t = $v;
bless \$t;
},
"-" => sub {
my $s = shift;
my $v = $$s;
my $a = shift;
print "sub($a)";
$$s = (shift) ? $v - $a : $a - $v;
};
sub new {
my $c = shift;
my $s = shift;
bless \$s, $c;
}
}}
my $i = new I "*";
print $i;
print int($i);
print $i--;
__END__
prints
stringify
*
numify
Argument "*" isn't numeric in addition (+) at ./op line 15.
numify
Argument "*" isn't numeric in addition (+) at ./op line 15.
0
sub(1)
Argument "*" isn't numeric in subtraction (-) at ./op line 35.
stringify
1
about which I have two questions:
1. Why is 'numify' called twice for int($i)?
2. Why is 'numify' not called at all before calling '-'? OK, I guess
that makes sense... but why, then, will '-' not autogenerate in
terms of 'numify'? Is there any way to call it, without giving the
sub a name (i.e., is it possible to simply 'numify' a scalar, as
"$s" will stringify)?
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@morrow.me.uk |----------------+---------------| The Levellers, 'Believers'
------------------------------
Date: Sat, 6 Dec 2003 08:58:52 -0600
From: sparkane <nertz@numb.no>
Subject: q on 'record', 'field' separators in text files
Message-Id: <MPG.1a3baa95baefbeb198968d@news-central.giganews.com>
What characters if any are typically used to delimit 'fields' and
'records' in a text file?
I'm working on a rewrite of an app that stores some data in text files
and uses '|' and "\n" as 'field' and 'record' separators, respectively.
I want to replace these and have been thinking of using "\e" and "\0",
respectively, to allow data in a field to use the current two
separators. Would it be ill-advised in particular to use \0? (The app
currently runs on *nix, in case this is important.)
thanks
spark
------------------------------
Date: Sat, 06 Dec 2003 12:01:13 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: reading binary files
Message-Id: <vt3h89q9g14g1f@corp.supernews.com>
In article <3fd1b263.172576@news.rwth-aachen.de>,
Zoran <Zoran.Marinkovic@post.rwth-aachen.de> wrote:
: I have problems with unpacking binary data(win98,activestate perl).
: The procedure is very simple:
: 1.Packing ascii data as binary data.
: 2.Unpacking binary data.
: 3.Printing the output.
: [...]
See below.
#! perl
use warnings;
use strict;
my @input = (
"0.1 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"0.2 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"1.1 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"1.5 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"2.1 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"2.3 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"2.5 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"3.1 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"3.2 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
"3.5 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0",
);
my $bin;
open $bin, ">", "bin.dat" or die "$0: open >bin.dat: $!\n";
binmode $bin;
my $line = 0;
for (@input) {
++$line;
my @data = split;
my($tag,$n) = splice @data, 0, 2;
unless (@data == $n) {
my $got = @data;
warn "$0: line $line: n = $n but $got field(s)\n";
next;
}
my $packed = pack "d I d$n", $tag, $n, @data;
print $bin length($packed), "!" => $packed;
}
close $bin or warn "$0: close bin.dat: $!\n";
sub check {
my $expect = shift;
my @data = split ' ', shift;
return unless @data == @$expect;
for (@$expect) {
my $datum = shift @data;
return unless $_ == $datum;
}
1;
}
sub getrec {
my $fh = shift;
my $bytes;
my $ch;
my $status = read $fh, $ch, 1;
if ($status == 0) {
return 0;
}
elsif (not defined $status) {
warn "$0: read: $!";
return;
}
while ($ch ne "!") {
last if $ch eq "!";
if ($ch =~ /\D/) {
warn "$0: unexpected '$ch'";
return;
}
$bytes .= $ch;
$status = read $fh, $ch, 1;
if ($status == 0) {
warn "$0: premature end-of-file";
return;
}
elsif (not defined $status) {
warn "$0: read: $!";
return;
}
}
unless (defined $bytes) {
warn "$0: number of bytes undefined";
return;
}
$status = read $fh, my($chunk), $bytes;
if ($status == 0) {
warn "$0: premature end-of-file";
return;
}
elsif (not defined $status) {
warn "$0: read: $!";
return;
}
elsif ($bytes != length $chunk) {
warn "$0: incomplete read";
return;
}
$chunk;
}
open $bin, "<", "bin.dat" or die "$0: open bin.dat: $!\n";
binmode $bin;
while (defined ($_ = getrec $bin)) {
last unless $_;
my @data = unpack "d I d*", $_;
my $input = shift @input;
if (check \@data, $input) {
print "[@data]:\n",
" MATCH\n";
}
else {
print "[$input]:\n",
" ERROR\n";
s/(.)/sprintf "%02x", ord $1/ges;
print " [$_]:\n",
" [@data]\n";
}
push @input, $input;
}
__END__
Hope this helps,
Greg
--
Nothing is so opportune for tyrants as a people tired of its liberty.
-- Alan Keyes
------------------------------
Date: Sat, 06 Dec 2003 12:26:27 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: reading binary files
Message-Id: <3FD1CACA.1B6E6A34@acm.org>
Zoran wrote:
>
> I have problems with unpacking binary data(win98,activestate perl).
> The procedure is very simple:
> 1.Packing ascii data as binary data.
> 2.Unpacking binary data.
> 3.Printing the output.
>
> This is my input file:
> 0.1 11 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
>
> [snip]
>
> This is the output:
> 0.1 0 0 0 0 0 0 0 0 0 0 0 0 0
>
> [snip]
>
> As you can see only the first numbers are correct.Maybe I am making
> mistakes ,when I try to pack the data in a special format
> @var= pack ("d I d11",$buf); .
>
> This is the code:
> ______________________________________________________
> print "\n ******************************\n";
>
> open (B ,"< stats.dat") or die "failed opening stats.dat \n";
> open (C ,"> bin.dat") or die "failed creating bin.dat \n";
You should include the $! variable in the error message so you know why
open() failed. Since you are running Windows you should use binmode()
on the C filehandle.
> while (my $buf = <B>)
> {
> chomp $buf;
> @var2 = pack("d I d11",$buf);
> print C "@var2\n";
> }
pack() takes a list and converts it to a string while unpack() takes a
string and converts it to a list. You are passing a list of one element
to pack. You need to separate the individual numbers into a list for it
to work correctly.
while ( <B> ) {
my $var2 = pack 'd I d11', split;
print C "$var2\n";
}
> close(C);
> close(B);
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 06 Dec 2003 12:34:59 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: reading binary files
Message-Id: <3FD1CCCB.6417B0A3@acm.org>
"John W. Krahn" wrote:
>
> Zoran wrote:
> >
> > while (my $buf = <B>)
> > {
> > chomp $buf;
> > @var2 = pack("d I d11",$buf);
> > print C "@var2\n";
> > }
>
> pack() takes a list and converts it to a string while unpack() takes a
> string and converts it to a list. You are passing a list of one element
> to pack. You need to separate the individual numbers into a list for it
> to work correctly.
>
> while ( <B> ) {
> my $var2 = pack 'd I d11', split;
> print C "$var2\n";
> }
Note that relying on a newline to delimit binary data makes no sense as
one of your 'd' or 'I' formatted numbers may contain the same bit
pattern as the "\n" character.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 06 Dec 2003 12:59:53 GMT
From: Zoran.Marinkovic@post.rwth-aachen.de (Zoran)
Subject: Re: reading binary files
Message-Id: <3fd1d111.1342929@news.rwth-aachen.de>
On Sat, 06 Dec 2003 12:34:59 GMT, "John W. Krahn" <krahnj@acm.org>
wrote:
>"John W. Krahn" wrote:
>>
>> Zoran wrote:
>> >
>> > while (my $buf = <B>)
>> > {
>> > chomp $buf;
>> > @var2 = pack("d I d11",$buf);
>> > print C "@var2\n";
>> > }
>>
>> pack() takes a list and converts it to a string while unpack() takes a
>> string and converts it to a list. You are passing a list of one element
>> to pack. You need to separate the individual numbers into a list for it
>> to work correctly.
>>
>> while ( <B> ) {
>> my $var2 = pack 'd I d11', split;
>> print C "$var2\n";
>> }
>
>Note that relying on a newline to delimit binary data makes no sense as
>one of your 'd' or 'I' formatted numbers may contain the same bit
>pattern as the "\n" character.
>
>
>
>John
>--
>use Perl;
>program
>fulfillment
Thank you very much!
I think I have managed it with your help.
Look at this:
@var2 = pack("d I d11",split(" ",$buf));
Finally my output file matches my input file,though the code
is not very elegant.
If I am using "use strict" the script won't even start and crushes
with warnings like this:
Possible unintended interpolation of @var2 in string at one.pl line17.
Possible unintended interpolation of @var in string at one.pl line 32.
Possible unintended interpolation of @var in string at one.pl line 33.
Global symbol "@var2" requires explicit package name at one.pl line16.
Global symbol "@var2" requires explicit package name at one.pl line17.
Global symbol "$buf2" requires explicit package name at one.pl line27.
Global symbol "$buf2" requires explicit package name at one.pl line30.
Global symbol "@var" requires explicit package name at one.pl line 31.
Global symbol "$buf2" requires explicit package name at one.pl line31.
Global symbol "@var" requires explicit package name at one.pl line 32.
Global symbol "@var" requires explicit package name at one.pl line 33.
Execution of one.pl aborted due to compilation errors.
So where can I find the explanation of these error warnings?
------------------------------
Date: Sat, 6 Dec 2003 12:59:40 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: reading binary files
Message-Id: <bqsjrs$t02$2@wisteria.csv.warwick.ac.uk>
Zoran.Marinkovic@post.rwth-aachen.de (Zoran) wrote:
> If I am using "use strict" the script won't even start and crushes
> with warnings like this:
>
> Possible unintended interpolation of @var2 in string at one.pl line17.
Don't retype code. Even error messages.
> Possible unintended interpolation of @var in string at one.pl line 32.
> Possible unintended interpolation of @var in string at one.pl line 33.
> Global symbol "@var2" requires explicit package name at one.pl line16.
<snop>
> So where can I find the explanation of these error warnings?
perldoc perldiag
Or add 'use diagnostics;' to the top of your script to have perl look
them up for you.
Ben
--
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else * ben@morrow.me.uk
------------------------------
Date: Sat, 06 Dec 2003 13:57:52 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: reading binary files
Message-Id: <vt3o305pj9c89f@corp.supernews.com>
In article <vt3h89q9g14g1f@corp.supernews.com>,
Greg Bacon <gbacon@hiwaay.net> wrote:
: sub getrec {
: my $fh = shift;
:
: my $bytes;
:
: my $ch;
: my $status = read $fh, $ch, 1;
: if ($status == 0) {
: return 0;
: }
: elsif (not defined $status) {
: warn "$0: read: $!";
: return;
: }
:
: while ($ch ne "!") {
: last if $ch eq "!";
:
: if ($ch =~ /\D/) {
: warn "$0: unexpected '$ch'";
: return;
: }
:
: $bytes .= $ch;
:
: $status = read $fh, $ch, 1;
: if ($status == 0) {
: warn "$0: premature end-of-file";
: return;
: }
: elsif (not defined $status) {
: warn "$0: read: $!";
: return;
: }
: }
:
: unless (defined $bytes) {
: warn "$0: number of bytes undefined";
: return;
: }
:
: $status = read $fh, my($chunk), $bytes;
: if ($status == 0) {
: warn "$0: premature end-of-file";
: return;
: }
: elsif (not defined $status) {
: warn "$0: read: $!";
: return;
: }
: elsif ($bytes != length $chunk) {
: warn "$0: incomplete read";
: return;
: }
:
: $chunk;
: }
Yuck, so much repetition. There's gotta be a better way.
Greg
--
The welfare state is the oldest con game in the world. First you take
people's money away quietly and then you give some of it back to them
flamboyantly.
-- Thomas Sowell
------------------------------
Date: 6 Dec 2003 11:11:10 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <bqsdge$db1$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Picker Leon:
> From perlsub
>
> To declare subroutines:
>
> sub NAME; # A "forward" declaration.
> sub NAME(PROTO); # ditto, but with prototypes
> sub NAME : ATTRS; # with attributes
> sub NAME(PROTO) : ATTRS; # with attributes and prototypes sub
> NAME BLOCK # A declaration and a definition.
> sub NAME(PROTO) BLOCK # ditto, but with prototypes
> sub NAME : ATTRS BLOCK # with attributes
> sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributesTo define
> an anonymous subroutine at runtime:
>
> $subref = sub BLOCK; # no proto
> $subref = sub (PROTO) BLOCK; # with proto
> $subref = sub : ATTRS BLOCK; # with attributes
> $subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributesIt looks
> to me that anonymous sub is better just as in file handle, using $FILE is
> always better than FILE. right?
Ah, but this analogy can't be drawn. Suppose you have this code:
sub hello {
my $name = shift;
print "Hello, $name\n";
}
hello("Picker");
If you re-write this using anonymous subroutines, it would become:
my $hello = sub {
my $name = shift;
print "Hello, $name\n";
}
$hello->("Picker");
Do you see any indication why the latter should be preferred over the
first piece of code?
Anonymous subroutines let you encapsulate a function in a variable in
order to do something special with it. For instance, you could pass this
function around. Here's an example from the world of functional
programming, also known as currying:
sub create_func {
my $arg1 = shift;
return sub { $arg1 + shift };
}
print create_func(5)->(6);
This involves two functions actually. Here's the first one:
my $ref = create_func(5);
create_func() returns a new function. 'create_func(5)' would roughly
return this function:
sub _returned_function {
return 5 + shift;
}
In order to create a dynamic amount of functions at runtime, it is
necessary to stuff them into a variable, therefore a reference to the
new function is returned. This new function can then be called via
reference:
my $ref = create_func(5); # $func1 is now the reference
print $ref->(6);
__END__
11
The above should just be an example for the context in which anonymous
subroutines are useful and in fact required. Perl beginners don't
usually indulge with such things. This will come later.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 5903
***************************************