[30738] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1983 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 14 14:09:50 2008

Date: Fri, 14 Nov 2008 11:09:13 -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, 14 Nov 2008     Volume: 11 Number: 1983

Today's topics:
    Re: atoi/atof sln@netherlands.com
        Client-Server Communication in Perl CGI <sriseetha1986@gmail.com>
    Re: Client-Server Communication in Perl CGI <spamtrap@dot-app.org>
    Re: Client-Server Communication in Perl CGI <jimsgibson@gmail.com>
        Decoding wav files for FFT <kieranocall@gmail.com>
    Re: Decoding wav files for FFT <smallpond@juno.com>
    Re: Decoding wav files for FFT <kieranocall@gmail.com>
    Re: Decoding wav files for FFT <smallpond@juno.com>
    Re: Editor with embedded perl interpreter? <cartercc@gmail.com>
    Re: Editor with embedded perl interpreter? <lap76@19yahoo.com>
    Re: Editor with embedded perl interpreter? <RedGrittyBrick@spamweary.invalid>
    Re: FAQ 6.19 Why does using $&, $`, or $' slow my progr <someone@example.com>
        Net::FTP hangs <slbentley@gmail.com>
    Re: Net::FTP hangs <tadmc@seesig.invalid>
        subroutine local variable with initialization <google@markginsburg.com>
    Re: subroutine local variable with initialization <uri@stemsystems.com>
    Re: subroutine local variable with initialization <joost@zeekat.nl>
        unpack "B*", 15 <Alexander.Farber@gmail.com>
    Re: unpack "B*", 15 <joost@zeekat.nl>
    Re: unpack "B*", 15 <smallpond@juno.com>
    Re: unpack "B*", 15 <mgjv@heliotrope.com.au>
    Re: unpack "B*", 15 <Alexander.Farber@gmail.com>
    Re: unpack "B*", 15 xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 14 Nov 2008 17:48:16 GMT
From: sln@netherlands.com
Subject: Re: atoi/atof
Message-Id: <j0drh49bmlme2v4pe9psucnqg4b0sfdku0@4ax.com>

On Thu, 13 Nov 2008 18:31:25 -0800 (PST), debjyoti@gmail.com wrote:

>Hi all
>
>I was messing around with a perl script and realized converting
>strings to integers/floats is iffy on perl (compared to C). There is
>all this mention of atoi/atof being system dependent etc and so I
>found this piece of  code for atoi():
>
>sub atoi {
>  my $t;
>  foreach my $d (split(//, shift())) {

Split() is being passed a string. So $d is now used as a string.
$t here is unknown at this point.

>    $t = $t * 10 + $d;
             ^    ^
Now $t and $d are being used as a number because of the *+ operators.
If $d were not a digit, there might be a runtime error.

>  }
>  return $t;
>}
>
[snip]

Perl's got a kind of general purpose type SCALAR that interprets 
if a number, string based on the usage of operators. Its interchangeable.
So its not really necessary to do this.

Of more use would be rounding in the conversion of float to int (both SCALARS).
Here you have to do it yourself. ie:
	$needwholenumberrounded = int($floatnumber+.5);

In your examples you emulate the algorithym for the C atoi().
When in effect, Perl, built on C, calls atoi() directly from the standard runtime
libraries. So there's no need to duplicate the work.

I noticed in your emulation you don't account for [whitespaces][sign][digits].

Typically, atoi() from C library does something like this:

int _myatoi (char *p)
{
	int num = 0, sign = 1, digit;
	while (p && (digit=*p) == ' ' || digit == '\t')	p++;
	if (*p == '-') {
		sign = -1;
		p++;
	} else
	if (*p == '+')
		p++;
	while (digit = *p++) {
		if (!(digit >= '0' && digit <= '9'))
			return 0;
		num = num * 10 + (digit - '0');
	}
	return sign*num;
}

Good job though, I like your emulations.


sln



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

Date: Fri, 14 Nov 2008 04:57:00 -0800 (PST)
From: sri <sriseetha1986@gmail.com>
Subject: Client-Server Communication in Perl CGI
Message-Id: <0097acec-c52e-4ac4-8de0-0400d0a9b27a@k24g2000pri.googlegroups.com>

Hi Friends,

I have a requirement like this,

My client program is a browser, and the server may be run in perl, ror
or any other server side scripting. The browser has to send the
request to the server once, after that the server has to keep track of
the client and send the response frequently to the client.

One way we can achieve this with Applets. Is there any other way to
achieve this ??

Regards,
Sri Seethalakshmi.B


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

Date: Fri, 14 Nov 2008 11:27:06 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Client-Server Communication in Perl CGI
Message-Id: <m14p2ap92d.fsf@dot-app.org>

sri <sriseetha1986@gmail.com> writes:

> My client program is a browser

Do you mean, a web browser?

>, and the server may be run in perl, ror
> or any other server side scripting. The browser has to send the
> request to the server once

Yes, that's how HTTP works. It's a stateless protocol, no connection is
maintained. The client sends a request, the server returns a response,
then they part company and go their separate ways until the client makes
another request.

> after that the server has to keep track of
> the client and send the response frequently to the client.

Rethink your design. A web server can't do that within the bounds of the
HTTP protocol, regardless of what scripting language it's using.

> One way we can achieve this with Applets.

Yes, an applet could open and maintain a long-running connection, but to
do so it would have to use something other than HTTP. That means it
would have to connect to an application server that speaks something
other than HTTP - i.e. something that isn't a web server.

> Is there any other way to
> achieve this ??

To achieve *what*? You haven't said what you want to do, just how you
think you want to do it. What's the end goal of all this?

sherm--

-- 
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


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

Date: Fri, 14 Nov 2008 10:09:07 -0800
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Client-Server Communication in Perl CGI
Message-Id: <141120081009072292%jimsgibson@gmail.com>

In article
<0097acec-c52e-4ac4-8de0-0400d0a9b27a@k24g2000pri.googlegroups.com>,
sri <sriseetha1986@gmail.com> wrote:

> Hi Friends,
> 
> I have a requirement like this,
> 
> My client program is a browser, and the server may be run in perl, ror
> or any other server side scripting. The browser has to send the
> request to the server once, after that the server has to keep track of
> the client and send the response frequently to the client.
> 
> One way we can achieve this with Applets. Is there any other way to
> achieve this ??

Sounds like 'AJAX' to me. I am only familiar with the concept, not the
methods, so I can't say for sure. See
<http://en.wikipedia.org/wiki/Ajax_(programming)> for more info.

For vanilla HTTP, the choice is "server push" and "client pull". See,
for example <http://oreilly.com/openbook/cgi/ch06_06.html>.

For an example of client pull, see
<http://www.stonehenge.com/merlyn/LinuxMag/col39.html>

-- 
Jim Gibson


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

Date: Fri, 14 Nov 2008 03:55:02 -0800 (PST)
From: kieran <kieranocall@gmail.com>
Subject: Decoding wav files for FFT
Message-Id: <d2d479dd-3ca2-46a1-842f-dd23c3635c17@e38g2000prn.googlegroups.com>

Hello,
I have been working on an audio comparison project which involves
running an FFT on some .wav files. The first thing I need to do id get
the wav information into my perl program so that I can run the FFT on
the data.
I used Matlab to import the wav file, which automaticall extracted the
samples from the wavfile.
I  am looking to do the same in perl but the results I get when I try
to pull out the sampels from the wav file are very different to what i
am seeing in Matlab.
I have used Audio::Wav  read() and Audio::Analyzer chunk->pcm
The results I am getting back from Audio::Wav read seem very large
(-80, -69, -73, 84, 66, etc...) with very few zero values, wheres as
with Matlab the results I am gettting back are as follows (0
-3.05175781250000e-05, -3.05175781250000e-05, 3.05175781250000e-05,
-3.05175781250000e-05,
-3.05175781250000e-05, etc...)

Any idea why I am getting such different results?
Any help greatly appreciated,
kieran


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

Date: Fri, 14 Nov 2008 04:18:23 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Decoding wav files for FFT
Message-Id: <7ebd2476-dfc7-4475-b1d8-8ada89358ed2@f40g2000pri.googlegroups.com>

On Nov 14, 6:55 am, kieran <kieranoc...@gmail.com> wrote:
> Hello,
> I have been working on an audio comparison project which involves
> running an FFT on some .wav files. The first thing I need to do id get
> the wav information into my perl program so that I can run the FFT on
> the data.
> I used Matlab to import the wav file, which automaticall extracted the
> samples from the wavfile.
> I  am looking to do the same in perl but the results I get when I try
> to pull out the sampels from the wav file are very different to what i
> am seeing in Matlab.
> I have used Audio::Wav  read() and Audio::Analyzer chunk->pcm
> The results I am getting back from Audio::Wav read seem very large
> (-80, -69, -73, 84, 66, etc...) with very few zero values, wheres as
> with Matlab the results I am gettting back are as follows (0
> -3.05175781250000e-05, -3.05175781250000e-05, 3.05175781250000e-05,
> -3.05175781250000e-05,
> -3.05175781250000e-05, etc...)
>
> Any idea why I am getting such different results?
> Any help greatly appreciated,
> kieran


Which scaler are you using?


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

Date: Fri, 14 Nov 2008 06:26:05 -0800 (PST)
From: kieran <kieranocall@gmail.com>
Subject: Re: Decoding wav files for FFT
Message-Id: <8b0d50e1-8e30-4b83-b0d9-3e4a71fcd9b9@t39g2000prh.googlegroups.com>

On Nov 14, 12:18 pm, smallpond <smallp...@juno.com> wrote:
> On Nov 14, 6:55 am, kieran <kieranoc...@gmail.com> wrote:
>
>
>
> > Hello,
> > I have been working on an audio comparison project which involves
> > running an FFT on some .wav files. The first thing I need to do id get
> > the wav information into my perl program so that I can run the FFT on
> > the data.
> > I used Matlab to import the wav file, which automaticall extracted the
> > samples from the wavfile.
> > I  am looking to do the same in perl but the results I get when I try
> > to pull out the sampels from the wav file are very different to what i
> > am seeing in Matlab.
> > I have used Audio::Wav  read() and Audio::Analyzer chunk->pcm
> > The results I am getting back from Audio::Wav read seem very large
> > (-80, -69, -73, 84, 66, etc...) with very few zero values, wheres as
> > with Matlab the results I am gettting back are as follows (0
> > -3.05175781250000e-05, -3.05175781250000e-05, 3.05175781250000e-05,
> > -3.05175781250000e-05,
> > -3.05175781250000e-05, etc...)
>
> > Any idea why I am getting such different results?
> > Any help greatly appreciated,
> > kieran
>
> Which scaler are you using?

Hi Smallpond,
I didn't specify a scalar, Im also new to perl so I'm not sure if this
is something I'm missing.
K


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

Date: Fri, 14 Nov 2008 08:31:13 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Decoding wav files for FFT
Message-Id: <557cb0a3-1f4d-484c-b28e-bcac9858dc70@v5g2000prm.googlegroups.com>

On Nov 14, 9:26 am, kieran <kieranoc...@gmail.com> wrote:
> On Nov 14, 12:18 pm, smallpond <smallp...@juno.com> wrote:
>
>
>
> > On Nov 14, 6:55 am, kieran <kieranoc...@gmail.com> wrote:
>
> > > Hello,
> > > I have been working on an audio comparison project which involves
> > > running an FFT on some .wav files. The first thing I need to do id get
> > > the wav information into my perl program so that I can run the FFT on
> > > the data.
> > > I used Matlab to import the wav file, which automaticall extracted the
> > > samples from the wavfile.
> > > I  am looking to do the same in perl but the results I get when I try
> > > to pull out the sampels from the wav file are very different to what i
> > > am seeing in Matlab.
> > > I have used Audio::Wav  read() and Audio::Analyzer chunk->pcm
> > > The results I am getting back from Audio::Wav read seem very large
> > > (-80, -69, -73, 84, 66, etc...) with very few zero values, wheres as
> > > with Matlab the results I am gettting back are as follows (0
> > > -3.05175781250000e-05, -3.05175781250000e-05, 3.05175781250000e-05,
> > > -3.05175781250000e-05,
> > > -3.05175781250000e-05, etc...)
>
> > > Any idea why I am getting such different results?
> > > Any help greatly appreciated,
> > > kieran
>
> > Which scaler are you using?
>
> Hi Smallpond,
> I didn't specify a scalar, Im also new to perl so I'm not sure if this
> is something I'm missing.
> K

scaler, not scalar.  See:
http://search.cpan.org/~triddle/Audio-Analyzer-0.1/lib/Audio/Analyzer.pm#SCALER_CLASSES




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

Date: Fri, 14 Nov 2008 06:01:14 -0800 (PST)
From: cartercc <cartercc@gmail.com>
Subject: Re: Editor with embedded perl interpreter?
Message-Id: <c1265b56-1336-4e58-b8e4-e461b4b12d5c@w24g2000prd.googlegroups.com>

On Nov 13, 9:37=A0pm, Bart Lateur <bart.lat...@pandora.be> wrote:
> What do you mean, "CPAN doesn't work"?

What I mean is that, when I give the command at the command prompt,
the machine just sits and I have to ctl-C to recover. This is the
command:
perl -MCPAN -e shell
I agree that my phrasing was less than artful.

> And second, even though ActiveState's repository doesn't list it,
> according to Kobes' Search, there *are* PPM repositories that carry
> Devel::REPL, including 1 for perl 5.8.

Okay, I'll look at this. Thanks for your help.

CC


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

Date: Fri, 14 Nov 2008 15:02:15 +0100
From: Paolo <lap76@19yahoo.com>
Subject: Re: Editor with embedded perl interpreter?
Message-Id: <op.ukl3x1xs4j0i0k@fsa1300>

> I see two things here, both of which I would classify as semi-
> important.

 ...as for me "editor with perl-interpreter embedded" means the
chance for execution of "perl-script" on selected part of a text.

example:

[...]                   (...)
 ...this is only an      (100)
example...              (101)
[...]                   (...)

if i select the line 100, 101 and run the script suppress-dots (included
in library of script executable...), i obtain:

[...]                   (...)
this is only an         (100)
example                 (101)
[...]                   (...)

This result is easily carried out with external perl-script but i've
to manage text file I/O.


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

Date: Fri, 14 Nov 2008 15:23:34 +0000
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Editor with embedded perl interpreter?
Message-Id: <491d97f7$0$10548$db0fefd9@news.zen.co.uk>


Paolo wrote:
>> I see two things here, both of which I would classify as semi-
>> important.
> 
> ...as for me "editor with perl-interpreter embedded" means the
> chance for execution of "perl-script" on selected part of a text.
> 
> example:
> 
> [...]                   (...)
> ...this is only an      (100)
> example...              (101)
> [...]                   (...)
> 
> if i select the line 100, 101 and run the script suppress-dots (included
> in library of script executable...), i obtain:
> 
> [...]                   (...)
> this is only an         (100)
> example                 (101)
> [...]                   (...)
> 
> This result is easily carried out with external perl-script but i've
> to manage text file I/O.

Did you want something like the following?

In vim
:100,101!suppress_dots.pl

-- 
RGB


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

Date: Fri, 14 Nov 2008 06:22:52 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: FAQ 6.19 Why does using $&, $`, or $' slow my program down?
Message-Id: <UQfTk.2603$Wd1.1086@newsfe06.iad>

