[23697] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5904 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 6 21:05:42 2003

Date: Sat, 6 Dec 2003 18:05:08 -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: 5904

Today's topics:
        a script to login a user an execute commands? (Marc-Olivier BERNARD)
    Re: a script to login a user an execute commands? <jwillmore@remove.adelphia.net>
    Re: a script to login a user an execute commands? <nospam@bigpond.com>
    Re: gui in perl <matthew.garrish@sympatico.ca>
    Re: How to open a file from the end and read the last 1 <jwillmore@remove.adelphia.net>
    Re: How to open a file from the end and read the last 1 (Anno Siegel)
    Re: How to open a file from the end and read the last 1 <uri@stemsystems.com>
    Re: How to write to drive A:\ from CGI Perl (Cle)
    Re: Idiom for array index that I'm foreach'ing over? <bmb@ginger.libs.uga.edu>
    Re: Idiom for array index that I'm foreach'ing over? <bmb@ginger.libs.uga.edu>
        Ongoing LWP impass...  advise please? (at)FinancialDataCorp.com (Bob Mariotti)
    Re: Ongoing LWP impass...  advise please? (Tad McClellan)
        Please Explain the Following Anonymous Sub <Temp@NoSuchDomain.Info>
    Re: Please Explain the Following Anonymous Sub <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: Please Explain the Following Anonymous Sub (Jay Tilton)
    Re: q on 'record', 'field' separators in text files <jwillmore@remove.adelphia.net>
    Re: What is anonymous sub? Why is it better? <Temp@NoSuchDomain.Info>
    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: 6 Dec 2003 09:09:25 -0800
From: s_p_a_m_mob@hotmail.com (Marc-Olivier BERNARD)
Subject: a script to login a user an execute commands?
Message-Id: <a846118c.0312060909.68297867@posting.google.com>

Hi there,


I need to create a small script to execute as root, which logs in as
standard user, and execute some commands.

Is there another way to do but use Expect.pm?

Thanks,


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

Date: Sat, 06 Dec 2003 22:15:11 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: a script to login a user an execute commands?
Message-Id: <20031206171511.755dcbff.jwillmore@remove.adelphia.net>

On 6 Dec 2003 09:09:25 -0800
s_p_a_m_mob@hotmail.com (Marc-Olivier BERNARD) wrote:
 
> I need to create a small script to execute as root, which logs in as
> standard user, and execute some commands.

Define "logs in".  What are you using to 'log in' - telnet, ssh, ??

> Is there another way to do but use Expect.pm?

Yes.  Many modules are available.  It all depends on what you're
trying to do.

Some code would be helpful :-)

HTH

-- 
Jim

Copyright notice: all code written by the author in this post is
 released under the GPL. http://www.gnu.org/licenses/gpl.txt 
for more information.

a fortune quote ...
"I stopped believing in Santa Claus when I was six.  Mother took 
me to see him in a department store and he asked for my
<autograph."   -- Shirley Temple 


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

Date: Sun, 07 Dec 2003 11:47:31 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: a script to login a user an execute commands?
Message-Id: <1879505.IeSxTByIXo@gregs-web-hosting-and-pickle-farming>

It was a dark and stormy night, and Marc-Olivier BERNARD managed to scribble:

> Hi there,
> 
> 
> I need to create a small script to execute as root, which logs in as
> standard user, and execute some commands.
> 
> Is there another way to do but use Expect.pm?
> 
> Thanks,

Assuming you use linux, why don't you just use the linux 'expect' command.

gtoomey


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

Date: Sat, 6 Dec 2003 10:51:14 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: gui in perl
Message-Id: <3WmAb.16259$yd.2605625@news20.bellglobal.com>


"Bart Lateur" <bart.lateur@pandora.be> wrote in message
news:p783tv0eelgdol7k3fq145ur392hsb3cot@4ax.com...
> Matt Garrish wrote:
>
> >It's not about complexity, but the speed at which you can toss a gui
> >together.
>
> Take a look at The GUI Loft, not for Tk, but for Win32::GUI on Win32. As
> easy as VB, which means, a lot easier than VC++ (IMO)
>
> <http://www.bahnhof.se/~johanl/perl/Loft/>
>

