[25546] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7790 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 16 18:05:58 2005

Date: Wed, 16 Feb 2005 15:05:28 -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           Wed, 16 Feb 2005     Volume: 10 Number: 7790

Today's topics:
    Re: [perl-python] problem: reducing comparison (correct <xah@xahlee.org>
    Re: [perl-python] problem: reducing comparison (erratum <xah@xahlee.org>
    Re: [perl-python] problem: reducing comparison (erratum <xah@xahlee.org>
    Re: A hopefully simple question <phaylon@dunkelheit.at>
    Re: A hopefully simple question <thundergnat@hotmail.com>
    Re: A hopefully simple question <jgibson@mail.arc.nasa.gov>
    Re: A hopefully simple question <harold@uga.edu>
    Re: A hopefully simple question <harold@uga.edu>
    Re: A hopefully simple question <nobull@mail.com>
    Re: A hopefully simple question <matternc@comcast.net>
    Re: backticks with threads or forks hang program (Anno Siegel)
    Re: Don't understand this syntax - (Larry)
    Re: Don't understand this syntax - <spamtrap@dot-app.org>
    Re: editing pdf files with perl <segraves_f13@mindspring.com>
    Re: forms, cgi - beginner question <1usa@llenroc.ude.invalid>
    Re: forms, cgi - beginner question <jfcampbell@aol.com>
        Hash references & parsing HTML with HTML::Parser <grabek@invalid.com>
    Re: Hash references & parsing HTML with HTML::Parser <nobull@mail.com>
    Re: Hash references & parsing HTML with HTML::Parser (Gary E. Ansok)
    Re: Hash references & parsing HTML with HTML::Parser <jgibson@mail.arc.nasa.gov>
    Re: Hash references & parsing HTML with HTML::Parser <grabek@invalid.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 16 Feb 2005 13:55:02 -0800
From: "Xah Lee" <xah@xahlee.org>
Subject: Re: [perl-python] problem: reducing comparison (correction)
Message-Id: <1108590902.480489.122610@g14g2000cwa.googlegroups.com>


Xah Lee wrote:
> In imperative languages such as Perl and Python and Java, in general
it
> is not safe to delete elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a copy first, and work
with
> the copy.

Correction:
When looping thru a list-like entity and delete elements in the vary
list, there's a question whether it will change the iteration. (For
example, if one loops thru 1 to 9, and deleted 8 while at 2, should the
loop still do 8?) For some languages and or list entities, the answer
may be yes or no. However, in imperative languages such as Perl and
Python and Java, often this is not allowed by the language, partially
as a protection to safeguard and assume programers as ignoramuses, but
partially because the internal issues of these languages can't handle
it.

The work around in these languages is always to make a copy of the
list-entity, and work with the copy.

 Xah
 xah@xahlee.org
 http://xahlee.org/PageTwo_dir/more.html



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

Date: 16 Feb 2005 14:06:27 -0800
From: "Xah Lee" <xah@xahlee.org>
Subject: Re: [perl-python] problem: reducing comparison (erratum)
Message-Id: <1108591587.564859.281090@o13g2000cwo.googlegroups.com>

Xah Lee wrote:
> In imperative languages such as Perl and Python
> and Java, in general it is not safe to delete
> elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a
> copy first, and work with the copy.

Correction:
When looping thru a list-like entity and delete elements in the vary
list, there's a question whether it will change the iteration. (For
example, if one loops thru 1 to 9, and deleted 8 while at 2, should the
loop still do 8?) For some languages and or list entities, the answer
may be yes or no. However, in imperative languages such as Perl and
Python and Java, often this is not allowed, justified as a protection
to safeguard programers as ignoramuses, but partially because the
internal issues of these languages. (These languages have molded a
generation of programers to question and discourse man-made issues.)

The work around in these languages is always to make a copy of the
list-entity, and work with the copy.

 Xah
 xah@xahlee.org
 http://xahlee.org/PageTwo_dir/more.html



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

Date: 16 Feb 2005 14:15:08 -0800
From: "Xah Lee" <xah@xahlee.org>
Subject: Re: [perl-python] problem: reducing comparison (erratum)
Message-Id: <1108592108.331641.254500@f14g2000cwb.googlegroups.com>

Xah Lee wrote:
> In imperative languages such as Perl and Python
> and Java, in general it is not safe to delete
> elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a
> copy first, and work with the copy.

Correction:
When looping thru a list-like entity and delete elements in the vary
list, there's a question whether it will change the iteration. (For
example, if one loops thru 1 to 9, and deleted 8 while at 2, should the
loop still do 8?) This is a design issue. Both behavior are useful. For
some languages and or list entities, the answer may be yes or no.
However, in imperative languages such as Perl and Python and Java,
often modifying a list while looping simply cannot be done, justified
as a protection to safeguard programers as ignoramuses, but partially
because the internal issues of these languages. (These languages have
molded a generation of programers to question and discourse man-made
complexities.)

