[21937] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4159 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 22 03:05:43 2002

Date: Fri, 22 Nov 2002 00: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           Fri, 22 Nov 2002     Volume: 10 Number: 4159

Today's topics:
    Re: BEGIN block and $:: variables <dave@dave.org.uk>
    Re: Convert form input by using a hash <mgjv@tradingpost.com.au>
    Re: disambiguating print (was Re: Basic syntax question <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
    Re: help this Newbie in Distress (Villy Kruse)
    Re: How to print whatever text in Perl? (Peter Wu)
    Re: How to print whatever text in Perl? (Tad McClellan)
    Re: Missing ..\Perl\lib\auto\Mail\Internet\autosplit.ix <goldbb2@earthlink.net>
        Ordering of command line switches (Matt Knecht)
    Re: passing address between classes - OO type question (Tad McClellan)
    Re: regex subroutine \) passing variables (Tad McClellan)
    Re: regexp help (Tad McClellan)
    Re: shtml and Perl <jim.bloggs@eudoramail.com>
    Re: shtml and Perl <stevenm@blackwater-pacific.com>
    Re: shtml and Perl <stevenm@blackwater-pacific.com>
    Re: shtml and Perl <goldbb2@earthlink.net>
    Re: Sorting an array <goldbb2@earthlink.net>
    Re: Way to print caller() info from first to last inste <goldbb2@earthlink.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 22 Nov 2002 08:03:36 +0000
From: "Dave Cross" <dave@dave.org.uk>
Subject: Re: BEGIN block and $:: variables
Message-Id: <pan.2002.11.22.08.03.33.303476@dave.org.uk>

On Thu, 21 Nov 2002 11:55:00 +0000, Kevin Brownhill wrote:

> "Dave Cross" <dave@dave.org.uk> wrote in message
> news:pan.2002.11.21.11.31.21.612732@dave.org.uk...
>> On Thu, 21 Nov 2002 11:08:25 +0000, Kevin Brownhill wrote:
>>
>> > Can anyone explain what the following code section does - it is the
>> > first bit of code in a subroutine.
>> >
>> > I've looked in all the documentation I can find and still can't
>> > understand it.
>> >
>> > BEGIN
>> >  {
>> >  $::pi = 4 * atan2(1,1);
>> >  }
>>
>> It sets up a package variable called $pi and gives it the appropriate
>> value. And it does it at compile time, not runtime.
>>
> Thanks, that explains it.
> 
> Does the $::pi package variable need to be declared anywhere with my ?

[Please don't top-post. I've re-ordered the discussion so it makes sense.]

Er... no. Like I said it's a _package_ variable. Using "my" would create a
new lexical variable of the same name.

Dave...

-- 
  .sig missing...



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

Date: Fri, 22 Nov 2002 05:43:32 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Convert form input by using a hash
Message-Id: <slrnatrh4f.mmn.mgjv@verbruggen.comdyn.com.au>

On Fri, 22 Nov 2002 03:03:17 GMT,
	J.C.Castro <jccastro@osite.com.br> wrote:
> The script below is an example of what I want to do, although I will
> not work with month names. But it has something wrong.

That is a very wwak description of your problem. _What_ do you want to
achieve, in words, and _how_ is the program below not achieving that?

> #!/usr/bin/perl

#!/usr/bin/perl -w
use strict;

and since this is a CGI program:

use CGI;
use CGI::Carp 'fatalsToBrowser';

People here do not like doing the work of computers. The above will
help your computer do the work that you want us to do.

> &hent_input;
> &conversion;

Don't invoke subroutines like that, unless you're still using perl 4.
Use

hent_input();
conversion();

instead. See the perlsub documentation for the difference.

> %longMonthName = ("Jan", "January",
>     "Feb", "February",
>     "Mar", "March",
>     "Apr", "April",
>     "May", "May",
>     "Jun", "June",
>     "Jul", "July",
>     "Aug", "August",
>     "Sep", "September",
>     "Oct", "October",
>     "Nov", "November",
>     "Dec", "December");