Very interesting! (And Win32::GUI definitely creates a much nicer interface
than you wind up with in Tk.) If it makes gui building as simple as it
looks, I'm definitely coming back to Perl for my user interfaces.

Matt




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

Date: Sat, 06 Dec 2003 22:03:53 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: How to open a file from the end and read the last 100 lines
Message-Id: <20031206170353.1f46376e.jwillmore@remove.adelphia.net>

On 6 Dec 2003 07:13:55 -0800
genericax@hotmail.com (Sara) wrote:
> 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>...
<snip>
> 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..

That statement is uncalled for.  I don't even view it as funny - if
that was the goal of the statement.

One of the main concepts of the Perl culture is laziness.  That
doesn't mean don't work :-)  It means don't do more work than is
absolutely necessary.  So ... if you can do it with a module, then
that should be the route to take.  Why re-invent the wheel?

For example - you *can* code SQL to operate with a vendor interface,
but that's not idiomatic Perl.  It *is* idiomatic Perl to use the DBI
module.  It also makes for less work *and* more reliable code.  *Most*
modules worth thier salt are tested and time proven.  Rolling your own
is not.  Unless you're learning, want to develop something for CPAN
(in which case, it *may* become tested and time proven one day), or
just want to hack something together just for the heck of it.

HTH and have a nice day :-)

-- 
Jim

Copyright notice: all code written by the author in this post is
 released under the GPL. http://www.gnu.org/licenses/gpl.txt 
for more information.

a fortune quote ...
Innovation is hard to schedule.   -- Dan Fylstra 



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

Date: 7 Dec 2003 00:58:49 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How to open a file from the end and read the last 100 lines
Message-Id: <bqtu09$ip2$1@mamenchi.zrz.TU-Berlin.DE>

Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
> 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.
>
> Oh yes a small mod for that trivial case:
> 
>  splice @l, @l-100 if @l > 100;

Don't shrug off trivial bugs.  In a production program, this kind of
bug can go unnoticed for a long time.  Then the program fails for no
good reason, perhaps because it's called a few times a day instead of
just once.  Not good.  CPAN modules don't *have* trivial bugs like that.

> 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..

Oh, come on.  You offered your code as a solution, after a dismissive
glance at possible CPAN modules.  Be prepared to be measured against
them.

Anno


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

Date: Sun, 07 Dec 2003 01:01:45 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: How to open a file from the end and read the last 100 lines
Message-Id: <x7brqlsao7.fsf@mail.sysarch.com>

>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:

  AS> Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
  >> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message
  >> news:<bqs8md$hfo$1@mamenchi.zrz.TU-Berlin.DE>...
  >> > > 
  >> > >    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.
  >> 
  >> Oh yes a small mod for that trivial case:
  >> 
  >> splice @l, @l-100 if @l > 100;

  AS> Don't shrug off trivial bugs.  In a production program, this kind of
  AS> bug can go unnoticed for a long time.  Then the program fails for no
  AS> good reason, perhaps because it's called a few times a day instead of
  AS> just once.  Not good.  CPAN modules don't *have* trivial bugs like that.

and my module is much faster than her code as well. slurping in a whole
file to get last 100 lines is a waste of ram and cpu. and if the file is
a large log, forget it. sara will just have to learn that rolling your
own all the time is fruitless. the ultimate result is write your own in
c because perl is just a large c based application. 

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 6 Dec 2003 15:29:33 -0800
From: canle@lecan.com (Cle)
Subject: Re: How to write to drive A:\ from CGI Perl
Message-Id: <8a21e493.0312061529.be1aaed@posting.google.com>

Hi!

Thank you all for your helps.

My codes from html file to access my drive A which is working OK:
file:///A|/

My sample in Perl script which is also working OK:
http://www.lecan.com/cgi-bin/cgiwrap/canle/hw3mcgi.pl#EST

This is output stored in Telnet server at my website directory after I
run above script, but I wanted to write below output to drive A of my
computer:

Book List Database Sample 
A2 T2 P2 E2 I2 30 
b bt bp be bi 15 
AA TT PP EE II 20 
aa tt pp ee ii 33 
--------

This is what was appeared in my telnet home directory when I wrote to
C:
C:Documents and SettingsMyName.COMPNameDesktopOutputFile.txt

I am trying to test your codes.

