[28444] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9808 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 5 18:05:42 2006

Date: Thu, 5 Oct 2006 15:05:08 -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           Thu, 5 Oct 2006     Volume: 10 Number: 9808

Today's topics:
        A doubt in using Class::Struct <avinash.ca@gmail.com>
    Re: A doubt in using Class::Struct <mritty@gmail.com>
    Re: A doubt in using Class::Struct <avinash.ca@gmail.com>
    Re: A doubt in using Class::Struct <avinash.ca@gmail.com>
    Re: A doubt in using Class::Struct <avinash.ca@gmail.com>
    Re: A doubt in using Class::Struct <mritty@gmail.com>
    Re: A doubt in using Class::Struct <avinash.ca@gmail.com>
    Re: A doubt in using Class::Struct <mritty@gmail.com>
    Re: A doubt in using Class::Struct <avinash.ca@gmail.com>
    Re: A doubt in using Class::Struct <mritty@gmail.com>
    Re: A doubt in using Class::Struct <brian.helterline@hp.com>
    Re: Find First Date of A Calendar Month\Year anno4000@radom.zrz.tu-berlin.de
    Re: Find First Date of A Calendar Month\Year usenet@DavidFilmer.com
    Re: Find First Date of A Calendar Month\Year usenet@DavidFilmer.com
    Re: How to delete temporary file after displaying in br <brian.helterline@hp.com>
    Re: Kolmogorov-Smirnov test <mark.clementsREMOVETHIS@wanadoo.fr>
        Parsing HTML - using HTML::TreeBuilder olson_ord@yahoo.it
    Re: Parsing HTML - using HTML::TreeBuilder <mritty@gmail.com>
    Re: Parsing HTML - using HTML::TreeBuilder <mritty@gmail.com>
    Re: Problems at writing multiple files <mark.clementsREMOVETHIS@wanadoo.fr>
    Re: Regarding numeric literals chaitask@yahoo.com
    Re: Regarding numeric literals <someone@example.com>
    Re: Regarding numeric literals <mritty@gmail.com>
    Re: Regarding numeric literals chaitask@yahoo.com
    Re: Regarding numeric literals <mritty@gmail.com>
    Re: Regarding numeric literals <wahab@chemie.uni-halle.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 5 Oct 2006 11:34:11 -0700
From: "None" <avinash.ca@gmail.com>
Subject: A doubt in using Class::Struct
Message-Id: <1160073251.162866.22640@i42g2000cwa.googlegroups.com>

Hello,

I am trying to use Class::Struct, for creating a structure as follows:

package Classes;
use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");
$msg->messages(@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
     print $elem ."\n";
}

This gives the following error on compilation:

Too many args to messages at (eval 1) line 17
        Classes::messages('Classes=ARRAY(0x8103084)', 'HELLO', 'WORLD',
'BYE')

Also, if I remove the third element of the array, the program compiles,
but I get the following output:
WORLD

The element "HELLO is not displayed.
Can anyone help me with this or let me know what the problem is?

Thanks,
Avi.



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

Date: 5 Oct 2006 11:59:04 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160074744.153032.183110@i42g2000cwa.googlegroups.com>

None wrote:
> I am trying to use Class::Struct, for creating a structure as follows:
>
> package Classes;

Where are
use strict;
use warnings;
?

I bet they would have given you some clues as to what you did wrong....

> use Class::Struct;
> struct(messages => '@');
>
> $msg = new Classes;
> push(@abc, "HELLO");
> push(@abc, "WORLD");
> push(@abc, "BYE");
> $msg->messages(@abc);

Please read the documentation for the module you're using!
>From perldoc Class::Struct:
=====================================
Array ('@' or '*@')
The element is an array, initialized by default to ().

With no argument, the accessor returns a reference to the element's
whole array (whether or not the element was specified as '@' or '*@').

With one or two arguments, the first argument is an index specifying
one element of the array; the second argument, if present, is assigned
to the array element. If the element type is '@', the accessor returns
the array element value. If the element type is '*@', a reference to
the array element is returned.