The work around in these languages is always to make a copy of the
list-entity, and work with the copy.

 Xah
 xah@xahlee.org
 http://xahlee.org/PageTwo_dir/more.html



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

Date: Wed, 16 Feb 2005 20:07:51 +0100
From: phaylon <phaylon@dunkelheit.at>
Subject: Re: A hopefully simple question
Message-Id: <pan.2005.02.16.19.07.51.76013@dunkelheit.at>

Harold Pritchett wrote:

> Subject:         A hopefully simple question

Weird problem ..

> I've tried to find this on the web, but have failed miserably.

 .. but you're lucky, I've got a simple question, what came out when you
tried? :D Also: Is that your real code? I was unable to find something in
perldoc about lopen. Is this a subroutine as & indicates?

-- 
http://www.dunkelheit.at/

Ordinary morality is only for ordinary people.
                       -- Aleister Crowley



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

Date: Wed, 16 Feb 2005 14:34:44 -0500
From: thundergnat <thundergnat@hotmail.com>
Subject: Re: A hopefully simple question
Message-Id: <HridnZ_0P77OPY7fRVn-rg@rcn.net>

Harold Pritchett wrote:

> 
> My question is the difference in the following two
> statements:
> 
>  print FILE "$_";
>  print FILE ">$_";
> 

What happened when you tested it?



my @words = qw/My question is the difference in the following two 
statements/;
print "$_\n" for @words;
print ">$_\n" for @words;


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

Date: Wed, 16 Feb 2005 11:34:00 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: A hopefully simple question
Message-Id: <160220051134005910%jgibson@mail.arc.nasa.gov>

In article <42139839.3050507@uga.edu>, Harold Pritchett
<harold@uga.edu> wrote:

> I am modifying a perl script which I did not write.
> 
> I am at best a beginning perl programmer, but have been
> writing code in other languages for over 30 years.
> 
> My question is the difference in the following two
> statements:
> 
>   print FILE "$_";
>   print FILE ">$_";

The second statement will print a '>' character before it prints the
contents of the variable $_ (to FILE). The first statement will not.


----== 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: Wed, 16 Feb 2005 14:41:24 -0500
From: Harold Pritchett <harold@uga.edu>
Subject: Re: A hopefully simple question
Message-Id: <ja6dnYE2ssGbP47fRVn-rg@snappydsl.net>

phaylon wrote:
> Harold Pritchett wrote:
> 
> 
>>Subject:         A hopefully simple question
> 
> 
> Weird problem ..
> 
> 
>>I've tried to find this on the web, but have failed miserably.
> 
> 
> .. but you're lucky, I've got a simple question, what came out when you
> tried? :D Also: Is that your real code? I was unable to find something in
> perldoc about lopen. Is this a subroutine as & indicates?
> 

This is a subroutine, defined in shlock.pl and majordomo.pl.

The code snippet from shlock.pl looks like this:

# open a file locked for exclusive access;
# we remember the name of the lock
# file, so that we can delete it when we close the file
#
sub main'lopen {
     local($FH) = shift;
     local($mode) = shift;
     local($file) = shift;
     # $fm is what will actually get passed to open()
     local($fm) = "$mode$file";
     local($status);

     # create name for lock file
     local($lockfile) = $file;
     $lockfile =~ s,([^/]*)$,L.$1,;

     # force unqualified filehandles into callers' package
     local($package) = caller;
     $FH =~ s/^[^':]+$/$package'$&/;

     return undef unless &main'set_lock("$lockfile");

     # Got the lock; now try to open the file
     if ($status = open($FH, $fm)) {
         # File successfully opened; remember the lock file for deletion
         $lock_files[fileno($FH)] = "$lockfile";
     } else {
         # File wasn't successfully opened; delete the lock
                &main'free_lock($lockfile);
      }
     # return the success or failure of the open
     return $status;
}