Thank you

Cle


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

Date: Sat, 6 Dec 2003 12:17:31 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <Pine.A41.4.58.0312061202450.13228@ginger.libs.uga.edu>

On Fri, 5 Dec 2003, Anno Siegel wrote:
> The code can be simplified a bit with re-introduction of a plain scalar $i.
> In particular, we can lose the pesky initialization to -1.
>
>     my %i;
>
>     sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }
>
>     sub them (;\@\@\@\@\@) {
>         my $i = $i{ join "", @_} ++; # increment for next time, use current
>         rewind(@_), return if grep { $i >= @$_ } @_;
>         ( $i, map $_->[$i], @_);
>     }
>

You guys are something else.  It only just occurred to me that you've
provided for the following to work:

my @aa = qw( a b c d e f );
my @nn = qw( 1 2 3 4 );
my @xx = qw( x y z );

print them( @aa, @nn, @xx ), "\n";
print these( \@aa, \@nn, \@xx ), "\n";
print them( @aa, @nn, @xx ), "\n";

sub these {
    my( $q, $w, $e ) = @_;
    them( @$q, @$w, @$e );
}

Nice.  I wonder if it would be useful to have a query for the position.
(I had to fiddle with the order of things ...)

{{
my %i;

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub saw (;\@\@\@\@\@) { ($i{ join "", @_}||0) - 1 }

sub them (;\@\@\@\@\@) {
    my $s = join "", @_;
    my $i = $i{ $s }||0;
    rewind(@_), return if grep { $i >= @$_ } @_;
    $i{ $s } ++;
    ( $i, map $_->[$i], @_);
}
}}

my @aa = qw( a b c d e f );
my @nn = qw( 1 2 3 4 );
my @xx = qw( x y z );

print "saw: ", saw( @aa, @nn, @xx ), "\n";

while( my ( $i, $a, $n, $x ) = them( @aa, @nn, @xx ) ) {
    print "$i, $a, $n, $x\n";
    print "saw: ", saw( @aa, @nn, @xx ), "\n";
}

print "saw: ", saw( @aa, @nn, @xx ), "\n";
_____
saw: -1
0, a, 1, x
saw: 0
1, b, 2, y
saw: 1
2, c, 3, z
saw: 2
saw: 2

Regards,

Brad


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

Date: Sat, 6 Dec 2003 14:01:15 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <Pine.A41.4.58.0312061353570.16468@ginger.libs.uga.edu>

On Sat, 6 Dec 2003, Brad Baxter wrote:
> {{
> my %i;
>
> sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }
>
> sub saw (;\@\@\@\@\@) { ($i{ join "", @_}||0) - 1 }
>
> sub them (;\@\@\@\@\@) {
>     my $s = join "", @_;
>     my $i = $i{ $s }||0;
>     rewind(@_), return if grep { $i >= @$_ } @_;
>     $i{ $s } ++;
>     ( $i, map $_->[$i], @_);
> }
> }}


Pardon the autofollowup.  That rewind(@_) isn't quite right.  Here's a
reason for using '&'.

sub them (;\@\@\@\@\@) {
    my $s = join "", @_;
    my $i = $i{ $s }||0;
    &rewind, return if grep { $i >= @$_ } @_;
    $i{ $s } ++;
    ( $i, map $_->[$i], @_);
}

Regards,

Brad


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

Date: Sat, 06 Dec 2003 23:31:48 GMT
From: R.Mariotti(at)FinancialDataCorp.com (Bob Mariotti)
Subject: Ongoing LWP impass...  advise please?
Message-Id: <3fd26503.210048609@news.cshore.com>

Thanks for the several responses I have received all of which I have
tried.  However, the program seems to fail during executing always at
the same spot.  I receive NO error messages, but I receive NO response
other than the html page being sent to me again.  I would assume this
indicates an error so retry.

Here is my code snippet.   How would one go about altering this to
make it work?
======================================================

my $URL2="https://www.blah.com";
my $DATA="";
my $fname="filename";

open(TFH,"<$filename.in") or die "Cannot Open $filename.in\n";
while(<TFH>) {
$DATA.=chomp($_);
}
close(TFH);

$REQ= POST $URL2, 
	[ NETCONNECT_TRANSACTION => $DATA ];