As a special case, when the accessor is called with an array reference
as the sole argument, this causes an assignment of the whole array
element. The object reference is returned.
======================================

If you want to set the entire array, you need to pass a reference to
the array, not the array itself.

Paul Lalli



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

Date: 5 Oct 2006 12:17:35 -0700
From: "None" <avinash.ca@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160075855.851105.221970@m73g2000cwd.googlegroups.com>



>
> If you want to set the entire array, you need to pass a reference to
> the array, not the array itself.
>

I tried doing that as follows:

package Classes;
use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");
$msg->messages(\@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
    print $elem ."\n";
}

But, even with this, the "print" statement does not print out anything!
I am a newbie to perl programming. So, please let me know if I am
overlooking some small error!

Refrence:
$msg->messages(\@abc);

Entire array:
$msg->messages(@abc);

In either case, I do not get the full array printed out! Thanks for the
help!

-Avi.



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

Date: 5 Oct 2006 12:21:05 -0700
From: "None" <avinash.ca@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160076065.653652.166610@c28g2000cwb.googlegroups.com>



>
> If you want to set the entire array, you need to pass a reference to
> the array, not the array itself.
>

I tried doing that as follows:

package Classes;
use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");
$msg->messages(\@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
    print $elem ."\n";
}

But, even with this, the "print" statement does not print out anything!
I am a newbie to perl programming. So, please let me know if I am
overlooking some small error!

Refrence:
$msg->messages(\@abc);

Entire array:
$msg->messages(@abc);

In either case, I do not get the full array printed out! Thanks for the
help!

-Avi.



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

Date: 5 Oct 2006 12:21:48 -0700
From: "None" <avinash.ca@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160076108.063363.172460@c28g2000cwb.googlegroups.com>



>
> If you want to set the entire array, you need to pass a reference to
> the array, not the array itself.
>

I tried doing that as follows:

package Classes;
use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");
$msg->messages(\@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
    print $elem ."\n";
}

But, even with this, the "print" statement does not print out anything!
I am a newbie to perl programming. So, please let me know if I am
overlooking some small error!

Refrence:
$msg->messages(\@abc);

Entire array:
$msg->messages(@abc);

In either case, I do not get the full array printed out! Thanks for the
help!

-Avi.



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

Date: 5 Oct 2006 12:28:01 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160076481.328543.44030@e3g2000cwe.googlegroups.com>

None wrote:
> >
> > If you want to set the entire array, you need to pass a reference to
> > the array, not the array itself.
> >
>
> I tried doing that as follows:
>
> package Classes;
> use Class::Struct;
> struct(messages => '@');
>
> $msg = new Classes;
> push(@abc, "HELLO");
> push(@abc, "WORLD");
> push(@abc, "BYE");
> $msg->messages(\@abc);
>
> @xyz = @{$msg->messages};
> foreach $elem(@xyz)
> {
>     print $elem ."\n";
> }
>
> But, even with this, the "print" statement does not print out anything!

Then you copy and pasted wrong.  Because when I copy and paste the
above into a new file, and run that file, the output I get is:
HELLO
WORLD
BYE

Paul Lalli



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

Date: 5 Oct 2006 12:38:15 -0700
From: "None" <avinash.ca@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160077095.350514.106890@e3g2000cwe.googlegroups.com>


> Then you copy and pasted wrong.  Because when I copy and paste the
> above into a new file, and run that file, the output I get is:
> HELLO
> WORLD
> BYE


I copied and pasted the same code in a new file as well, but I am not
getting any output from the execution. I am not able to get if there
any problem with the version of perl, or a problem with the program!

perl --version:
This is perl, v5.6.1 built for i686-linux....


Thanks,
Avi.



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

Date: 5 Oct 2006 12:52:10 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160077930.335894.70810@b28g2000cwb.googlegroups.com>