Obviously, this code is a majordomo archive module, and I want to 
understand how it works so I can modify it's behavior.

Harold


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

Date: Wed, 16 Feb 2005 14:44:00 -0500
From: Harold Pritchett <harold@uga.edu>
Subject: Re: A hopefully simple question
Message-Id: <ja6dnYA2ssE8P47fRVn-rg@snappydsl.net>

Jim Gibson wrote:
> In article <42139839.3050507@uga.edu>, Harold Pritchett
> <harold@uga.edu> wrote:
> 
> 
>>I am modifying a perl script which I did not write.
>>
>>I am at best a beginning perl programmer, but have been
>>writing code in other languages for over 30 years.
>>
>>My question is the difference in the following two
>>statements:
>>
>>  print FILE "$_";
>>  print FILE ">$_";
> 
> 
> The second statement will print a '>' character before it prints the
> contents of the variable $_ (to FILE). The first statement will not.

Obvious now that I see it.  Sometimes it's hard to tell data from code

Thank you very much.

Harold



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

Date: Wed, 16 Feb 2005 20:04:09 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: A hopefully simple question
Message-Id: <cv08lv$e8m$1@sun3.bham.ac.uk>



Harold Pritchett wrote:

> phaylon wrote:
> 
>> Harold Pritchett wrote:
>>
>>
>>> Subject:         A hopefully simple question
>>
>>
>>
>> Weird problem ..
>>
>>
>>> I've tried to find this on the web, but have failed miserably.
>>
>>
>>
>> .. but you're lucky, I've got a simple question, what came out when you
>> tried? :D Also: Is that your real code? I was unable to find something in
>> perldoc about lopen. Is this a subroutine as & indicates?
>>
> 
> This is a subroutine, defined in shlock.pl and majordomo.pl.
> 
> The code snippet from shlock.pl looks like...

 ... shlock.pl is a Perl4 script.

There in general much better ways to do things than there were in Perl4.

Comparatively few poeple here can remeber Perl4 well if at all.  I, for 
example, have only been using Perl for a little over a decade so I 
hardly touched Perl4.

Perl5 is almost but not quite 100% compatible with Perl4.