More idiomatic is something like:

my %longMonthName = (
    Jan => "January",
    Feb => "February",
    #etc..
);

> $test = $input{n};

You're using hidden global variables. Bad idea. It makes
debugging very hard.

Case in point:

You already called the subroutine conversion earlier, at the top of
the program. It is the only place, besides here, where the global
$test is used. However, $test is not set to anything, until here. You
need to set it between the two calls to the subroutines, or even
better, not use global variables this way. Use arguments to your
subroutines, and use return values.

> sub hent_input {
> 
> 	if ( $ENV{'REQUEST_METHOD'} eq "GET")  {
> 		$input = $ENV{'QUERY_STRING'};
> 	} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
> 		read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
> 	}

The CGI module will do all of this for you, and will do it with code
that has been debugged over many years.

> 	@input = split (/&/, $input);
> 	foreach (@input) {
> 		s/\+/" "/g;
> 		($name, $value) = split (/=/,$_); 
> 
> 		$name =~s/%(..)/pack("c",hex($1))/ge; 
> 		$value =~s/%(..)/pack("c",hex($1))/ge; 
> 		$input{$name} .= "\0" if (defined($input{$name}));
> 		$input{$name} .= $value;
> 	}
> }

And it will do it correctly.

You're using $input, @input and %input. Could become pretty confusing.
none of them are scoped in any way, and %input is the only one used
globally.

What is that stuff with the null character anyway? What are you doing
there? Do you know that you can have complex data structures in Perl?
See the perllol and perldsc documentation for some examples. I think
you're testing whether $input{$name} already exists, which you should
do with exists(), not defined().

> sub conversion {
> print "Content-type: text/html\n\n";
> print "$longMonthName{$test}\n";

That's not HTML. you're outputting a content header, folllowed by
invalid content.

> }
> ________________________________________________________________
> 
> I know that this would work:
> $test = 'Nov';
> print "$longMonthName{$test}\n";
> 
> And this also would work:
> print "$input{n}\n";
> 
> But I don't know how to use $longMonthName{$test} when $test =
> $input{n}

%input;
$input{n} = 'Nov';
$test = $input{n};
$longMonth = $longMonthName{$test};

or directly

$longMonth = $longMonthName{$input{n}};

What exactly is the problem? Are you sure that you're setting the
parameter 'n'?

I'm pretty sure most of your code could be replaced with:

#!/usr/local/bin/perl -w
use strict;
use CGI qw/:standard/;

my %longMonthName = (
    Jan => "January",
    Feb => "February",
    Mar => "March",
    Apr => "April",
    May => "May",
    Jun => "June",
    Jul => "July",
    Aug => "August",
    Sep => "September",
    Oct => "October",
    Nov => "November",
    Dec => "December"
);

my $longMonth = param('n');
print header, start_html, p($longMonthName{$longMonth}), end_html;
__END__

If only I knew what you were trying to do with that null character.



Martien
-- 
                        | 
Martien Verbruggen      | 
Trading Post Australia  | 42.6% of statistics is made up on the spot.
                        | 


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

Date: Fri, 22 Nov 2002 07:27:44 +0100
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: disambiguating print (was Re: Basic syntax question on using arrays returned from function)
Message-Id: <newscache$8msy5h$qg8$1@news.emea.compuware.com>

Tad McClellan wrote (Thursday 21 November 2002 16:48):


> Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com> wrote:


>> Edward Wildgoose wrote (Thursday 21 November 2002 14:44):


> [somebody snipped:  using
> 
>    print +(), ...;
> 
>  to disambiguate the extent of print()'s argument list.
> ]
> 
> 


>>> Now I need to read up on what the "+" does,


>> "Everything is not as difficult as it seems" -- or sort of.
>> + is just plain arithmetic.


> It might be interesting to find out, but I think it is a poor
> choice over the alternative (see below).
> 
> I never ever choose that method of disambiguating (because it
> makes me pause and remember what unary plus does, and why I am
> using it there).
[excellent explaination skipped]