None wrote:
> > Then you copy and pasted wrong.  Because when I copy and paste the
> > above into a new file, and run that file, the output I get is:
> > HELLO
> > WORLD
> > BYE
>
>
> I copied and pasted the same code in a new file as well, but I am not
> getting any output from the execution. I am not able to get if there
> any problem with the version of perl, or a problem with the program!
>
> perl --version:
> This is perl, v5.6.1 built for i686-linux....

Interesting.  I get no output with Perl v.5.6.1 as well, using module
version 0.59.  However, with Perl v5.8.4 and Class::Struct version
0.63, I get the desired output.

My first and obvious recommendation is that you upgrade.  If that's not
feasable for you, consult the documentation for the version of
Class::Struct on your system, and see if there are any differences
between that and the documentation found at:
http://search.cpan.org/~nwclark/perl-5.8.8/lib/Class/Struct.pm

Sorry I can't be of more help . . . 
Paul Lalli



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

Date: 5 Oct 2006 12:57:01 -0700
From: "None" <avinash.ca@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160078221.614436.186470@m7g2000cwm.googlegroups.com>

Thanks,

I tried inserting into the array using indices and that works... thanks
for the help!

-Avi.

Paul Lalli wrote:
> None wrote:
> > > Then you copy and pasted wrong.  Because when I copy and paste the
> > > above into a new file, and run that file, the output I get is:
> > > HELLO
> > > WORLD
> > > BYE
> >
> >
> > I copied and pasted the same code in a new file as well, but I am not
> > getting any output from the execution. I am not able to get if there
> > any problem with the version of perl, or a problem with the program!
> >
> > perl --version:
> > This is perl, v5.6.1 built for i686-linux....
>
> Interesting.  I get no output with Perl v.5.6.1 as well, using module
> version 0.59.  However, with Perl v5.8.4 and Class::Struct version
> 0.63, I get the desired output.
>
> My first and obvious recommendation is that you upgrade.  If that's not
> feasable for you, consult the documentation for the version of
> Class::Struct on your system, and see if there are any differences
> between that and the documentation found at:
> http://search.cpan.org/~nwclark/perl-5.8.8/lib/Class/Struct.pm
> 
> Sorry I can't be of more help . . . 
> Paul Lalli



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

Date: 5 Oct 2006 12:58:01 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <1160078281.226038.145870@k70g2000cwa.googlegroups.com>

Paul Lalli wrote:
> None wrote:
> > > Then you copy and pasted wrong.  Because when I copy and paste the
> > > above into a new file, and run that file, the output I get is:
> > > HELLO
> > > WORLD
> > > BYE
> >
> >
> > I copied and pasted the same code in a new file as well, but I am not
> > getting any output from the execution. I am not able to get if there
> > any problem with the version of perl, or a problem with the program!
> >
> > perl --version:
> > This is perl, v5.6.1 built for i686-linux....
>
> Interesting.  I get no output with Perl v.5.6.1 as well, using module
> version 0.59.  However, with Perl v5.8.4 and Class::Struct version
> 0.63, I get the desired output.
>
> Sorry I can't be of more help . . .

Actually, it turns out I can be of more help.  I did a diff of the two
 .pm files on my system, and found that the special case of calling the
accessor with a reference to an array is new.  In version 0.59, it
seems the only way to set an array of values is to set them one at a
time:
for my $i (0..$#abc){
    $msg->messages($i, $abc[$i]);
}

The above works for me with both versions of the module.

Paul Lalli



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

Date: Thu, 05 Oct 2006 20:00:07 GMT
From: Brian Helterline <brian.helterline@hp.com>
Subject: Re: A doubt in using Class::Struct
Message-Id: <bvdVg.741$IX3.550@news.cpqcorp.net>

None wrote:
> Hello,
> 
> I am trying to use Class::Struct, for creating a structure as follows:
> 
> package Classes;
> use Class::Struct;
> struct(messages => '@');
> 
> $msg = new Classes;
> push(@abc, "HELLO");
> push(@abc, "WORLD");
> push(@abc, "BYE");
> $msg->messages(@abc);

according to the docs, array type structs take 2 elements, the index and 
the value so you would populate it like this:

$msg->messages(0, 'HELLO');
$msg->messages(1, 'WORLD');
$msg->messages(2, 'BYE');

> 
> @xyz = @{$msg->messages};
> foreach $elem(@xyz)
> {
>      print $elem ."\n";
> }
> 
> This gives the following error on compilation:
> 
> Too many args to messages at (eval 1) line 17
>         Classes::messages('Classes=ARRAY(0x8103084)', 'HELLO', 'WORLD',
> 'BYE')
> 
> Also, if I remove the third element of the array, the program compiles,
> but I get the following output:
> WORLD

with two elements, the first is interpeted as an index ("HELLO" => 0) 
and the value stored there is 'WORLD'

> 
> The element "HELLO is not displayed.
> Can anyone help me with this or let me know what the problem is?
> 
> Thanks,
> Avi.
> 


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

Date: 5 Oct 2006 21:02:47 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Find First Date of A Calendar Month\Year
Message-Id: <4ola7nFf3kk3U1@news.dfncis.de>

 <idgarad@gmail.com> wrote in comp.lang.perl.misc:
> Very simple in description but tricky to implement so far. What I am
> after is given a year, say 2006, what is the First sunday's date on the
> calendar (For January). For instance in 2006 this would be january 1st
> 2006. But in 2005 it would be Dec 26th 2004. Is there a simple module
> for getting this kind of date?

Just one question:  How is Dec 26th 2004 the first Sunday in 2005?

Anno


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

Date: 5 Oct 2006 14:23:34 -0700
From: usenet@DavidFilmer.com
Subject: Re: Find First Date of A Calendar Month\Year
Message-Id: <1160083414.898250.67240@m73g2000cwd.googlegroups.com>

anno4000@radom.zrz.tu-berlin.de wrote:
> Just one question:  How is Dec 26th 2004 the first Sunday in 2005?

I think the OP means  if you look at a calendar (where Sunday is the
first column) and see which row holds Jan 1, what is the date of the
first column of that row?

-- 
David Filmer (http://DavidFilmer.com)



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

Date: 5 Oct 2006 15:03:14 -0700
From: usenet@DavidFilmer.com
Subject: Re: Find First Date of A Calendar Month\Year
Message-Id: <1160085794.600435.191500@b28g2000cwb.googlegroups.com>

idgarad@gmail.com wrote:
> But in 2005 it would be Dec 26th 2004. Is there a simple module
> for getting this kind of date?

Date::Calc's perldocs show several examples which could be adapted,
notably:

7) How do I calculate the date of the Wednesday of the same week as the
current date?

10) How do I calculate the last and the next Saturday for any given
date?