Once of the things that is different is what happens if you use a GLOB 
(kinda poor-man's reference) in a string context.  In Perl4 the 
delimiter between the namespace and the symbol is a single quote, in 
Perl5 it's a double colon.

> # open a file locked for exclusive access;
> # we remember the name of the lock
> # file, so that we can delete it when we close the file
> #
> sub main'lopen {
>     local($FH) = shift;
>     local($mode) = shift;
>     local($file) = shift;
>     # $fm is what will actually get passed to open()
>     local($fm) = "$mode$file";
>     local($status);
> 
>     # create name for lock file
>     local($lockfile) = $file;
>     $lockfile =~ s,([^/]*)$,L.$1,;
> 
>     # force unqualified filehandles into callers' package
>     local($package) = caller;
>     $FH =~ s/^[^':]+$/$package'$&/;
> 
>     return undef unless &main'set_lock("$lockfile");
> 
>     # Got the lock; now try to open the file
>     if ($status = open($FH, $fm)) {
>         # File successfully opened; remember the lock file for deletion
>         $lock_files[fileno($FH)] = "$lockfile";
>     } else {
>         # File wasn't successfully opened; delete the lock
>                &main'free_lock($lockfile);
>      }
>     # return the success or failure of the open
>     return $status;
> }
> 
> Obviously, this code is a majordomo archive module, and I want to 
> understand how it works so I can modify it's behavior.

So, can you point out the parts that you are finding difficulty finding 
in the refernce manuals or the passages in the reference manuals you are 
finding unclear.

Note: to make sense of this code you using contemporary documentation 
you  need to translate all the singe quetes in the middle of symbol 
names into double colons.



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

Date: Wed, 16 Feb 2005 15:39:17 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: A hopefully simple question
Message-Id: <BfqdnRKEQoboMo7fRVn-pA@comcast.com>

Harold Pritchett wrote:

> I am modifying a perl script which I did not write.
> 
> I am at best a beginning perl programmer, but have been
> writing code in other languages for over 30 years.
> 
> My question is the difference in the following two
> statements:
> 
>   print FILE "$_";

This prints the contents of $_ to the filehandle FILE.

>   print FILE ">$_";

This prints ">" and then the contents of $_ to the 
filehandle FILE.
> 
> The file is the same, and is opened with the statement:
> 
>      &lopen($FH, ">>", "$opt_f$suffix") ||
>          die("Can't append to $opt_f$suffix: $!");
>      $is_open = 1;
> 
> This is done in a subroutine and the file handle FILE is
> passed into the subroutine as FH.  The file name obviously
> is passed in two pieces, $opt_f and $suffix.
> 
> I've tried to find this on the web, but have failed
> miserably.
> 
> Thanks
> 
> Harold

-- 
             Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"


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

Date: 16 Feb 2005 19:57:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: backticks with threads or forks hang program
Message-Id: <cv08j6$fpq$1@mamenchi.zrz.TU-Berlin.DE>

Warren <warren@netlab.co.za> wrote in comp.lang.perl.misc:
> Greetings
> 
> The function of the script below is to execute scripts (perl) at
> regular intervals. As I have no idea how long these external scripts
> will take to excute I decided to use threads and execute each script
> from its own thread. This appeared to work until the number of threads
> increased. The greater the number of threads the more lightly the
> program will hang. As the output of the external scripts is required
> the backticks are used.

Don't use threads, use background processes.

Specifically, for every external command, open a filehandle to read its
output from.  See "perldoc -f open" and look for "-|".  That the commands
are Perl scripts doesn't make a difference here.

You will now have to find out which processes have finished -- a problem
you tried to sweep under a carpet of threads before.  There are several
possibilities.  Making the filehandles non-blocking and polling them every
so many seconds is one solution.  Others would involve select() (q.v.) in
one way or another.  For a more general discussion, see perlipc.

Anno


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

Date: Wed, 16 Feb 2005 21:46:29 GMT
From: noway@none.com (Larry)
Subject: Re: Don't understand this syntax -
Message-Id: <VaPQd.6403$Tj7.2892@twister.socal.rr.com>

In article <aLOdnVsKbYpsXo_fRVn-qA@adelphia.com>, Sherm Pendley <spamtrap@dot-app.org> wrote:
>Larry wrote:
>
>> I'm a novice trying to learn some perl, and am trying to understand some
>> syntax used in a formmail script to seperate a list of names.
>
>First off, understand this: If you're referring to Matt Wright's formmail,
>don't even bother trying to understand it. It's horribly written, and the
>only useful purpose it has is to serve as an example of how *not* to write
>Perl.
>
>> The use is - (split /\s*,\s*/, $recipient)
>> 
>> I understand split, but can't see why the \s*,\s* is used as the marker.
>> It seems to evaluate to s,s so why the escapes and asterisks?
>
>Check the docs for the function - "perldoc -f split" and see what it says.
>Notice how the first argument is listed as /PATTERN/? That means it's a
>regex, so the escapes and asterisks have the same meaning they do in other
>regexes. Let's break it down:
>
>    /          # Begin the pattern
>        \s*    # Match any number (including zero) of whitespace (\s)
>               # characters
>        ,      # Match a comma
>        \s*    # Again, any number of whitespace characters
>    /          # End pattern
>
>So "," "    ," "     ,      " would all match the pattern.
>
>Have a look at the following perldocs for more about regexes:
>
>    perldoc perlrequick
>    perldoc perlretut
>    perldoc perlre
>
>sherm--

Sherm,

Thanks much. It was, of course "casual to the most obvious observer". I had 
actually looked at numerous lists of the common escape characters, but none 
had the \s on them. I've used lots of others, but apparantly just never needed 
a space before, so didn't know that one.

Back to studying regexes a little more closely.

Larry L



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

Date: Wed, 16 Feb 2005 18:04:32 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Don't understand this syntax -
Message-Id: <ZKydnWA6uawdTI7fRVn-iQ@adelphia.com>

Larry wrote:

> had actually looked at numerous lists of the common escape characters, but
> none had the \s on them. I've used lots of others, but apparantly just
> never needed a space before, so didn't know that one.

A bit of a nit-pick: "\s" doesn't just match "a space", it matches *any*
whitespace character. That includes spaces, tabs, carriage returns,
newlines, and possibly other whitespace characters I'm forgetting at the
moment.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: Wed, 16 Feb 2005 22:14:09 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: editing pdf files with perl
Message-Id: <RAPQd.2721$9J5.1510@newsread2.news.atl.earthlink.net>

"Bill Segraves" <segraves_f13@mindspring.com> wrote in message news:...
> "pikus" <hacker.poet@gmail.com> wrote in message
> news:1108438561.091781.264040@o13g2000cwo.googlegroups.com...
> <snip>
> > So why does this not qualify as an appropriate question for this group?
> > It certainly looks like a miscellaneous perl question to me...
>
<snip>

O.K. Here's a Perl script that uses Pdftk to merge an FDF file into a PDF
file, which could provide a command-line solution for the OP (Tested on this
Win32 workstation).

#!perl
# usage - ..\bin\perl pdftk.pl inpdf=sam_hdrftr.pdf
infdf=sample_was_data.fdf outpdf=sam_hdrftr_filled.pdf
# note - all files used in this example reside in c:\indigoperl\tmp on this
Win32 workstation

use strict;
use warnings;
use CGI qw(:param);
my $inpdf  = param('inpdf');
my $infdf  = param('infdf');
my $outpdf = param('outpdf');

#Example 4 - fill a PDF with the data in an FDF file
system('pdftk.exe', $inpdf, 'fill_form', $infdf, 'output', $outpdf);

See the documentation for Pdftk, www.accesspdf.com/pdftk, for any questions
on command syntax for Pdftk.

<snip>

> >Plus I
> > want to know the answer but I'm lazy enough not to go to comp.text.pdf
> > to get it. :-) Thanks. - pikus