Truth be told Tad, I actuallay never have seen it implemented that way or 
used it myself either. But is saw it in the docs as a alternative. So I 
thought, what the heck, "Go ahead, make my post..."
And I just knew clpm would be *all* over me for posting weird stuff :-)
Thanks for the excellent clarification.

-- 
KP



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

Date: 22 Nov 2002 07:47:25 GMT
From: vek@station02.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: help this Newbie in Distress
Message-Id: <slrnatro8d.3i8.vek@station02.ohout.pharmapartners.nl>

On Thu, 21 Nov 2002 16:58:53 -0600,
    Bob Dover <dover@nortelnetworks.com> wrote:


>Another problem often seen in this NG (but not flamed as much as
>top-posting) is the habit some have of quoting an entire article only
>to comment on a line or two at the end.
>



I do beleive that is what they are realy complaining about, that is,
a short remark follow by a series of quoted copies of previous messages,
most of which is unrelated to the remark.  Keeping the same amount of
quoted material and adding a short remark at the bottom is just as bad
(IMHO).

We might wish to go back to the old rule: quoted material may never
be more than half of the entire post.  Older software enforced that.



Villy


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

Date: 21 Nov 2002 21:06:22 -0800
From: peterwu@hotmail.com (Peter Wu)
Subject: Re: How to print whatever text in Perl?
Message-Id: <9acc2ac1.0211212106.7a0c658c@posting.google.com>

Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net> wrote:
> In article <9acc2ac1.0211210133.5e165930@posting.google.com>, Peter Wu wrote:
> > Hello,
> > 
> > My program reads the all contents of a ASCII file and prints them out.
> > I notice that when there is something like $/ $. $), those characters
> > disappear.
> > 
> > A simple sample:
> > 
> > #!/usr/bin/perl 
> > 
> > $buffer =<<END_OF_BUFFER; 
> > This is a test message ending with a $. 
> > 
> > END_OF_BUFFER 
> > 
> > print $buffer; # The $ disappears but I want it. :s
> > 
> > 
> > I know that they are probably control characters in Perl but how to
> > print them out "AS IS"? As the file is quite big, is there anyway to
> > make it happen without doing the substitution of those special
> > characters? TIA!
> 
> 
> Either "escape" the special characters, i.e.:
> 
> -------
> $buffer =<<END_OF_BUFFER;
> This is a test message ending with a \$. 
> 
> END_OF_BUFFER
> -------
> 
> 
> or use single quotes:
> 
> 
> -------
> $buffer =<<'END_OF_BUFFER';
> This is a test message ending with a $.
> 
> END_OF_BUFFER
> -------
> 

Thanks for the help!

This works great if we deal with a string constant. 

How to deal with a string variable that contains those characters?

Say, I already have a variable $message, which contains the string value.

abcd$.

How to print $message as is? Thanks!

- Peter


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

Date: Thu, 21 Nov 2002 23:18:15 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: How to print whatever text in Perl?
Message-Id: <slrnatrfgn.24r.tadmc@magna.augustmail.com>

Peter Wu <peterwu@hotmail.com> wrote:

> Say, I already have a variable $message, which contains the string value.
> 
> abcd$.


Why not "say" it in Perl so that there will be no ambiguity?

   my $message = 'abcd$';


> How to print $message as is? Thanks!


Errr,

   print $message;

?



Do you understand the distinction between "code" and "data"?


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


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

Date: Fri, 22 Nov 2002 02:37:37 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Missing ..\Perl\lib\auto\Mail\Internet\autosplit.ix
Message-Id: <3DDDDEC0.9E1C22E0@earthlink.net>

Fred wrote:
> 
> I am trying to use MIME::Parser in Win98SE in a script.
> But I get an error
> 
> "Cant' locate auto/Mail/Internet/autosplit.ix in @inc"
>  at C:/../Perl/lib/Autoloader.pm line 146
>  at C:/../Perl/lib/Mail/Internet.pm line 14
> 
> Execution is not aborted.
> 
> I cannot find auto/Mail/Internet/autosplit.ix anywhere on
> http://www.activestate.com/ or on http://cpan.valueclick.com/modules/
> or with Google-search.