$RSP=$UA->request($REQ, "$filename.out");


=====================================================

The above request should be a rather normal POST request and the
content should be the descriptor "NETCONNECT_TRANSACTION=" and the
field $DATA is the entire contents of an XML formatted file.  There is
also a cookie automatically attached to the request that was
previously retrieved after authenticating with the remote host.

The request line should write the response received from the remote
host into the file $filename.out.

Any guidance, advise, code revisions, etc. will be G-R-E-A-T-L-Y
appreciated.

Bob


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

Date: Sat, 6 Dec 2003 17:59:35 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Ongoing LWP impass...  advise please?
Message-Id: <slrnbt4rb7.b8k.tadmc@magna.augustmail.com>

at <R.MariottiFinancialDataCorp.com> wrote:

> How would one go about altering this to
> make it work?

> while(<TFH>) {
> $DATA.=chomp($_);
> }


Have a look at what value you have collected at this point:

   print $DATA;

Then you might figure that you're goint to have to read the
documentation for the function that you are using to find out
what it returns:

   perldoc -f chomp

The 2nd sentence should help solve your problem.



To my mind, the return value from chomp() is not useful, so it is a 
red flag to me when I see the return value from chomp being used.  :-)


> Any guidance, advise, code revisions, etc. will be G-R-E-A-T-L-Y
> appreciated.


Use the docs, Luke.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 06 Dec 2003 19:15:31 GMT
From: "Picker Leon" <Temp@NoSuchDomain.Info>
Subject: Please Explain the Following Anonymous Sub
Message-Id: <nVpAb.403654$0v4.19980961@bgtnsc04-news.ops.worldnet.att.net>

##code with problems
#!/usr/bin/perl -w
    use warnings;
    use diagnostics;
  use HTML::LinkExtor;

$input='<a href=hreflink></a> <img src=imglink>';

getlink ('img');
getlink ('a');

sub getlink{
  my $matchkey = $_[0];
  print "matchkey outsite callback $matchkey \n";

  sub callback {
     print "matchkey inside callback $matchkey\n";
     return; #WHAT IS THE RETURN HERE FOR?
  }

  my $p = HTML::LinkExtor->new(\&callback);
  $p->parse($input);
}
**************
output:
matchkey outsite callback img
matchkey inside callback img
matchkey inside callback img
matchkey outsite callback a
matchkey inside callback img
matchkey inside callback img
**************
I was told to change lines to those:
my $callback = sub { print "matchkey in $matchkey\n"; }; #WHY NO MORE
RETURN?
my $p = HTML::LinkExtor->new($callback);
*************
Here is the explain from perl:
    (W closure) An inner (nested) named subroutine is referencing a
    lexical variable defined in an outer subroutine.

    When the inner subroutine is called, it will probably see the value of
    the outer subroutine's variable as it was before and during the *first*
    call to the outer subroutine; in this case, after the first call to the
    outer subroutine is complete, the inner and outer subroutines will no
    longer share a common value for the variable.  In other words, the
    variable will no longer be shared.

    Furthermore, if the outer subroutine is anonymous and references a
    lexical variable outside itself, then the outer and inner subroutines
    will never share the given variable.

    This problem can usually be solved by making the inner subroutine
    anonymous, using the sub {} syntax.  When inner anonymous subs that
    reference variables in outer subroutines are called or referenced, they
    are automatically rebound to the current values of such variables.
************
What I don't understand is when I delete my, then it works perfectly. I
copied the code from linkextor, but apparently it only works if I use globle
varible not my varibles. I think linkextor should change their sample codes.
Please update it, Larry.
MY QUESTION IS WHY INNER AND OUTTER ARE NOT SHAREING THE MY VARIBLE? I THINK
LARRY SHOULD CHANGE THE BEHAVOUR OF MY TOO.





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

Date: Sat, 6 Dec 2003 12:02:03 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: Please Explain the Following Anonymous Sub
Message-Id: <rjctqb.pp2.ln@goaway.wombat.san-francisco.ca.us>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

On 2003-12-06, Picker Leon <Temp@NoSuchDomain.Info> wrote:
[snip]
> What I don't understand is when I delete my, then it works perfectly.