I hope you looked at the examples and documentation for Pdftk, where you
might have learned some of the resons why you shouldn't just routinely edit
a PDF file, even though it may "look" like a plain text file.

Cheers.
--
Bill Segraves




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

Date: 16 Feb 2005 22:05:09 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: forms, cgi - beginner question
Message-Id: <Xns95FFADCE3552Basu1cornelledu@132.236.56.8>

"John" <jfcampbell@aol.com> wrote in news:1108585901.037171.173400
@l41g2000cwc.googlegroups.com:

> Contact the server administrator (me), who notes that Sinan thinks the
> server administrator writes crappy Perl and can't configure a Web
> server.

Well, actually, I never said that. I said, if the web server is echoing the 
contents of the script rather than running it, then, your web server was 
not properly configured to serve CGI scripts out of that directory. I did 
not know the peculiarities of your system, so I was not able provide the 
insight Sherm provided.

Second, I did not say you write carppy Perl but pointed out that your 
script did not send a proper header, so even if the server tried to run 
your script, you would get an error, just as you described above.

I think you have come a long way realizing the distinctions between server 
configuration, CGI, and Perl questions, and I am sure, in the future, you 
will direct your questions to the most approriate forum.

> Here's why I did it anyway:
> 
> I know nothing of cgi, nothing about Apache and couldn't even frame the
> question in those terms.

If you are going to write CGI scripts, you probably want to learn something 
about CGI. Being able to partition the problem domain will enable you to 
pinpoint problems and solicit effective and helpful responses.

Sinan


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

Date: 16 Feb 2005 12:31:41 -0800
From: "John" <jfcampbell@aol.com>
Subject: Re: forms, cgi - beginner question
Message-Id: <1108585901.037171.173400@l41g2000cwc.googlegroups.com>

This is the kind of thing I was looking for originally. Somehow in the
meantime, I made things worse, and can no longer even echo the script
to the browser window.

Strictly speaking,no I did not know that cgi-bin is not a real
subdirectory. But based on the short experience with Personl Web
Sharing and plain old HTML, I was already dizzy trying to find the
'real' directory that apache/os x/apple/personal web sharing was
hiding. I knew it wasn't as simple as the folder in the finder being a
plain old directory.

