[26276] in Perl-Users-Digest
Perl-Users Digest, Issue: 8458 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Sep 26 14:05:28 2005
Date: Mon, 26 Sep 2005 11:05:05 -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 Mon, 26 Sep 2005 Volume: 10 Number: 8458
Today's topics:
Re: How do I get more-detailed directory info? <zen13097@zen.co.uk>
Re: Packing/Unpacking bit fields from a byte. (Anno Siegel)
Re: print @{1} versus print @{11} <BLOCKSPAMfishfry@your-mailbox.com>
Re: print @{1} versus print @{11} (Anno Siegel)
Re: print @{1} versus print @{11} xhoster@gmail.com
Re: Script for migrating HTML tree into a single direct (Miguel Cruz)
Re: Website scraper <xx087@freenet.carleton.ca>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 26 Sep 2005 14:35:37 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: How do I get more-detailed directory info?
Message-Id: <43380739$0$31917$da0feed9@news.zen.co.uk>
Reinhard Pagitsch <rprp@gmx.net> wrote:
> --- code---
> use Cwd;
> use strict;
>
> my $CurDir = getcwd();
> print "CWD = ", $CurDir, "\n";
> opendir(DOT, ".") or die "Can\'t open the directory!!!";
Yuk. Lexical filehandles have been available for years.
opendir my $dir "." or die ...
>
> my @LocalFiles;
> my $FileName;
> my @files = readdir(DOT);
> my $Rec;
> foreach (@files)
If the only thing you're going to do with @files is loop over
them like this, why bother slurping them into an array in the first
place?
It's better and more scaleable to use something like
while( $FileName = readdir( $dir ) {
> {
> my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
> $atime,$mtime,$ctime,$blksize,$blocks) = stat($_);
No need to create all those variables and then ignore them. Just get
the things you need from stat():
my ( $mode, $size, $mtime ) = (stat)[ 2, 7, 9 ];
> my $FileRecord = {};
> $FileRecord->{Date} = "Unknown";
> $FileRecord->{Time} = $mtime;
> $FileRecord->{Type} = $mode;
> $FileRecord->{Size} = $size;
> $FileRecord->{Attr} = "Unknown";
> $FileRecord->{Name} = $_;
>
> push(@LocalFiles, $FileRecord);
I would write this as:
push @LocalFiles, {
Time => $mtime,
Type => $mode,
Size => $size,
Name => $FileName,
... etc ...
};
My code to output a simple ls-like listing would be as follows:
#!/usr/bin/perl
use warnings;
use strict;
my $dirname = "/tmp";
opendir my $dir, $dirname or die $!;
while ( my $filename = readdir $dir ) {
my $pathname = "$dirname/$filename";
my ( $size, $mtime ) = (stat $pathname)[ 7, 9 ];
my $type = "other";
# See "perldoc -f -X" for a complete list of tests
$type = "dir" if -d $pathname;
$type = "link" if -l $pathname;
$type = "file" if -f $pathname;
printf "%5s %20s %10d %s\n",
$type,
$filename,
$size,
scalar localtime $mtime;
}
closedir $dir;
------------------------------
Date: 26 Sep 2005 18:01:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Packing/Unpacking bit fields from a byte.
Message-Id: <dh9d29$4c3$1@mamenchi.zrz.TU-Berlin.DE>
Shashank Khanvilkar <shashank@mia.ece.uic.edu> wrote in comp.lang.perl.misc:
Ugh, top posting.
> Thanks
> I have already checked out vector. It does not help.
Sure? vec() (not vector) gives you access to the individual bits of a
bit string, which is what you want.
> Howver I now found out that the following works.
>
> @headerFeilds = unpack("A12A1A2....", unpack("B*", $header));
>
> The innermost unpack first converts the header to a string of bits.
> the outer unpack just seperates out these bits into the fields.
Here is one way using vec():
my $i = 0;
my @fields = map join( '', map vec( $header, $i ++, 1), 1 .. $_),
12, 1, 2;
There is no programming-around the quirks of unpack(), it accesses
each individual bit as directly as vec() allows. It's longer, but
so would be the unpack()-solution if it had to build the pack format
from parameters.
Pack and unpack are powerful, but not easy to use. Many Perl programmers
feel it's enough to know the possibilities but avoid them if there is
an alternative.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Mon, 26 Sep 2005 09:36:54 -0700
From: fishfry <BLOCKSPAMfishfry@your-mailbox.com>
Subject: Re: print @{1} versus print @{11}
Message-Id: <BLOCKSPAMfishfry-0BE621.09365426092005@comcast.dca.giganews.com>
In article <slrndjf7sk.ra.tassilo.von.parseval@localhost.localdomain>,
"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote:
> Also sprach fishfry:
>
> > Can someone please explain this result?
> >
> > print @{1}
> >
> > compiles but doesn't print anything.
> >
> > @{11}
> >
> > throws "Can't use string ("11") as an ARRAY ref while "strict refs" in
> > use"
> >
> > This one's really got me going. Perl 5.8.7.
>
> This is a bit obscure and probably due to the special nature of the
> digit variables ($1, $2...). You get an idea when you use B::Deparse to
> see that for perl those two things are treated differently in a subtle
> manner:
>
> ethan@ethan:~$ perl -MO=Deparse -e 'print @{1}'
> print @1;
> -e syntax OK
> ethan@ethan:~$ perl -MO=Deparse -e 'print @{11}'
> print @{11;};
> -e syntax OK
>
> @{1} is condensed into @1. Strictures don't warn on certain symbols that
> are always global and live in package main::. These are variables with
> digits and punctuation as name (so you are always allowed to use e.g.
> $`, @`, %` etc., even $2).
>
> @{11} however is @{11;} which is a symbolic reference. That means the
> block {...} is executed and whatever is returned is turned into a string
> and taken as the name of the variable. These (also called soft
> references) are disallowed when "strict 'refs'" are in effect.
>
> Having said that, this different treatment of @{1} and @{11} is a bug
> IMO.
>
Thank you much. But this brings up more questions.
* What exactly is @1? I know what $1 is, but what's @1?
* In perldoc perlop, if you search for '@{' you find this gem:
"Punctuation" arrays such as @+ are only interpolated if the name is
enclosed in braces @{+}.
Now, what on earth is a "punctuation" array and why is "punctuation" in
quotes? Googling reveals that this cryptic comment in perlop is the only
known use of the phrase "punctuation array."
And what does @{+} mean? What does it do?
These are not idle questions by the way ... I'm trying to unpack some
obfuscated Perl.
------------------------------
Date: 26 Sep 2005 17:01:13 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: print @{1} versus print @{11}
Message-Id: <dh99gp$29r$1@mamenchi.zrz.TU-Berlin.DE>
fishfry <BLOCKSPAMfishfry@your-mailbox.com> wrote in comp.lang.perl.misc:
> In article <slrndjf7sk.ra.tassilo.von.parseval@localhost.localdomain>,
> "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote:
>
> > Also sprach fishfry:
> >
> > > Can someone please explain this result?
> > >
> > > print @{1}
> > >
> > > compiles but doesn't print anything.
> > >
> > > @{11}
> > >
> > > throws "Can't use string ("11") as an ARRAY ref while "strict refs" in
> > > use"
[...]
> > ethan@ethan:~$ perl -MO=Deparse -e 'print @{1}'
> > print @1;
> > -e syntax OK
> > ethan@ethan:~$ perl -MO=Deparse -e 'print @{11}'
> > print @{11;};
> > -e syntax OK
[...]
> > Having said that, this different treatment of @{1} and @{11} is a bug
> > IMO.
> >
>
> Thank you much. But this brings up more questions.
>
> * What exactly is @1? I know what $1 is, but what's @1?
The same package variable, essentially, just like %1, the file handle 1
the subroutine 1 and some more. Packages (symbol tables) are organized
so that there is only one name entry for all of these, so in some sense
if one is defined so are the others.
> * In perldoc perlop, if you search for '@{' you find this gem:
>
> "Punctuation" arrays such as @+ are only interpolated if the name is
> enclosed in braces @{+}.
>
>
> Now, what on earth is a "punctuation" array and why is "punctuation" in
> quotes? Googling reveals that this cryptic comment in perlop is the only
> known use of the phrase "punctuation array."
It's the manual's way of saying "an array with punctuation characters in
its name".
> And what does @{+} mean? What does it do?
The name of a perl variable can (always) be enclosed in {} if necessary
for disambiguation. Its most common use is in string interpolation
my $plural = "${thing}s";
but it has other uses as the example shows.
This syntax does not make the {} block braces, nor their content perl
code. Otherwise we'd have a bareword plus a symref.
> These are not idle questions by the way ... I'm trying to unpack some
> obfuscated Perl.
...which, of course, is the exact opposite of an idle activity :)
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: 26 Sep 2005 17:15:05 GMT
From: xhoster@gmail.com
Subject: Re: print @{1} versus print @{11}
Message-Id: <20050926131505.070$TX@newsreader.com>
fishfry <BLOCKSPAMfishfry@your-mailbox.com> wrote:
>
> Thank you much. But this brings up more questions.
>
> * What exactly is @1? I know what $1 is, but what's @1?
@1 is the array which happens to have the same name as the scalar $1.
$1 is special, while @1 is not special other than that it has the same
name as $1, which has the side-effect that @1 will not trigger errors under
strict.
>
> * In perldoc perlop, if you search for '@{' you find this gem:
>
> "Punctuation" arrays such as @+ are only interpolated if the name is
> enclosed in braces @{+}.
>
> Now, what on earth is a "punctuation" array and why is "punctuation" in
> quotes?
A punctuation array is an array whose name is composed of punctuation. It
is in quotes because the other of perlop is notifying you that he is either
coining the term, or is using the term advisedly.
> Googling reveals that this cryptic comment in perlop is the only
> known use of the phrase "punctuation array."
A punctuation array is an array which has punctuation name.
perldoc perlvar:
NAME
perlvar - Perl predefined variables
DESCRIPTION
Predefined Names
The following names have special meaning to Perl. Most punctuation
names have reasonable mnemonics, or analogs in the shells.
...
>
> And what does @{+} mean? What does it do?
@{+} is the way you get @+ to interpolate into double-quoted strings. @+
is documented in perldoc perlvar.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Mon, 26 Sep 2005 11:04:12 -0500
From: mnc@admin.u.nu (Miguel Cruz)
Subject: Re: Script for migrating HTML tree into a single directory ?
Message-Id: <w5ydnb1uO9phhqXeRVn-sw@speakeasy.net>
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> Pertti Kosunen wrote:
>> Pan Am wrote:
>>> My Web hosting service does not support multiple directories...
>>> Can anyone suggest a Unix script that traverses a HTML tree and
>>> produces a working "single directory" version of the same?
>>
>> Not perl but "wget --no-directories ..." might do the job.
>
> Such a limited hosting account does most likely not offer shell access...
The user did ask for a unix script.
But anyway, you can run wget anywhere, and then transfer the results to the
web host.
miguel
--
Hit The Road! Photos from 36 countries on 5 continents: http://travel.u.nu
Latest photos: Queens Day in Amsterdam; the Grand Canyon; Amman, Jordan
------------------------------
Date: 26 Sep 2005 13:17:53 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Website scraper
Message-Id: <slrndjftcg.nvl.xx087@smeagol.ncf.ca>
At 2005-09-24 11:11AM, Stephen Hildrey <steve@uptime.org.uk> wrote:
> No. This is Perl - the backslash is a syntax error:
>
> $ cat > backslash.pl << _EOF && perl backslash.pl
> > use strict;
> > use warnings;
> > my $foo = "foo" \
> > if (1);
> > _EOF
> syntax error at backslash.pl line 3, near "my ="
> Execution of backslash.pl aborted due to compilation errors.
No, your shell is doing variable substitution:
$ foo=bar cat > foo.pl << _EOF
> my $foo = `date`;
> _EOF
$ cat foo.pl
my bar = Mon Sep 26 09:15:37 EDT 2005;
That's the perl syntax error you're seeing.
If you want to use shell here-docs to type perl programs, single-quote
your delimiter:
$ foo=bar cat > foo.pl << '_EOF'
> my $foo = `date`;
> _EOF
$ cat foo.pl
my $foo = `date`;
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
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 8458
***************************************