PerlFAQ Server wrote:
> This is an excerpt from the latest version perlfaq6.pod, which
> comes with the standard Perl distribution. These postings aim to 
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is at http://faq.perl.org .
> 
> --------------------------------------------------------------------
> 
> 6.19: Why does using $&, $`, or $' slow my program down?
> 
>      
>     (contributed by Anno Siegel)
> 
>     Once Perl sees that you need one of these variables anywhere in the
>     program, it provides them on each and every pattern match. That means
>     that on every pattern match the entire string will be copied, part of it
>     to $`, part to $&, and part to $'. Thus the penalty is most severe with
>     long strings and patterns that match often. Avoid $&, $', and $` if you
>     can, but if you can't, once you've used them at all, use them at will
>     because you've already paid the price. Remember that some algorithms
>     really appreciate them. As of the 5.005 release, the $& variable is no
>     longer "expensive" the way the other two are.
> 
>     Since Perl 5.6.1 the special variables @- and @+ can functionally
>     replace $`, $& and $'. These arrays contain pointers to the beginning
>     and end of each match (see perlvar for the full story), so they give you
>     essentially the same information, but without the risk of excessive
>     string copying.
> 
>     Perl 5.10 added three specials, "${^MATCH}", "${^PREMATCH}", and
>     "${^POSTMATCH}" to do the same job but without the global performance
>     penalty. Pelr only sets these variables if you compile or execute the
                ****
                Perl

>     regular expression with the "/p" modifier.


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: Fri, 14 Nov 2008 08:08:16 -0800 (PST)
From: Scooter <slbentley@gmail.com>
Subject: Net::FTP hangs
Message-Id: <3fb8c833-ace2-4c97-89bd-d6057d55e19f@z6g2000pre.googlegroups.com>

I was attempting to use Net::FTP today for the first time, but the
thing is locking up my perl program. I dont even have any FTP code
yet, just 'use Net::FTP;' at the top of my code, and when you run it
nothing seems to happen.
So I wrote this script...and this is the entire script:

#!/usr/bin/perl

use Net::FTP;

And if you run it, it just sits there doing nothing. Any thoughts, any
debug tricks I might try? Running Perl 5.6.0 and my version of
Net::FTP is 2.77


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

Date: Fri, 14 Nov 2008 11:38:21 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Net::FTP hangs
Message-Id: <slrnghrdsd.qlu.tadmc@tadmc30.sbcglobal.net>

Scooter <slbentley@gmail.com> wrote:

> Running Perl 5.6.0


A lot has happened in the last 8 years...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Fri, 14 Nov 2008 10:11:10 -0800 (PST)
From: Mark <google@markginsburg.com>
Subject: subroutine local variable with initialization
Message-Id: <48ed5e7d-b93b-4745-99fa-b63e1dd6678c@w1g2000prk.googlegroups.com>

Suppose I have a program with the entry point at the top and
subroutines following the exit point. I want to have a subroutine
local variable, $count in my example, that is initialized to a
starting value.

This works, but seems overly cumbersome to me.  Is there a better way?

use strict;
use warnings;

# entry point
doit();
doit();
exit; # exit point before subroutine definition

{
    my $count;

    BEGIN {
        $count = 1;
    }

    sub doit {
        print "count=$count\n";
        $count++;
    }
}

__END__

Output:
count=1
count=2


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

Date: Fri, 14 Nov 2008 13:17:05 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: subroutine local variable with initialization
Message-Id: <x77i76kw9q.fsf@mail.sysarch.com>

>>>>> "M" == Mark  <google@markginsburg.com> writes:

  M> doit();
  M> exit; # exit point before subroutine definition

  M> {
  M>     my $count;

  M>     BEGIN {
  M>         $count = 1;
  M>     }

  M>     sub doit {
  M>         print "count=$count\n";
  M>         $count++;
  M>     }
  M> }

put the sub and count together into the begin block:

BEGIN {

    my $count = 1 ;

    sub doit {
        print "count=$count\n";
        $count++;
    }
}

or use closures (plenty about that on the net so google for it)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Fri, 14 Nov 2008 19:19:02 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: subroutine local variable with initialization
Message-Id: <87ej1ecgrt.fsf@zeekat.nl>

Mark <google@markginsburg.com> writes:

> Suppose I have a program with the entry point at the top and
> subroutines following the exit point. I want to have a subroutine
> local variable, $count in my example, that is initialized to a
> starting value.
>
> This works, but seems overly cumbersome to me.  Is there a better way?

Some different solutions:

* put your $count variable and initialization at the top.

* put the whole $count and subroutine construct in a module and use() it
  (this is probably what I would prefer to do)

* get perl 5.10 and use state() variables

* use the my $var if 0 trick - not recommended, probably.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Fri, 14 Nov 2008 04:48:16 -0800 (PST)
From: "A. Farber" <Alexander.Farber@gmail.com>
Subject: unpack "B*", 15
Message-Id: <c6feb79a-f168-40da-a654-d73bc2613a5d@c22g2000prc.googlegroups.com>

Hello,

why doesn't unpack "B*", 15 print "1111"?
Why do you have to insert pack "N" inbetween?

# perl -e 'print unpack "B*", 15'
0011000100110101

# perl -e 'print unpack "B*", pack "N", 15'
00000000000000000000000000001111

Is this some Perl-internal presentation
(some Perl flags, like value=string/numeric?)
which is being printed by the 1st statement?

Thank you
Alex

# perl -v
This is perl, v5.8.8 built for i386-linux-thread-multi
# cat /etc/*release
CentOS release 5.2 (Final)




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

Date: Fri, 14 Nov 2008 13:58:03 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: unpack "B*", 15
Message-Id: <877i76ea78.fsf@zeekat.nl>

"A. Farber" <Alexander.Farber@gmail.com> writes:

> Hello,
>
> why doesn't unpack "B*", 15 print "1111"?
> Why do you have to insert pack "N" inbetween?
>
> # perl -e 'print unpack "B*", 15'
> 0011000100110101

unpack's parameters are ALL STRINGS. so that code is equivalent to

 unpack "B*","15"

> # perl -e 'print unpack "B*", pack "N", 15'
> 00000000000000000000000000001111

pack "N",15 converts the perl number 15 to the binary string containing
a native integer with the value 15, which is what unpack "B*" expects.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Fri, 14 Nov 2008 04:59:51 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: unpack "B*", 15
Message-Id: <05da1e5d-245c-4eaf-ac4c-42f61fc13c30@u18g2000pro.googlegroups.com>

On Nov 14, 7:48 am, "A. Farber" <Alexander.Far...@gmail.com> wrote:
> Hello,
>
> why doesn't unpack "B*", 15 print "1111"?
> Why do you have to insert pack "N" inbetween?
>
> # perl -e 'print unpack "B*", 15'
> 0011000100110101
>

Its doing what you told it to:
"1" is 00110001 and "5" is 00110101

perldoc -f unpack

"takes a string and expands it out into a list of values"



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

Date: Sat, 15 Nov 2008 00:31:54 +1100
From: Martien Verbruggen <mgjv@heliotrope.com.au>
Subject: Re: unpack "B*", 15
Message-Id: <slrnghqvea.cvq.mgjv@mgjv.heliotrope.home>

On Fri, 14 Nov 2008 04:48:16 -0800 (PST),
	A. Farber <Alexander.Farber@gmail.com> wrote:
> Hello,
>
> why doesn't unpack "B*", 15 print "1111"?

You seem to be misunderstanding what unpack and pack do, precisely.

pack takes a list of perl values, and packs them into a 'string', which
probably would better be called a byte array, or byte string, or
something like that. pack and unpack were originally meant to allow the
packing of structs from a perl program in a simiar way a C program would
do it. The resulting 'string' is a binary pattern, resembling the way
data from a C program would live in memory or in binary data files.

unpack takes one of those packed strings, and expands it to a list of
values, according to the template. In this case it would interpret the
string '15' as a bit pattern, and it will expand that to the string
representation of that bit pattern, as per the B field.

> Why do you have to insert pack "N" inbetween?

because '15' is not a bit pattern for the number 15. pack "N", 15 is.
'15' is the Perl string consisting of the characters '1' and '5'. you
didn't put quotes there, but unpack will still interpret that argument
as a string, because, well, because it does. That's its job.

pack "N", 15 takes the perl value 15, treats it as a number, and returns
a string whose bytes are the network byte order unsigned long
representation of 15, so you get 4 bytes, the first three of which are
0, and the last is 15, or 0x0f or 0b00001111.

unpack "B*", ...

takes those bytes, and interprets the individual bits in them,
returning the string 00000000000000000000000000001111: 8 zeroes each for
the first three bytes, and 4 zeroes and 4 ones for the last one.

Try the following:

# perl -l
print unpack "B*", pack "C", 15;
print unpack "B*", pack "n", 15;
print unpack "B*", pack "N", 15;
print unpack "B*", pack "v", 15;
print unpack "B*", pack "V", 15;
__END__

Here you can see clearly that the unpack is not showing you the bit
representation of 15, but of the packed string. There are many possible
bit representations for the number 15. These are only five of them.

The Vax byte order is little endian, as opposed to the network byte
order, which is big endian, which is what you're looking for.

> # perl -e 'print unpack "B*", 15'
> 0011000100110101

This is two bytes with values:

00110001 00110101
49       53

And whose happen to be the ASCII values for the characters 1 and 5,
which is exactly what you put in.

Martien
-- 
                        | 
Martien Verbruggen      | Useful Statistic: 75% of the people make up
                        | 3/4 of the population.
                        | 


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

Date: Fri, 14 Nov 2008 06:14:36 -0800 (PST)
From: "A. Farber" <Alexander.Farber@gmail.com>
Subject: Re: unpack "B*", 15
Message-Id: <8c025fde-4c66-4e34-9dd2-7d34d3595163@c22g2000prc.googlegroups.com>

Thank you


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

Date: 14 Nov 2008 17:22:32 GMT
From: xhoster@gmail.com
Subject: Re: unpack "B*", 15
Message-Id: <20081114122310.185$G3@newsreader.com>

"A. Farber" <Alexander.Farber@gmail.com> wrote:
> Hello,
>
> why doesn't unpack "B*", 15 print "1111"?
> Why do you have to insert pack "N" inbetween?
>
> # perl -e 'print unpack "B*", 15'
> 0011000100110101
>
> # perl -e 'print unpack "B*", pack "N", 15'
> 00000000000000000000000000001111

Others have already explained that 15 is treated as two ASCII characters
'1' and '5'.  I just wanted to point out that in this case you can use chr
instead of pack "N".

perl -e 'print unpack "B*", chr(15)'
00001111

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

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 V11 Issue 1983
***************************************


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