Now I've gone from 'not found' to one of two results. In the Safari
browser, if I click Submit, I get jumped into a finder window, with the
icon of example.pl selected.

If I  hit it from another machine in the room, running Netscape, I get
"Method Not Allowed"

I'm wondering, did I foul things up by *creating* a directory named
cgi-bin?

So I remove the directory blahblah/Sites/cgi-bin ...

example.pl is now only in /Library/WebServer/CGI-Executables

In Safari on the local machine, I'm now back to 'Not found.'

On the other machines, it now says "Forbidden - you do not have
permission to access /cgi-bin/example.pl on this server.

Back to /Library/WebServer/CGI-Executables, where we go 777 on the
permissions for example.pl

Over to Netscape.

Now we see 'Internal Server Error please contact the server
administrator.'

Contact the server administrator (me), who notes that Sinan thinks the
server administrator writes crappy Perl and can't configure a Web
server.

Could it be that my crappy Perl is crapping out?

This is indeed progress.

Following Sinan's advice, I  ...

use CGI();

Voila!

Netscape now shows the output of the script, and only the output.

Thanks to all who contributed.

Now to all who suggested I was in the wrong group as this was really an
Apache question ...

You're right.

Here's why I did it anyway:

I know nothing of cgi, nothing about Apache and couldn't even frame the
question in those terms.

But I was hoping the common language of Perl would help me get the
question across. It did.

Thanks again.



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

Date: Wed, 16 Feb 2005 19:10:20 +0000 (UTC)
From: Lukasz Grabun <grabek@invalid.com>
Subject: Hash references & parsing HTML with HTML::Parser
Message-Id: <slrnd176lk.2pb.grabek@localhost.localdomain>

So, I am still working on my simple HTML parser. Now, following your 
tips, I am using HTML::Parser. I've made one simple script with that 
package and now want to make something more serious.

According to perldoc HTML::Parser attr is a reference to a hash. As far 
as I understand it is possible to retrieve (and modify) that hash's 
values using $attr->{'href'} for example. Now, say I want to *add* a new 
value to the hash. I am not that good at references (and at perl as 
well, unfortunately) but according to Programming Perl passing values to 
hash via reference can be achieved by $$attr{KEY} = "VALUE"; so, since 
no id attribute is defined in my HTML source I want to pass its value 
for every tag of my choice. I do this with $$attr{id} = "prefix-$number" 
but apparently this does not work. Here's corresponding code:

sub start {
    my ($self, $tag, $attr, $attrseq, $origtext) = @_;
    
    unless ($tag =~ /^a$|^blockquote$|^p$/) { print $origtext; return; }
    $$attr{id} = "pref-$number"; $number +=5;
    
    print "<$tag ";
    foreach my $i (@$attrseq) { print $i, qq(="$attr->{$i}"); }
    print ">";
    }

I am using warnings but no message is being displayed. The output is 
stopped before the first line that containts tags id's of whose I want 
to modify.

Anyone can enlighten me with what I'm doing wrong?




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

Date: Wed, 16 Feb 2005 19:44:00 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Hash references & parsing HTML with HTML::Parser
Message-Id: <cv07g6$dn1$1@sun3.bham.ac.uk>



Lukasz Grabun wrote:

> sub start {
>     my ($self, $tag, $attr, $attrseq, $origtext) = @_;
>     
>     unless ($tag =~ /^a$|^blockquote$|^p$/) { print $origtext; return; }
>     $$attr{id} = "pref-$number"; $number +=5;
>     
>     print "<$tag ";
>     foreach my $i (@$attrseq) { print $i, qq(="$attr->{$i}"); }
>     print ">";
>     }

When your start subroutine is called $attr is a reference to a hash 
relating attributes names to attribute values and $attrseq is a 
reference to an array containing the attributes in order.

But these two variables are not magically linked. If you change one the 
other doesn't change.  You chage %$attr correctly but then you iterate 
over @$attrseq and @$attrseq will not contain your new attruibute.

You need to insert

   push @$attrseq => "pref-$number";



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

Date: Wed, 16 Feb 2005 20:24:09 +0000 (UTC)
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: Hash references & parsing HTML with HTML::Parser
Message-Id: <cv0a59$4pd$1@naig.caltech.edu>

