[31460] in Perl-Users-Digest
Perl-Users Digest, Issue: 2712 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 9 21:09:49 2009
Date: Wed, 9 Dec 2009 18:09:17 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 9 Dec 2009 Volume: 11 Number: 2712
Today's topics:
Re: Any way to tell if a scalar is a string? <rvtol+usenet@xs4all.nl>
Re: Any way to tell if a scalar is a string? <derykus@gmail.com>
Re: Any way to tell if a scalar is a string? sln@netherlands.com
Re: Any way to tell if a scalar is a string? <jl_post@hotmail.com>
Re: Any way to tell if a scalar is a string? <jl_post@hotmail.com>
Re: Any way to tell if a scalar is a string? <tadmc@seesig.invalid>
Re: Any way to tell if a scalar is a string? <sreservoir@gmail.com>
Re: Any way to tell if a scalar is a string? <ben@morrow.me.uk>
Re: Any way to tell if a scalar is a string? <sreservoir@gmail.com>
Re: Any way to tell if a scalar is a string? sln@netherlands.com
Re: Any way to tell if a scalar is a string? <jl_post@hotmail.com>
Re: Any way to tell if a scalar is a string? sln@netherlands.com
Re: Any way to tell if a scalar is a string? <ben@morrow.me.uk>
Re: Any way to tell if a scalar is a string? <uri@StemSystems.com>
parsing query strings <sreservoir@gmail.com>
Re: parsing query strings <ben@morrow.me.uk>
Re: parsing query strings <sreservoir@gmail.com>
Re: parsing query strings <john@castleamber.com>
Re: parsing query strings <ben@morrow.me.uk>
Re: Perl to Java bytecode <rabbits77@my-deja.com>
tj win pos <robin1@cnsp.com>
Re: tj win pos <ben@morrow.me.uk>
Re: Unblessed reference. <justin.0912@purestblue.com>
Re: Unblessed reference. <tadmc@seesig.invalid>
Re: Unblessed reference. <tadmc@seesig.invalid>
Re: Unblessed reference. <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 09 Dec 2009 21:21:49 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <4b2006de$0$22936$e4fe514c@news.xs4all.nl>
jl_post@hotmail.com wrote:
> I'm wondering if there's a way in Perl to tell if a scalar was set
> as a string.
Not dependably, unless nothing happened to it after.
$v = "123";
Here, $v only has a string face.
1 if $v > 0;
Now, $v also has numeric faces.
$v = 123;
Now, $v only has an integer face.
$v eq "123";
And now, it also has a string face.
> So my question is: How can I know if a scalar (that looks like a
> number) was originally a string?
Not possible. Unless it was never touched. Or was initialized with some
whitespace before and/or after. :)
perl -MDevel::Peek -wle'
my $v = " 123 ";
Dump($v);
1 if $v > 0;
Dump($v);
1 if $v/1;
Dump($v)
'
SV = PV(0x803048) at 0x800228
REFCNT = 1
FLAGS = (PADBUSY,PADMY,POK,pPOK)
PV = 0x201390 " 123 "\0
CUR = 5
LEN = 8
SV = PVIV(0x802060) at 0x800228
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,POK,pIOK,pPOK)
IV = 123
PV = 0x201390 " 123 "\0
CUR = 5
LEN = 8
SV = PVNV(0x804480) at 0x800228
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 123
NV = 123
PV = 0x201390 " 123 "\0
CUR = 5
LEN = 8
--
Ruud
------------------------------
Date: Wed, 9 Dec 2009 12:49:09 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <bb8cf724-db9c-47d0-8ed4-e11195a849aa@w19g2000pre.googlegroups.com>
On Dec 9, 9:35=A0am, "jl_p...@hotmail.com" <jl_p...@hotmail.com> wrote:
> Hi,
>
> =A0 =A0I'm wondering if there's a way in Perl to tell if a scalar was set
> as a string. =A0For example, I'd like to write code like this:
>
> =A0 =A0my $var1 =3D "7777";
> =A0 =A0my $var2 =3D 7777;
>
> =A0 =A0foreach ($var1, $var2)
> =A0 =A0{
> =A0 =A0 =A0 if ( SOME_TEST )
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0# Value is a string, so print it with quotes:
> =A0 =A0 =A0 =A0 =A0print "Value =3D \"$_\"\n";
> =A0 =A0 =A0 }
> =A0 =A0 =A0 else
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0# Print it without quotes:
> =A0 =A0 =A0 =A0 =A0print "Value =3D $_\n";
> =A0 =A0 =A0 }
> =A0 =A0}
>
> =A0 =A0The reason I want to do this is because I write a lot of Perl code
> that uses the unpack() function, and then reports the data it
> extracts. =A0For example, I might write:
>
> =A0 =A0 $string =3D 'A 65';
> =A0 =A0 @data =3D unpack('c x a2', $string);
>
> in which case @data should be filled with two elements: =A0the first
> being the integer 65, and the second the string "65". =A0But since Perl
> seamlessly converts strings to the numeric values they contain, it can
> be hard for me to know whether a value that looks numeric was
> originally a string or a number.
>
> =A0 =A0I noticed that if I use the perl debugger (with "perl -wde 1"),
> typing "x @data" does not show which element is a string. =A0On the
> other hand, "use Data::Dumper; print Dumper @data;" DOES show the
> difference -- and it shows it by surrounding the second element with
> single quotes. =A0(Evidently Perl does keep track of whether a numeric
> scalar was set as a string or not.)
>
> =A0 =A0So my question is: =A0How can I know if a scalar (that looks like =
a
> number) was originally a string? =A0(That is, assigned/encoded to a
> string?)
>
> =A0 =A0I've read "perldoc Scalar::Util", but the only functions that look
> related are looks_like_number() (which is not what I want because
> regardless of whether it looks like a number I want to know if it was
> encoded as a string) and isvstring() (which returns true if the value
> was coded as a "vstring" (but not as a string, which is what I want)).
>
Maybe just use Data::Dumper...
my @var =3D ("7777", 7777, 'foobar', 123_456) ;
for (@var) {
my $d =3D Data::Dumper->new([$_]);
print "var is a ", ( $d->Dump =3D~ /'/ ? "string" : "number");
}
-> var is a string
var is a number
var is a string
var is a number
--
Charles DeRykus
------------------------------
Date: Wed, 09 Dec 2009 12:57:40 -0800
From: sln@netherlands.com
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <he20i5dcjquo0sp7m1opdk0o7hqmfes632@4ax.com>
On Wed, 9 Dec 2009 09:35:51 -0800 (PST), "jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
>Hi,
>
> I'm wondering if there's a way in Perl to tell if a scalar was set
>as a string. For example, I'd like to write code like this:
>
> my $var1 = "7777";
> my $var2 = 7777;
>
> foreach ($var1, $var2)
> {
> if ( SOME_TEST )
> {
> # Value is a string, so print it with quotes:
> print "Value = \"$_\"\n";
> }
> else
> {
> # Print it without quotes:
> print "Value = $_\n";
> }
> }
>
> The reason I want to do this is because I write a lot of Perl code
>that uses the unpack() function, and then reports the data it
>extracts. For example, I might write:
>
> $string = 'A 65';
> @data = unpack('c x a2', $string);
>
>in which case @data should be filled with two elements: the first
>being the integer 65, and the second the string "65". But since Perl
>seamlessly converts strings to the numeric values they contain, it can
>be hard for me to know whether a value that looks numeric was
>originally a string or a number.
>
Seems the truth table of Perl is:
65 == '65' true
65 eq '65' true
65 eq 'A' false
65 == 'A' error, 'A' is not a digit
The bias is in the usage, in which conditionals are
hard to get around.
I guess if you could find out what something is without a
usage context, but how's that possible?
This :
@data = unpack('c x a2', $string);
is a known data format isin't it?
When do you not know what something is?
-sln
------------------------------
Date: Wed, 9 Dec 2009 14:17:07 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <c11a634e-d122-4aef-a942-3fe9928c5734@g4g2000pri.googlegroups.com>
On Dec 9, 1:49=A0pm, "C.DeRykus" <dery...@gmail.com> wrote:
>
> Maybe just use Data::Dumper...
>
> my @var =3D ("7777", 7777, =A0'foobar', 123_456) ;
>
> for (@var) =A0{
> =A0 =A0my $d =3D Data::Dumper->new([$_]);
> =A0 =A0print "var is a ", ( $d->Dump =3D~ /'/ ? "string" : "number");
> }
Yeah, I was thinking of checking Data::Dumper's output for "'" as
well. However, to do that we really should be checking to see if our
scalar is not a reference. So we could write a function like this:
#!/usr/bin/perl
# Returns a true value if set as a string;
# returns false if not:
sub isString
{
return 0 if ref($_[0]);
use Data::Dumper;
return 1 if Dumper($_[0]) =3D~ m/'/;
return 0;
}
foreach ("7777", 7777, 'foobar', 123_456)
{
print((isString($_) ? "Is " : "Is not "),
"a string.\n");
}
__END__
outputs:
Is a string.
Is not a string.
Is a string.
Is not a string.
So using Data::Dumper works, as you say. But I think this might be
overkill for what I'm trying to do. I just want to know if there's a
simpler way that I'm overlooking.
Thanks for your reply, though.
-- Jean-Luc
------------------------------
Date: Wed, 9 Dec 2009 14:48:31 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <2cc15b05-6c32-4fdb-857a-49b9bc1395db@13g2000prl.googlegroups.com>
On Dec 9, 1:57 pm, s...@netherlands.com wrote:
> This :
> @data = unpack('c x a2', $string);
> is a known data format, isn't it?
>
> When do you not know what something is?
That's a good question. The example I gave is overly simplistic;
in reality the binary strings/files I'm parsing through can have
literally hundreds (if not thousands) of entries, and keeping track of
whether they are derived from strings or numeric values can get a bit
hairy.
Not only that, but if I use a format string like:
'I/i I/(a10) I/i'
the number of elements I extract is not fixed. Therefore, it's
difficult to know when the first set of numerical values ends and the
set of not-necessarily-numerical values begins (as well as ends).
I could work around this by parsing the string without using the
unpack() function, but unpack() is so useful I'd rather not work
around it.
The Devel::Peek module looks promising, but its Dump() method seems
to output by printing to STDOUT. So if I wanted to use that approach,
I'd have to capture the text sent to STDOUT and parse through that in
order to examine what I have. Unless, of course, there's a
Devel::Peek function I don't know about yet that returns me the
information I need in a nice structure.
-- Jean-Luc
------------------------------
Date: Wed, 09 Dec 2009 16:55:11 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <slrnhi0agb.765.tadmc@tadbox.sbcglobal.net>
sln@netherlands.com <sln@netherlands.com> wrote:
> Seems the truth table of Perl is:
None of the operations performed below is performed as written below.
perl does automatic conversion of string<=>number as required by
the operator it is used with.
> 65 == '65' true
A numeric operator, so the string is converted to a number:
65 == 65 true
> 65 eq '65' true
A string operator, so the number is converted to a string:
'65' eq '65' true
> 65 eq 'A' false
'65' eq 'A' false
> 65 == 'A' error, 'A' is not a digit
65 == 0 false
It is not an error. 'A' is converted to zero (and a *warning* is generated).
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Wed, 09 Dec 2009 18:14:14 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <hfpav7$do5$1@aioe.org>
jl_post@hotmail.com wrote:
> On Dec 9, 1:57 pm, s...@netherlands.com wrote:
>> This :
>> @data = unpack('c x a2', $string);
>> is a known data format, isn't it?
>>
>> When do you not know what something is?
>
>
> That's a good question. The example I gave is overly simplistic;
> in reality the binary strings/files I'm parsing through can have
> literally hundreds (if not thousands) of entries, and keeping track of
> whether they are derived from strings or numeric values can get a bit
> hairy.
>
> Not only that, but if I use a format string like:
>
> 'I/i I/(a10) I/i'
>
> the number of elements I extract is not fixed. Therefore, it's
> difficult to know when the first set of numerical values ends and the
> set of not-necessarily-numerical values begins (as well as ends).
>
> I could work around this by parsing the string without using the
> unpack() function, but unpack() is so useful I'd rather not work
> around it.
>
> The Devel::Peek module looks promising, but its Dump() method seems
> to output by printing to STDOUT. So if I wanted to use that approach,
> I'd have to capture the text sent to STDOUT and parse through that in
> order to examine what I have. Unless, of course, there's a
> Devel::Peek function I don't know about yet that returns me the
> information I need in a nice structure.
Devel::Peek prints to stderr, and can be circumvented by open()ing the
file handle to a variable. perhaps local() on *STDERR, and then call
Dump() and process the output.
--
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
------------------------------
Date: Wed, 9 Dec 2009 23:14:12 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <4jn5v6-drr1.ln1@osiris.mauzo.dyndns.org>
Quoth "jl_post@hotmail.com" <jl_post@hotmail.com>:
>
> The Devel::Peek module looks promising, but its Dump() method seems
> to output by printing to STDOUT. So if I wanted to use that approach,
> I'd have to capture the text sent to STDOUT and parse through that in
> order to examine what I have. Unless, of course, there's a
> Devel::Peek function I don't know about yet that returns me the
> information I need in a nice structure.
B::svref_2object, though you will need a good understanding of perl guts
to make use of the returned objects.
Ben
------------------------------
Date: Wed, 09 Dec 2009 18:18:25 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <hfpb71$do5$2@aioe.org>
Tad McClellan wrote:
> sln@netherlands.com <sln@netherlands.com> wrote:
>
>> Seems the truth table of Perl is:
>
>
> None of the operations performed below is performed as written below.
>
> perl does automatic conversion of string<=>number as required by
> the operator it is used with.
>
>> 65 == '65' true
>
> A numeric operator, so the string is converted to a number:
>
> 65 == 65 true
>
>
>> 65 eq '65' true
>
> A string operator, so the number is converted to a string:
>
> '65' eq '65' true
>
>
>> 65 eq 'A' false
>
> '65' eq 'A' false
>
>
>> 65 == 'A' error, 'A' is not a digit
>
> 65 == 0 false
>
> It is not an error. 'A' is converted to zero (and a *warning* is generated).
use warnings qw(FATAL all);
makes it an error. it's generally good practice to treat warnings as
errors anyway.
--
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
------------------------------
Date: Wed, 09 Dec 2009 16:13:20 -0800
From: sln@netherlands.com
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <j6f0i5ljmm6bjiuhmn8rjih71edorsng0m@4ax.com>
On Wed, 09 Dec 2009 16:55:11 -0600, Tad McClellan <tadmc@seesig.invalid> wrote:
>sln@netherlands.com <sln@netherlands.com> wrote:
>
>> Seems the truth table of Perl is:
>
>
>None of the operations performed below is performed as written below.
>
>perl does automatic conversion of string<=>number as required by
>the operator it is used with.
>
>> 65 == '65' true
>
>A numeric operator, so the string is converted to a number:
>
> 65 == 65 true
>
Got it!
-sln
------------------------------
Date: Wed, 9 Dec 2009 16:20:18 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <3c73e234-42a0-47db-891a-e2e5ca3ec1dd@z35g2000prh.googlegroups.com>
> Quoth "jl_p...@hotmail.com" <jl_p...@hotmail.com>:
> > Unless, of course, there's a Devel::Peek function
> > I don't know about yet that returns me the
> > information I need in a nice structure.
On Dec 9, 4:14 pm, Ben Morrow <b...@morrow.me.uk> replied:
> B::svref_2object, though you will need a
> good understanding of perl guts
> to make use of the returned objects.
Excellent!
Thanks to you, I wrote this code which (I think) does what I want:
#!/usr/bin/perl
use strict;
use warnings;
foreach ("7777", 7777, 'foobar', 123_456)
{
use B;
if (ref(B::svref_2object(\$_)) eq 'B::PV') {
print "Is a string.\n";
} else {
print "Is not a string.\n";
}
}
__END__
which outputs:
Is a string.
Is not a string.
Is a string.
Is not a string.
This does exactly what I want, provided that I don't use
mathematical operators on a variable before I check it. (That is, if
I do "if ($var > 0) { ... }" then it is not necessarily a reference to
B::PV anymore.)
Thanks again, Ben!
-- Jean-Luc
------------------------------
Date: Wed, 09 Dec 2009 16:29:03 -0800
From: sln@netherlands.com
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <a4g0i5dn2p05gjluc1mpvhl1n3aumf2vkt@4ax.com>
On Wed, 9 Dec 2009 16:20:18 -0800 (PST), "jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
>> Quoth "jl_p...@hotmail.com" <jl_p...@hotmail.com>:
>> > Unless, of course, there's a Devel::Peek function
>> > I don't know about yet that returns me the
>> > information I need in a nice structure.
>
>On Dec 9, 4:14 pm, Ben Morrow <b...@morrow.me.uk> replied:
>> B::svref_2object, though you will need a
>> good understanding of perl guts
>> to make use of the returned objects.
>
[snip]
> if (ref(B::svref_2object(\$_)) eq 'B::PV') {
> print "Is a string.\n";
> } else {
> print "Is not a string.\n";
> }
Problem solved!
-sln
------------------------------
Date: Thu, 10 Dec 2009 00:52:22 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <6bt5v6-bms1.ln1@osiris.mauzo.dyndns.org>
Quoth "jl_post@hotmail.com" <jl_post@hotmail.com>:
> > Quoth "jl_p...@hotmail.com" <jl_p...@hotmail.com>:
> > > Unless, of course, there's a Devel::Peek function
> > > I don't know about yet that returns me the
> > > information I need in a nice structure.
>
> On Dec 9, 4:14 pm, Ben Morrow <b...@morrow.me.uk> replied:
> > B::svref_2object, though you will need a
> > good understanding of perl guts
> > to make use of the returned objects.
>
>
> Excellent!
>
> Thanks to you, I wrote this code which (I think) does what I want:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> foreach ("7777", 7777, 'foobar', 123_456)
> {
> use B;
> if (ref(B::svref_2object(\$_)) eq 'B::PV') {
Don't ever check for class membership with 'ref'. In fact, most uses of
'ref' except as a boolean are bugs. The correct way to check for class
membership is to call the ->isa method; if you aren't sure something is
an object, you can use Scalar::Util::blessed or wrap it in an eval {}.
if (B::svref_2object(\$_)->isa("B::PV")) {
However, even this isn't safe. Try this:
for ("1", 1) {
my $x = $_;
say B::svref_2object \$x;
}
Since perl reuses lexicals where possible, on the second iteration $x is
a PVIV, and thus will pass ->isa("B::PV"). (Going back to checking
'ref', or checking 'Scalar::Util::blessed', won't help, as the list
could have been (1, "1").) What you actually want is the (apparently
undocumented) ->POK method, which tells you if the scalar has a
currently-valid string representation. (Generally speaking, if there is
a string representation it is canonical.)
if (B::svref_2object(\$_)->POK) {
See, I told you you needed to know about perl's guts :).
You should be aware that trying to determine if a scalar is a string or
a number (other than by using looks_like_number) is a highly dubious
proposition. Perl just isn't set up to consistently record that
information. If this is just for display, and if you treat the PV form
as canonical where it exists, you should be OK; but don't get the idea
this is something you should be doing in general.
Ben
------------------------------
Date: Wed, 09 Dec 2009 20:26:54 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <87zl5rzkip.fsf@quad.sysarch.com>
>>>>> "jpc" == jl post@hotmail com <jl_post@hotmail.com> writes:
jpc> Yeah, I was thinking of checking Data::Dumper's output for "'" as
jpc> well. However, to do that we really should be checking to see if our
jpc> scalar is not a reference. So we could write a function like this:
jpc> sub isString
jpc> {
jpc> return 0 if ref($_[0]);
jpc> use Data::Dumper;
jpc> return 1 if Dumper($_[0]) =~ m/'/;
jpc> return 0;
jpc> }
see the other post as to why that will not work. a scalar value can be
both an integer and string at the same time. so dumper could report one
or the other based on how it looks at things.
jpc> overkill for what I'm trying to do. I just want to know if there's a
jpc> simpler way that I'm overlooking.
i smell an xy problem. if you are breaking up strings with unpack but
don't know which fields are number (in text) or strings, just use a
regex on them or to extract them. you need to show some examples of data
and code and what you expect to see. unpack is not how you check for an
integer or string.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 09 Dec 2009 18:22:12 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: parsing query strings
Message-Id: <hfpbe4$do5$3@aioe.org>
yes, it would generally be better to just let CGI do it; but is there a
way to let CGI parse arbitrary strings and stuff them into the param()
hash, or will I have to write a wrapper?
--
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
------------------------------
Date: Thu, 10 Dec 2009 00:31:45 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: parsing query strings
Message-Id: <h4s5v6-vcs1.ln1@osiris.mauzo.dyndns.org>
Quoth sreservoir <sreservoir@gmail.com>:
> yes, it would generally be better to just let CGI do it; but is there a
> way to let CGI parse arbitrary strings and stuff them into the param()
> hash, or will I have to write a wrapper?
You need to explain a bit more about what you are trying to do. Are you
trying to parse a query-string from somewhere other than the CGI
environment? If so you may be find the URI and URI::QueryParam modules
helpful.
Ben
------------------------------
Date: Wed, 09 Dec 2009 20:01:33 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: parsing query strings
Message-Id: <hfph8e$mlo$1@aioe.org>
Ben Morrow wrote:
> Quoth sreservoir <sreservoir@gmail.com>:
>> yes, it would generally be better to just let CGI do it; but is there a
>> way to let CGI parse arbitrary strings and stuff them into the param()
>> hash, or will I have to write a wrapper?
>
> You need to explain a bit more about what you are trying to do. Are you
> trying to parse a query-string from somewhere other than the CGI
> environment? If so you may be find the URI and URI::QueryParam modules
> helpful.
say you have a string "a=b&b=a" and a cgi object $q.
parse the string to let $q->param("a") eq 'b' && $q->param("b") eq 'a'.
er, the opposite of URI::QueryParam.
--
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
------------------------------
Date: Wed, 09 Dec 2009 19:55:17 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: parsing query strings
Message-Id: <87skbjvbi2.fsf@castleamber.com>
sreservoir <sreservoir@gmail.com> writes:
> say you have a string "a=b&b=a" and a cgi object $q.
>
> parse the string to let $q->param("a") eq 'b' && $q->param("b") eq 'a'.
>
> er, the opposite of URI::QueryParam.
The hash method in URI::Query and use $cgi->param() to set the values?
(I never like the use of $q)
--
John Bokma
Read my blog: http://johnbokma.com/
Hire me (Perl/Python): http://castleamber.com/
------------------------------
Date: Thu, 10 Dec 2009 01:59:32 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: parsing query strings
Message-Id: <4916v6-2rs1.ln1@osiris.mauzo.dyndns.org>
Quoth sreservoir <sreservoir@gmail.com>:
> Ben Morrow wrote:
> > Quoth sreservoir <sreservoir@gmail.com>:
> >> yes, it would generally be better to just let CGI do it; but is there a
> >> way to let CGI parse arbitrary strings and stuff them into the param()
> >> hash, or will I have to write a wrapper?
> >
> > You need to explain a bit more about what you are trying to do. Are you
> > trying to parse a query-string from somewhere other than the CGI
> > environment? If so you may be find the URI and URI::QueryParam modules
> > helpful.
>
> say you have a string "a=b&b=a" and a cgi object $q.
>
> parse the string to let $q->param("a") eq 'b' && $q->param("b") eq 'a'.
>
> er, the opposite of URI::QueryParam.
You can do something like
use List::MoreUtils qw/natatime/;
use URI;
my $U = URI->new;
$U->query("a=b&b=a");
my $next = natatime 2, $U->query_form;
while (my ($k, $v) = $next->()) {
$q->param(-name => $k, -value => $v);
}
though I don't know how well it handles ;-separated query strings.
Ben
------------------------------
Date: Wed, 09 Dec 2009 16:50:53 -0500
From: rabbits77 <rabbits77@my-deja.com>
Subject: Re: Perl to Java bytecode
Message-Id: <hfp62l$7h1$1@aioe.org>
ccc31807 wrote:
> Searching the archives, I found old posts, but nothing recent. Is
> there anything going on with Perl to match Scala or Clojure or Jython,
> i.e., a compilation targeted to the JVM?
There is jperl
http://www.javainc.com/projects/jperl/
I took a brief look at this in the past.
There is no source distribution so you can't
look under the hood too closely(without
decompilation anyway *cough*).
Maybe it will get you started on what you
are looking to do. One note of caution though
is that it appears jperl has been dormant for
7 or 8 years...
------------------------------
Date: Wed, 9 Dec 2009 15:41:52 -0800 (PST)
From: Robin <robin1@cnsp.com>
Subject: tj win pos
Message-Id: <0483c828-3631-4801-9a8b-beabeaee3da3@m33g2000pri.googlegroups.com>
How can I position a top level or mainwindow widget in tk ? I want to
make a window apprear in in perl tk in the far right hand side of the
screen...
-Robin
------------------------------
Date: Thu, 10 Dec 2009 00:35:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: tj win pos
Message-Id: <8bs5v6-vcs1.ln1@osiris.mauzo.dyndns.org>
Quoth Robin <robin1@cnsp.com>:
> How can I position a top level or mainwindow widget in tk ? I want to
> make a window apprear in in perl tk in the far right hand side of the
> screen...
See the ->geometry method in Tk::Wm (a Tk::MainWindow isa Tk::Wm).
Ben
------------------------------
Date: Wed, 9 Dec 2009 21:03:54 +0000
From: Justin C <justin.0912@purestblue.com>
Subject: Re: Unblessed reference.
Message-Id: <quf5v6-tbg.ln1@purestblue.com>
In article <slrnhhvmiu.69q.tadmc@tadbox.sbcglobal.net>, Tad McClellan wrote:
> Justin C <justin.0911@purestblue.com> wrote:
>> On 2009-12-08, Ben Morrow <ben@morrow.me.uk> wrote:
>>> Quoth Justin C <justin.0911@purestblue.com>:
>
>
>>>> my ($worksheet, $format) = create_excel_file();
>>>>
>>>> artist_chart() ;
>>>
>>> Don't pass parameters to subs through globals. It's very confusing.
>>>
>>> artist_chart($worksheet, $format);
>>
>> OK, sounds sensible - I really should re-read perlstyle.
>
>
> Though rereading perlstyle is certainly a good idea, it does not
> address the issue that Ben pointed out.
>
> Probably because passing arguments rather than communicating with
> subroutines via global variables is not an element of Perl style,
> it is an element of any-programming-language style.
>
> That is, it is fundamental Computer Science.
Unfortunately that wasn't part of my education, all my programming was
learnt post academia, under my own steam. If I'm lacking some of these
fundamentals it's because the sources I leaned from didn't cover them
adequately (or I haven't got there yet). I'm working on it, alright
already!!! :)
I did take 'computer studies' at school, I remember lots of punched
cards, BASIC, 2 Commodore PETs (yes, two!) each with a cassette for
storage, and very little else. No passing of arguments was ever covered
in the curriculum, most of us had never seen a computer before much less
used one. Fundamental for us was a *lot* more fundamental - like "what
is a computer?". Until perl the only way I knew how to call a sub was
with GOSUB (Sinclair Spectrum anyone?) - I probably didn't use those
correctly either.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Wed, 09 Dec 2009 16:11:04 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Unblessed reference.
Message-Id: <slrnhi07tk.765.tadmc@tadbox.sbcglobal.net>
Justin C <justin.0912@purestblue.com> wrote:
> (Sinclair Spectrum anyone?)
<me> raises hand
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 09 Dec 2009 16:40:03 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Unblessed reference.
Message-Id: <slrnhi09jv.765.tadmc@tadbox.sbcglobal.net>
Justin C <justin.0912@purestblue.com> wrote:
> In article <slrnhhvmiu.69q.tadmc@tadbox.sbcglobal.net>, Tad McClellan wrote:
[ snip passing args instead of using globals ]
>> That is, it is fundamental Computer Science.
>
> Unfortunately that wasn't part of my education, all my programming was
> learnt post academia, under my own steam. If I'm lacking some of these
> fundamentals it's because the sources I leaned from didn't cover them
> adequately (or I haven't got there yet).
You might benefit from:
http://en.wikipedia.org/wiki/Global_variable
http://en.wikipedia.org/wiki/Action_at_distance_%28computer_science%29
> I did take 'computer studies' at school, I remember lots of punched
> cards,
[snip other nostalgia]
I was light on fundamentals too, as my undergrad was in Electrical Engineering
rather than Computer Science or Software Engineering.
After I switched from "hardware" to "software" work, I eventually
noticed that my projects would get to 80-90% complete, and then
stall in insurmountable cross-dependencies, or bad design decisions
made too long ago to undo.
I was fortunate enough to be able to go back to school and pick up
my missing fundamentals.
I'll share with you the most important thing I learned in slogging
through seven years (part time) of grad school:
Think before you code!
A quick google search leads me to recommend
http://www.codewalkers.com/c/a/Programming-Basics/Coding-Best-Practicesor-at-least-Better-Practices/
since they get to that right near the beginning.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 9 Dec 2009 23:10:17 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Unblessed reference.
Message-Id: <pbn5v6-drr1.ln1@osiris.mauzo.dyndns.org>
Quoth Justin C <justin.0911@purestblue.com>:
> On 2009-12-08, Ben Morrow <ben@morrow.me.uk> wrote:
> >
> > Quoth Justin C <justin.0911@purestblue.com>:
> >>
> >> I'm using Spreadsheet::WriteExcel, and (I think) I've created a
> >> reference to the worksheet. When I try to use this elsewhere I get:
> >> Can't call method ... on unblessed reference at ...
> >>
> >> This is the minumum I've been able to cut the code down to (sorry it's
> >> not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
> >> documentation I've created the object, and it's a reference to that
> >> object that I've passed from a subroutine to Main:: to pass on to other
> >> subroutines.
> >
> > An object is already a reference. You don't need to take another layer
> > of reference, just return the object.
>
> I think I'm getting the hang. I've been using references for ages,
> hashrefs, arrayrefs, but whenever I use objects I get a bit confused.
> It's coming together slowly now that I'd started working through the OO
> documentation.
Maybe you need to read perlboot. In simple terms, an object is just a
refrence with a bit of magic attached. Here is how you make an object:
my $obj = { a => 1 }; # an ordinary hashref
bless $obj, "My::Class"; # now it's an object
You can still deref this object with $obj->{a}, just like any other
hashref, but it's considered bad style to do so from outside the class
that defined it. (The author of the class is entitled to change the
internals in the next release, without warning you.)
However, you can also call a method on it, with $obj->method(...). This
is where 'My::Class' comes in: that is the package Perl will search
first for the method.
If you think of an object as much like an arrayref or a hashref you
won't go too far wrong.
Ben
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 2712
***************************************