Which my?  You have two in your original code, and the new line of code
suggested to you also has a my.  If you want help, perhaps you should be
more specific.

> I copied the code from linkextor, but apparently it only works if I use globle
> varible not my varibles. I think linkextor should change their sample codes.
> Please update it, Larry.

Did you even read perldoc HTML::LinkExtor?  I think you'll find that
Larry doesn't maintain that module.

How do you suggest HTML::LinkExtor change its behaviour?  You don't
appear to have found any sort of a bug, except in your own code.

> MY QUESTION IS WHY INNER AND OUTTER ARE NOT SHAREING THE MY VARIBLE? I THINK
> LARRY SHOULD CHANGE THE BEHAVOUR OF MY TOO.

IF YOU THINK IT'S THAT EASY TO CHANGE PERL'S BEHAVIOUR YOU SHOULD DO IT
YOURSELF.

And please stop shouting.

I haven't looked at the code for this particular quirk, but it is
probably sufficiently hairy that simply changing this particular
behaviour, and nothing else, is very much nontrivial.  A few posts ago
you didn't even understand the differences between my, our, and local,
and now you're suggesting changes in my?

Finally, I'm sure that many of the more knowledgeable folks have either
already killfiled you or are considering doing so.  If you wish to
continue to receive help, I'd suggest you think more carefully when you
compose your posts, to make sure you've done sufficient research into
your problem and that you're being sufficiently clear in what you think
is wrong.

- --keith

- -- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/0jW4hVcNCxZ5ID8RAh/4AJ4ydyb4UM+OFrQ3cuh7qJ8UUG26bgCgicO0
aFKcQI3Zygq/lRv84fhrwmg=
=mfZR
-----END PGP SIGNATURE-----


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

Date: Sat, 06 Dec 2003 20:27:43 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Please Explain the Following Anonymous Sub
Message-Id: <3fd22f4d.184467588@news.erols.com>

"Picker Leon" <Temp@NoSuchDomain.Info> wrote:

: ##code with problems
: #!/usr/bin/perl -w
:     use warnings;
:     use diagnostics;
:   use HTML::LinkExtor;
: 
: $input='<a href=hreflink></a> <img src=imglink>';
: 
: getlink ('img');
: getlink ('a');
: 
: sub getlink{
:   my $matchkey = $_[0];
:   print "matchkey outsite callback $matchkey \n";
: 
:   sub callback {
:      print "matchkey inside callback $matchkey\n";
:      return; #WHAT IS THE RETURN HERE FOR?
:   }
: 
:   my $p = HTML::LinkExtor->new(\&callback);
:   $p->parse($input);
: }
: **************
: output:
: matchkey outsite callback img
: matchkey inside callback img
: matchkey inside callback img
: matchkey outsite callback a
: matchkey inside callback img
: matchkey inside callback img
: **************
: I was told to change lines to those:
: my $callback = sub { print "matchkey in $matchkey\n"; }; #WHY NO MORE
: RETURN?

You're focusing on a trivial and irrelevant difference there.

With or without return(), control flow goes back to the calling statement.
If it's really bothering you, perldoc -f return explains what happens
differently with an explicit return() and without one.

: my $p = HTML::LinkExtor->new($callback);
: *************
: Here is the explain from perl:
:     (W closure) An inner (nested) named subroutine is referencing a
:     lexical variable defined in an outer subroutine.
: 
:     When the inner subroutine is called, it will probably see the value of
:     the outer subroutine's variable as it was before and during the *first*
:     call to the outer subroutine; in this case, after the first call to the
:     outer subroutine is complete, the inner and outer subroutines will no
:     longer share a common value for the variable.  In other words, the
:     variable will no longer be shared.
: 
:     Furthermore, if the outer subroutine is anonymous and references a
:     lexical variable outside itself, then the outer and inner subroutines
:     will never share the given variable.
: 
:     This problem can usually be solved by making the inner subroutine
:     anonymous, using the sub {} syntax.  When inner anonymous subs that
:     reference variables in outer subroutines are called or referenced, they
:     are automatically rebound to the current values of such variables.
: ************
: What I don't understand is when I delete my, then it works perfectly.

There's not much to add to the diagnostics output.

Subroutine getlink() assigns a value to $matchkey.
The nested subroutine callback() will see that value of $matchkey, and it
will never recognize when getlink() changes it.