In article <cv07g6$dn1$1@sun3.bham.ac.uk>,
Brian McCauley  <nobull@mail.com> wrote:
>Lukasz Grabun wrote:
>
>> sub start {
>>     my ($self, $tag, $attr, $attrseq, $origtext) = @_;
>>     
>>     unless ($tag =~ /^a$|^blockquote$|^p$/) { print $origtext; return; }
>>     $$attr{id} = "pref-$number"; $number +=5;
>>     
>>     print "<$tag ";
>>     foreach my $i (@$attrseq) { print $i, qq(="$attr->{$i}"); }
>>     print ">";
>>     }
>
>When your start subroutine is called $attr is a reference to a hash 
>relating attributes names to attribute values and $attrseq is a 
>reference to an array containing the attributes in order.
>
>But these two variables are not magically linked. If you change one the 
>other doesn't change.  You chage %$attr correctly but then you iterate 
>over @$attrseq and @$attrseq will not contain your new attruibute.
>
>You need to insert
>
>   push @$attrseq => "pref-$number";

Shouldn't that be

   push @$attrseq => "id";

since @$attrseq contains a list of the keys of %$attr?

Gary


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

Date: Wed, 16 Feb 2005 12:36:02 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Hash references & parsing HTML with HTML::Parser
Message-Id: <160220051236029237%jgibson@mail.arc.nasa.gov>

In article <slrnd176lk.2pb.grabek@localhost.localdomain>, Lukasz Grabun
<grabek@invalid.com> wrote:

> So, I am still working on my simple HTML parser. Now, following your 
> tips, I am using HTML::Parser. I've made one simple script with that 
> package and now want to make something more serious.
> 
> According to perldoc HTML::Parser attr is a reference to a hash. As far 
> as I understand it is possible to retrieve (and modify) that hash's 
> values using $attr->{'href'} for example. Now, say I want to *add* a new 
> value to the hash. I am not that good at references (and at perl as 
> well, unfortunately) but according to Programming Perl passing values to 
> hash via reference can be achieved by $$attr{KEY} = "VALUE"; so, since 
> no id attribute is defined in my HTML source I want to pass its value 
> for every tag of my choice. I do this with $$attr{id} = "prefix-$number" 
> but apparently this does not work. Here's corresponding code:
> 
> sub start {
>     my ($self, $tag, $attr, $attrseq, $origtext) = @_;
>     
>     unless ($tag =~ /^a$|^blockquote$|^p$/) { print $origtext; return; }
>     $$attr{id} = "pref-$number"; $number +=5;
>     
>     print "<$tag ";
>     foreach my $i (@$attrseq) { print $i, qq(="$attr->{$i}"); }
>     print ">";
>     }
> 
> I am using warnings but no message is being displayed. The output is 
> stopped before the first line that containts tags id's of whose I want 
> to modify.
> 
> Anyone can enlighten me with what I'm doing wrong?

You are only printing the elements of %$attr for keys in the array
@$attrseq. Does @$attrseq contain the string 'id'? If not, you will not
print the new hash element you have just created.

Use the Data::Dumper module to view the cotents of complex data
structures (untested):

use Data::Dumper;
 .
 .
 .
print Dumper($attr);

or print all of the elements of %$attr:
   foreach my $key ( %$attr ) {
      print "$key=$$attr[$key}\n";
   }

or just print the new element:

   print "$$attr{id}\n";


----== 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: Wed, 16 Feb 2005 20:42:52 +0000 (UTC)
From: Lukasz Grabun <grabek@invalid.com>
Subject: Re: Hash references & parsing HTML with HTML::Parser
Message-Id: <slrnd17bs3.2pb.grabek@localhost.localdomain>

On Wed, 16 Feb 2005 20:24:09 +0000 (UTC), Gary E. Ansok wrote:

> Shouldn't that be
>    push @$attrseq => "id";
> since @$attrseq contains a list of the keys of %$attr?

Either the way, I found a workaround - instead of messing up with %$attr 
or @$attreseq I simply print "id=\"pref-$number\"; I don't need it for 
any further operations so once it displayed I can forget about it.

Both answers I received are very helpful, nonetheless. Well, practice 
makes the master; back to the drawing board. Thank you, guys.


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

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


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