-- 
David Filmer (http://DavidFilmer.com)



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

Date: Thu, 05 Oct 2006 20:25:28 GMT
From: Brian Helterline <brian.helterline@hp.com>
Subject: Re: How to delete temporary file after displaying in browser ?
Message-Id: <YSdVg.743$wY3.539@news.cpqcorp.net>

Yohan N Leder wrote:
> In article <1159804489.823095.242200@m73g2000cwd.googlegroups.com>, 
> bart@nijlen.com says...
> 
>>Normally you can just use unlink without need for extra precautions
>>here. The CGI has sent everything before the unlink-command is invoked,
>>that is, unlink will "wait" until the output stream is closed.
>>
> 
> 
> Sure ? So, you say something like this is enough without any more 
> precaution ?
> 
> #!/usr/bin/perl
> use CGI::Carp qw/fatalsToBrowser/;
> use strict;
> use warnings;
> $|=1;
> print "Content-type: text/html; charset=iso-8859-1\n\n";
> my $imgpath = "../httpdocs/upload/tmp.gif";
> my $imgurl = "/upload/tmp.gif";
> my $imghtm = "<img src='".$imgurl."' border=0 width=150 height=150>";

An alternate approach would be to output something like this:
my $imghtm = "<img src='"/cgi/fetch.pl?image=$imgurl"' border=0 
width=150 height=150>";

and then when the browser issues another request to get this image,
fetch.pl serves it up and then deletes it.

> print "<html><head></head><body>".$imghtm."</body></html>";
> unlink $imgpath;
> exit 0;
> 
> Knowing, of course, that tmp.gif has been correctly uploaded previously 
> and exists on server now.

-- 
brian


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

Date: Thu, 05 Oct 2006 20:15:27 +0200
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Kolmogorov-Smirnov test
Message-Id: <45254bb6$0$5098$ba4acef3@news.orange.fr>

Wuming.Gong@gmail.com wrote:
> Dear list,
> 
> Is there any perl modules for conducting Kolmogorov-Smirnov test
> (http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test)?
> 
> Thanks,
> 
> Wuming

search.cpan.org

It appears not.

Mark


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

Date: 5 Oct 2006 11:05:43 -0700
From: olson_ord@yahoo.it
Subject: Parsing HTML - using HTML::TreeBuilder
Message-Id: <1160071543.817563.98340@k70g2000cwa.googlegroups.com>

Hi,
	I am trying to use Perl to parse a webpage - and I cannot get
started. I hope someone could help me.
	I searched online and I found that I am supposed to use the
HTML::TreeBuilder. In the example below I am trying to get the text in
the TAG named "H2". From the documentation there seems to be two
ways to do this (I might be wrong - then please correct me) i.e.
Using the look_down() and find_by_tag_name(). The latter is rather old.
I have used the former to look for images (just as a test) and the
latter to look for the "H2" tags. In both cases I get the number of
H2's or Images to be 0.
	What am I doing wrong here - or is there an easier way to get the
text in a HTML tag. I would be grateful for any help.

Regards,
Rio

--------------- Code -------------------------
use strict;
use LWP::UserAgent;
use LWP::Simple;
use URI::Escape;
use HTTP::Request::Common;
  use HTML::TreeBuilder;
  my $url = "http://wordlist.gredic.com/kaleidoscope";
  my $html =  get($url);
#   print $html;

  my $tree = HTML::TreeBuilder->new();
    $tree->parse_file($html);

## --- Trial 1 ----------------
    my @imgs = $tree->look_down( _tag => 'img');

## --- Trial 2 ----------------
    my $elements = $tree->elementify();

    my @word = $elements->find_by_tag_name('h2');

## --- Results  ----------------
    print "H2 Words = " . @word . "\n";
    print "Imgs = " . @imgs . "\n";

# At the end need to free up the memory
    $tree->delete;
    print "completed script\n";
--------- End of Code ---------------------

P.S. The above is not my actual code - but a working example to
demonstrate my question



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

Date: 5 Oct 2006 11:23:38 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Parsing HTML - using HTML::TreeBuilder
Message-Id: <1160072618.077252.177500@c28g2000cwb.googlegroups.com>

olson_ord@yahoo.it wrote:
> 	What am I doing wrong here - or is there an easier way to get the
> text in a HTML tag.

I personally prefer HTML::TokeParser for parsing HTML, but TIMTOWTDI

> use strict;

You forgot:
use warnings;

> use LWP::UserAgent;
> use LWP::Simple;

You generally don't use both of these. . .

> use URI::Escape;
> use HTTP::Request::Common;
>   use HTML::TreeBuilder;
>   my $url = "http://wordlist.gredic.com/kaleidoscope";
>   my $html =  get($url);

This function returns the actul HTML content of the URL.

> #   print $html;
>
>   my $tree = HTML::TreeBuilder->new();
>     $tree->parse_file($html);

This attempts to find a file named by the string in $html and parse
that file.  Obviously, no such file exists.

You want
$tree->parse($html);

Paul Lalli



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

Date: 5 Oct 2006 11:28:04 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Parsing HTML - using HTML::TreeBuilder
Message-Id: <1160072884.297642.157410@b28g2000cwb.googlegroups.com>

olson_ord@yahoo.it wrote:
> 	What am I doing wrong here - or is there an easier way to get the
> text in a HTML tag.

I personally prefer HTML::TokeParser for parsing HTML, but TIMTOWTDI

> use strict;

You forgot:
use warnings;

> use LWP::UserAgent;
> use LWP::Simple;

You generally don't use both of these. . .

> use URI::Escape;
> use HTTP::Request::Common;
>   use HTML::TreeBuilder;
>   my $url = "http://wordlist.gredic.com/kaleidoscope";
>   my $html =  get($url);

This function returns the actul HTML content of the URL.

> #   print $html;
>
>   my $tree = HTML::TreeBuilder->new();
>     $tree->parse_file($html);

This attempts to find a file named by the string in $html and parse
that file.  Obviously, no such file exists.

You want
$tree->parse($html);

Paul Lalli



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

Date: Thu, 05 Oct 2006 20:13:12 +0200
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Problems at writing multiple files
Message-Id: <45254b30$0$5098$ba4acef3@news.orange.fr>

vladimir.giron@gmail.com wrote:
> I have a problem at writing multiple files. I have this code where I
> write several XML files (the number of files it's dynamic). I use a
> loop and in Linux works fine but the problem is Windows.
> 
> For example lets say that I want to write a long essay into 1 file,
> Windows and Linux do it as it should be, the problem comes when I want
> to write that same essay (divided into 5 parts) in 5 files. Linux works
> fine but windows skips the first part (the first file is not written)
> and the remaining parts are written well.. if I want to write the essay
> into 10 files, the first three are not written and as the number of
> files grows so does the number of files not written.

Are the files created but have no content?

> Does anyone knows why could this be? Next is the part of the code where
> I write into the files.

Is possible you need to set binmode, but would depend on your data.

Mark


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

Date: 5 Oct 2006 12:02:56 -0700
From: chaitask@yahoo.com
Subject: Re: Regarding numeric literals
Message-Id: <1160074976.695465.223780@e3g2000cwe.googlegroups.com>

Hi Paul,

Thank you very much for all the informative comments. I did note down
your point about enabling 'strict' and 'warnings' pragmas in my code
from now on.

I'm wondering why Perl treats a numeric literal with underscores in it
and a variable with a value made of  numbers punctuated by underscores
so differently......what difference would (or SHOULD perhaps?) it make
for it to evaluate 1_23_456 + 1 to 123457 and $num + 1 (where $num
holds 1_23_456) to 2 ?

Do you happen to know any concrete reasons behind this or it is one of
the design anomalies?

-Krish.



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

Date: Thu, 05 Oct 2006 19:13:46 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Regarding numeric literals
Message-Id: <KPcVg.2923$H7.307@edtnps82>

chaitask@yahoo.com wrote:
> 
> Thank you very much for all the informative comments. I did note down
> your point about enabling 'strict' and 'warnings' pragmas in my code
> from now on.
> 
> I'm wondering why Perl treats a numeric literal with underscores in it
> and a variable with a value made of  numbers punctuated by underscores
> so differently......what difference would (or SHOULD perhaps?) it make
> for it to evaluate 1_23_456 + 1 to 123457 and $num + 1 (where $num
> holds 1_23_456) to 2 ?
> 
> Do you happen to know any concrete reasons behind this or it is one of
> the design anomalies?

Because 1_23_456 is *code* and $num is *data*.  Code is parsed and compiled by
perl while with data it is up to you to write the code to parse it.


John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


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

Date: 5 Oct 2006 12:15:21 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Regarding numeric literals
Message-Id: <1160075721.228860.101760@h48g2000cwc.googlegroups.com>

chait...@yahoo.com wrote:
> I'm wondering why Perl treats a numeric literal with underscores in it
> and a variable with a value made of  numbers punctuated by underscores
> so differently......what difference would (or SHOULD perhaps?) it make
> for it to evaluate 1_23_456 + 1 to 123457 and $num + 1 (where $num
> holds 1_23_456) to 2 ?
>
> Do you happen to know any concrete reasons behind this or it is one of
> the design anomalies?

Perl allows you to put an underscore within your numeric literals to
increase readability.  The number
878912793109
Is difficult to read without actually counting the digits.  In English,
if we're writing that number, we write it like
878,912,793,109
so that we can easily see we're talking about 878 billion, etc.   In
Perl, as you noted in your original post, the comma is the list
separator, so obviously the comma cannot be used to improve
readability.  So they allowed the use of the underscore to increase
readability instead:
my $x = 878_912_793_109;
We can tell that this is the number 878 billion (etc) rather than a
string, because we did not surround the value with quotes.  If I had
actually wanted the string "eight seven eight underscore (etc)", I
would have written:
my $x = '878_912_793_109';

But how would you make that determination in a non-literal value?  If I
put in the code:
my $x = <STDIN>;
and the user enters:
878_912_793_109
how is your program supposed to tell whether the user meant the number
878 billion (etc), or the string "8 7 8 underscore (etc)"?  More to the
point, how is Perl supposed to tell?

Note that this is not unique to numeric literals.  There are also
string literal special characters, that do not apply to values read or
computed.
$x = "foobar\n";
means "foobar, followed by newline", but
chomp($x = <STDIN>);
where the user types
foobar\n
means "foobar, slash, n".

Similarly,
$x = "\\";
$x .= "n";
does not make $x into a newline.  It makes it into the two characters
string '\n'.  Only literals get the specialness associated with these
characters.

Hope this helps,
Paul Lalli



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

Date: 5 Oct 2006 12:37:37 -0700
From: chaitask@yahoo.com
Subject: Re: Regarding numeric literals
Message-Id: <1160077057.279719.229860@h48g2000cwc.googlegroups.com>

Hi Paul,

Yes, your point is crystal clear. Is this all mentioned in what one of
the posters called the Camel book, or you have any other links or
tutorials for me to go through? For instance, what you wrote above has
been very descriptive and easy to understand for someone relatively
young in Perl like me. A book of that tone or style is best suited for
many like me, I figure.

Thanks again for your time, your help has been invaluable.

-Krish



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

Date: 5 Oct 2006 13:00:54 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Regarding numeric literals
Message-Id: <1160078454.851927.183470@m73g2000cwd.googlegroups.com>

chaitask@yahoo.com wrote:
> Yes, your point is crystal clear. Is this all mentioned in what one of
> the posters called the Camel book

That's "Programming Perl", by Larry Wall, published by O'Reilly.  And I
honestly have no idea if using _ in numeric literals is spelled out in
that detail in the Camel or not, because I don't currently have mine on
me.

> , or you have any other links or tutorials for me to go through?

I always recommend starting with these three (fire up your command line
and type them in, one at a time):
perldoc perlintro
perldoc perlsyn
perldoc perldata


> For instance, what you wrote above has
> been very descriptive and easy to understand for someone relatively
> young in Perl like me. A book of that tone or style is best suited for
> many like me, I figure.

You might also try http://learn.perl.org, which has links to other good
references and tutorials, including the free e-book, "Beginning Perl".

Paul Lalli



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

Date: Thu, 05 Oct 2006 22:14:54 +0200
From: Mirco Wahab <wahab@chemie.uni-halle.de>
Subject: Re: Regarding numeric literals
Message-Id: <eg3pcf$2jh$1@mlucom4.urz.uni-halle.de>

Thus spoke Paul Lalli (on 2006-10-05 22:00):
> That's "Programming Perl", by Larry Wall, published by O'Reilly.  And I
> honestly have no idea if using _ in numeric literals is spelled out in
> that detail in the Camel or not, because I don't currently have mine on
> me.

It is, its "detailed enogh" for me - and
says all what we discussed here (Chap.2, 2.6.1):

<Larry>
   ... because Perl uses the comma as a list separator,
   you cannot use it to separate the thousands in a large
   number. Perl does allow you to use an underscore
   character instead. The underscore only works within
   literal numbers specified in your program, not for
   strings functioning as numbers or data read from
   somewhere else ...
</Larry>

He then also explains the workings of
the number system designators (0 & 0x).

(I hope reading a 'quoted part' from a copyrighted
material in a context with your own eyes is legal
in «your country» ...)

Regards

Mirco


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

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


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