This is because auto/.../autosplit.ix is a file created when you run the
installation procedudure for the Mail-Internet.  If you had properly
installed that distribution (using the 'install' command from any of
PPM, or the CPAN shell, or the CPANPLUS shell), then the file would
exist.

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Fri, 22 Nov 2002 07:11:08 GMT
From: hex@voicenet.com (Matt Knecht)
Subject: Ordering of command line switches
Message-Id: <slrnatrm3p.92p.hex@unix01.voicenet.com>


Getting the ordering of command line switches to get expected
behaviour has been an exercise in trial and error.

The chunk of code I'm working on sets a user's passwd to their
username, and is called as a shell function:

update_shadow()
{
    perl -lnaF: -i -e "
        BEGIN { @s = (0..9, 'A'..'Z', 'a'..'z', '.', '/'); };
        \$F[2] = 0, \$F[1] = crypt('$1', $s[rand @s] . $s[rand @s]) if /^$1:/;
        print +join ':', @F[0..7];
    " /etc/shadow
}

From a purely aesthetic sense, I'd like to pack the switches as
tightly as possible.  -lnaF:ie doesn't do the trick.  Any suggestions?

-- 
Matt Knecht <hex@voicenet.com>


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

Date: Thu, 21 Nov 2002 23:00:29 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: passing address between classes - OO type question
Message-Id: <slrnatrefd.1pg.tadmc@magna.augustmail.com>

kit <manutd_kit@yahoo.com> wrote:

>> Your definition of Kid->new() makes no use of its arguments,
>> so why call it with arguments?
> 
> The reason why I call Kid->new() with arguments is trying to mimic the
> idea of explicit constructor. 


Then you need to modify the constructor to make use of the
argument, if it is present.


> If the call is like this,
> 
>    Kid->new($name); 
> 
> then, it could change 
                 ^^^^^^

You can't "change" the name. This is the _constructor_, the
kid has no name, because there is no kid (until we finish
constructing it).

I expect you meant "set the name" instead?


> the data member <name>. Is there any features in
> perl to solve this problem?


I'm not too sure what "this problem" is.

There is a solution to the problem of defaulting the name
if no argument is provided in the constructor call:


   my $name = shift || 'Kit';
   ...
   my $self = {
       name => $name;
   }


>> You have a poor design. The kid's name is stored in two
>> places, in the Kid object AND in Mum's kid{} hash.
>>
>> So you need to change it in both places.
>  
> Do you mean it could be better off if I can take away the <name>
> datamember in the Kid object, and instead 


Eh? It is _already_ in the Mum too.


> put it in the Mum's kid{}
> hash?


Heavens no! That would make it worse rather than better.

The "name" is an attribute of the child, not of the mother, so
it should be in the child object.


I meant (and said, but you snipped that) that you should use
an anonymous array for $self{kid} instead of an anon hash.

The problem is storing the name in 2 places. The solution is
to remove the name from one of the places.

Have just an array of Kid objects in Mum, no names (hash keys).

This has the added benefit of modeling the birth order of Mum's Kids.


>    Number of arguments for $self
>    When we do,
>      my ($self, $a) = @_;
>    How many arguments can we have??


As many as your (virtual) memory will allow you to have.

How many do you want to have?


> ----This is for your reference----Kid.pm----

> ----Mum.pm----
> package Mum;

> sub new {
>         my $class = shift;
> 	my $self = {
> 		name => 'myMum',
> 		age => 40,
>                 kid => {},


   kid => [],   # now it's an anon array


> 	};
> 	return bless $self, $class;
> }
> sub giveBirth {
>   my ($self, $name) = @_;
>   ${$self->{kid}}{$name} = Kid->new();


   push @{$self->{kid}}, Kid->new($name);  # so you populate it differently


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


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

Date: Thu, 21 Nov 2002 22:32:22 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: regex subroutine \) passing variables
Message-Id: <slrnatrcqm.1pg.tadmc@magna.augustmail.com>