Don't nest named subroutines.  It's that simple.

: I copied the code from linkextor,

I see no code in the HTML::LinkExtor documentation remotely resembling what
you have written above.  What version of HTML::LinkExtor are you looking
at?  Is there a URL available for us to see *exactly* where you got that
code?

: but apparently it only works if I use globle varible not my varibles.

That's true for the way you have it written, with the nested subroutine.

my() variables will work splendidly if you un-nest the callback()
subroutine and declare $matchkey in a scope enclosing both getlink() and
callback().

    { # bare block to limit the scope of $matchkey
        my $matchkey;

        sub getlink{
            $matchkey = $_[0];
            print "matchkey outsite callback $matchkey \n";
            my $p = HTML::LinkExtor->new(\&callback);
            $p->parse($input);
        }

        sub callback {
            print "matchkey inside callback $matchkey\n";
        }
    }

: I think linkextor should change their sample codes. Please update it, 
: Larry.

Are you sure you're looking at a current version of the HTML::LinkExtor
documentation?

Larry is not the author of HTML::LinkExtor.  The module's documentation
itself attributes copyright to Gisle Aas.

: MY QUESTION IS WHY INNER AND OUTTER ARE NOT SHAREING THE MY VARIBLE?

Please stop shouting.

: I THINK LARRY SHOULD CHANGE THE BEHAVOUR OF MY TOO.

Just because you can't get a grip on the documented behavior of how a
nested subroutine handles a lexical variable defined in the enclosing
subroutine?  Fat chance.



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

Date: Sat, 06 Dec 2003 22:12:50 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: q on 'record', 'field' separators in text files
Message-Id: <20031206171250.688bd98f.jwillmore@remove.adelphia.net>

On Sat, 6 Dec 2003 08:58:52 -0600
sparkane <nertz@numb.no> wrote:

> 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.)

More often than not, ':' and ',' are used for text (aka CSV or "flat
file") databases.  Use of other characters *may* lead to issues you
may not have thought of.  One may be you leaving your organization and
someone else having to pick up where you left off.  Or, you starting a
project, not having the time to maintain it, and then someone else
having to pick up where you left off.  Or, just Father Time.  I know
I've written code from a few years ago, did something "funky", and not
having an idea what the logic was behind it.  I code much better now,
so I don't run into that situation too often.  But, it is something to
consider.

Just a suggestion - you *may* want to consider using DBI for database
functionality.  One driver available is DBD::CSV - which *may* fit the
bill for you.

HTH

-- 
Jim

Copyright notice: all code written by the author in this post is
 released under the GPL. http://www.gnu.org/licenses/gpl.txt 
for more information.

a fortune quote ...
It has been said that man is a rational animal.  All my life I
<have been searching for evidence which could support this.   -- 
Bertrand Russell 


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

Date: Sat, 06 Dec 2003 18:58:37 GMT
From: "Picker Leon" <Temp@NoSuchDomain.Info>
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <xFpAb.403581$0v4.19978236@bgtnsc04-news.ops.worldnet.att.net>

How did you know too much about Perl?
I read the perl documents, but find nothing near to what you know. Do you
have super book or super site or something?
The more codes I wrote in perl now and the more problems I am facing.
Before, I can basically find all the answers in perldoc, but now I feel the
explaination in it is not enough for me to understand.




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

Date: 6 Dec 2003 21:08:14 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <bqtgfu$h51$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Picker Leon:

> How did you know too much about Perl?
> I read the perl documents, but find nothing near to what you know. Do you
> have super book or super site or something?

Neither of that. I am using the same documentation others use. The
information are all scattered somewhere in the perldocs. 

Another invaluable source besides the standard documentation is this
newsgroup. Some of the most infamous Perl hackers post here regularly. 

> The more codes I wrote in perl now and the more problems I am facing.
> Before, I can basically find all the answers in perldoc, but now I feel the
> explaination in it is not enough for me to understand.

Maybe you are too impatient. Perl is quite feature-rich and you happen
to occupy with some of the more interesting things right at the start.
Some might suggest to take a gentler approch. However, practice, the
occasional read in the perldocs (even if you don't understand all of it)
and in this group is enough to teach you some decent Perl skills.

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


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