Lance Hoffmeyer <lance@augustmail.com> wrote:

> Subject: regex subroutine \) passing variables


Your problem has nothing to do with subroutines, nor with
passing variables.

Your problem has to do with quoting strings.


> I keep trying to run this subroutine.
> It keeps bombing out on the second item
> 
> "\(\+5\) VERY"
> 
> Quantifier follows nothing before HERE 2 (+ <<HERE
> if I remove the "+" I get error
> Unmatched ( before HERE   ( <<HERE
> 
> 
> So, I guess 


No need for that. If you guess wrong you'll waste time.

See for yourself what the regex engine will end up seeing:

   print "\(\+5\) VERY";

(then try it with single quotes.)


> my slashs are not being passed and a \+ is being interpreted
> as a + and a \( is being interpreted as a (.


Right.


> How do I correct this?


Use single quotes instead of double quotes.


> sub find_num{

[snip]

> return my $num = $1;


That has the same effect as:

   return $1;


Why do you declare $num?


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


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

Date: Thu, 21 Nov 2002 22:22:57 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: regexp help
Message-Id: <slrnatrc91.1pg.tadmc@magna.augustmail.com>

Lance Hoffmeyer <lance@augustmail.com> wrote:

> My question concerns the part ?:(\d{1,2}\.\d)\D+{$colnum}
                                ^^
                                ^^
Those should have gone along with the open parenthesis.

   (?: ... )

is the same as

   ( ... )

except it does not capture to memory.

The token for this kind of open paren is all three characters  (?: 


> I need to incorporate an "or" in this so that this part can be
> \d\d.\d or "-" (a dash). followed by any non-numeric data


   /(?:-|(\d{1,2}\.\d)\D+)/

(no m//s option needed for this regex.)

or

   /(?: - | (\d{1,2}\.\d) \D+ )/x


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


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

Date: Fri, 22 Nov 2002 06:10:22 -0000
From: "doofus" <jim.bloggs@eudoramail.com>
Subject: Re: shtml and Perl
Message-Id: <arkhog$jfimp$1@ID-150435.news.dfncis.de>

News wrote:
> Got a question, I have a perl script that calls a shtml file.
> In the shtml file I have a "<!--#include virtual= "  function. If I
> run the shtml file by itself, it works fine but if I run it through
> the perl script, the include doesn't work.
> Any ideas? Am I missing a server config?

I don't _think_ you can do that. I assume you're using Apache. My
understanding is that you get to use either includes or perl but not
both. Probably better to just make the perl proggie do the including its
little old self.

Basically Apache gets one shot at parsing your request as something else
and then processing it. You're trying to make it do two at once. But I
may be wrong. Dunno. Others may confirm or deny. :(

--
best, doofus




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

Date: Thu, 21 Nov 2002 23:05:49 -0800
From: Steven May <stevenm@blackwater-pacific.com>
Subject: Re: shtml and Perl
Message-Id: <arkkna$8qu$1@quark.scn.rain.com>

News wrote:
> Got a question, I have a perl script that calls a shtml file.
> In the shtml file I have a "<!--#include virtual= "  function. If I run the
> shtml file by itself, it works fine but if I run it through the perl script,
> the include doesn't work.
> Any ideas? Am I missing a server config?
> 
> Thanks!
> -T
> 
> 

Not really a Perl question, but more than likely this is a path issue....

s.





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

Date: Thu, 21 Nov 2002 23:15:42 -0800
From: Steven May <stevenm@blackwater-pacific.com>
Subject: Re: shtml and Perl
Message-Id: <arkl9t$9pd$1@quark.scn.rain.com>

Steven May wrote:
> News wrote:
> 
>> Got a question, I have a perl script that calls a shtml file.
>> In the shtml file I have a "<!--#include virtual= "  function. If I 
>> run the
>> shtml file by itself, it works fine but if I run it through the perl 
>> script,
>> the include doesn't work.
>> Any ideas? Am I missing a server config?
>>
>> Thanks!
>> -T
>>
>>
> 
> Not really a Perl question, but more than likely this is a path issue....
> 
> s.
> 
> 
> 

It's been a long day..... I was thinking javascript and etc. above and 
didn't realize my mistake until about 1/2 second after hitting the send 
button. Sheesh.

Html generated by a cgi script does NOT get run through the SSI 
processeor, so it's a waste of time to even try shtml markup in such 
pages....

s.



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

Date: Fri, 22 Nov 2002 02:48:18 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: shtml and Perl
Message-Id: <3DDDE142.3217F460@earthlink.net>

Steven May wrote:
> 
> Steven May wrote:
> > News wrote:
> >
> >> Got a question, I have a perl script that calls a shtml file.
> >> In the shtml file I have a "<!--#include virtual= "  function. If I
> >> run the shtml file by itself, it works fine but if I run it through
> >> the perl script, the include doesn't work.
> >> Any ideas? Am I missing a server config?
[snip]
> Html generated by a cgi script does NOT get run through the SSI
> processeor, so it's a waste of time to even try shtml markup in such
> pages....

I think you trick Apache to running the output of the CGI through the
SSI preprocessor, if it's content-type is text/x-server-parsed-html. 
But this is just a vague recollection, and definitely not tested.

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Fri, 22 Nov 2002 02:25:44 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Sorting an array
Message-Id: <3DDDDBF8.F26C5F81@earthlink.net>

Blnukem wrote:
> 
> Hi All
> 
> I'm reading flat file that looks like this into an array.
> 
> "$name $number $year"
> 
> Flat File:
> 
> bill|4|1998
> ray|3|1954
> john|5|1967
> 
> How would I sort the array one the year?

TIMTOWTDI.

Assuming that you've got the data in one array, with "john|5|1967" type
values, then you can do:

@sorted = map $_->[0], sort { $a->[1] <=> $b->[1] }
          map [ $_, /\|(.*?)\|/ ], @unsorted;

Or:

@sorted = map substr($_,4), sort
          map pack("N", /\|(.*?)\|/) . $_, @unsorted

Or:

@years = map /\|(.*?)\|/ ? $1 : die, @unsorted;
@sorted = @unsorted[ sort { $years[$a] <=> $years[$b] } 0..$#years ];

Or various and sundry other ways, depending on your preferences.

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Fri, 22 Nov 2002 02:53:45 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Way to print caller() info from first to last instead of last to first?
Message-Id: <3DDDE289.11132C6C@earthlink.net>

Carlos C. Gonzalez wrote:
> 
> Hi everyone,
> 
> Normally caller() can be called with a number indicating how far back
> in the call trace one wants to acquire information for.  As in the
> following snippet of code...
> 
> my $i = 1; # The previous call - NOT the very first one.
> while (@a = caller($i++))
> {
>   my ($package,$file,$line,$sub) = @a;
>   printf "%s $s $d $s\n", $package, $file, $line, $sub;
> }
> 
> The first call might have been 4 functions back.
> 
> That's all well and good but the last line of info printed out
> corresponds to the first call.  Meaning that one must traverse one's
> way back up the output to find the info for the last call.
> 
> Is there some way to find out how far back in the call trace caller()
> can return info for?

Yes, but only by calling caller for successive values of $i, until it
returns an empty list.

> Such that one could output the call trace from the first to the last
> call?  Resulting in the last call showing up at the bottom of the
> output?
[snip]

Twould be easier to capture the results of the successive calls to
caller, and then reverse them, as so:

my ($i, @a) = (1);
while( my @b = caller($i++) ) {
   push @a, [@b[0..3]];
}
foreach my $c ( reverse @a ) {
   my ($package, $file, $line, $sub) = @$c;
   printf "%s $s $d $s\n", $package, $file, $line, $sub;
}

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

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


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