[31862] in Perl-Users-Digest
Perl-Users Digest, Issue: 3125 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 9 00:09:25 2010
Date: Wed, 8 Sep 2010 21:09:05 -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 Wed, 8 Sep 2010 Volume: 11 Number: 3125
Today's topics:
Re: convert byte stream to integers <toralf.foerster@gmx.de>
Re: convert byte stream to integers <toralf.foerster@gmx.de>
Re: convert byte stream to integers <uri@StemSystems.com>
Re: convert byte stream to integers <derykus@gmail.com>
Re: Ideal data structure for nested list format? <rvtol+usenet@xs4all.nl>
Re: Ideal data structure for nested list format? sln@netherlands.com
invisible objects <monkey@joemoney.net>
Re: invisible objects <peter@makholm.net>
Re: invisible objects <monkey@joemoney.net>
Re: invisible objects (Randal L. Schwartz)
Re: invisible objects <jimsgibson@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 08 Sep 2010 10:08:56 +0200
From: Toralf =?UTF-8?B?RsO2cnN0ZXI=?= <toralf.foerster@gmx.de>
Subject: Re: convert byte stream to integers
Message-Id: <i67gaq$h3e$1@news.eternal-september.org>
Ben Morrow wrote:
>...
> Ben
Thx Ben for your help, now I got it.
--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
------------------------------
Date: Wed, 08 Sep 2010 18:44:58 +0200
From: Toralf =?UTF-8?B?RsO2cnN0ZXI=?= <toralf.foerster@gmx.de>
Subject: Re: convert byte stream to integers
Message-Id: <i68eia$ptj$1@news.eternal-september.org>
Steve C wrote:
> The unpack might seem non-obvious. It takes a string and unpacks it as a
> perl value. The reason this step is separate from the read is that perl
> variables have to know what type of data they contain, since they can hold
> different types.
Yep thx, I thought it could be done in one step.
This is now sufficient for me :
perl -we 'open (FILE, "./4byte"); while (read (FILE, my $s, 16)) { my @N =
unpack "I*", $s; print join ("\t", @N), "\n"; } close (FILE)'
--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
------------------------------
Date: Wed, 08 Sep 2010 14:12:54 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: convert byte stream to integers
Message-Id: <87iq2gm7dl.fsf@quad.sysarch.com>
>>>>> "TF" == Toralf Förster <toralf.foerster@gmx.de> writes:
TF> Steve C wrote:
>> The unpack might seem non-obvious. It takes a string and unpacks it as a
>> perl value. The reason this step is separate from the read is that perl
>> variables have to know what type of data they contain, since they can hold
>> different types.
TF> Yep thx, I thought it could be done in one step.
it can be. you need to learn perl one liner tricks.
TF> This is now sufficient for me :
TF> perl -we 'open (FILE, "./4byte"); while (read (FILE, my $s, 16)) { my @N =
TF> unpack "I*", $s; print join ("\t", @N), "\n"; } close (FILE)'
with perl -0777n you can read the whole file into $_. then split to the
size you want unpack and print. much simpler than your code. there is
also no need for the close or the temp vars you have.
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, 8 Sep 2010 19:48:41 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: convert byte stream to integers
Message-Id: <3fa3f278-4071-4dcd-ae33-9f157649aa2d@q21g2000prm.googlegroups.com>
On Sep 8, 9:44=A0am, Toralf F=F6rster <toralf.foers...@gmx.de> wrote:
> Steve C wrote:
> > The unpack might seem non-obvious. =A0It takes a string and unpacks it =
as a
> > perl value. The reason this step is separate from the read is that perl
> > variables have to know what type of data they contain, since they can h=
old
> > different types.
>
> Yep thx, I thought it could be done in one step.
>
> This is now sufficient for me :
>
> perl -we 'open (FILE, "./4byte"); while (read (FILE, my $s, 16)) { my @N =
=3D
> unpack "I*", $s; print join ("\t", @N), "\n"; } close (FILE)'
>
That can be simplified a bit without getting golf-ish:
perl -we 'open(F,"./4byte"); $,=3D"\t"; print unpack "I*",$s while read
F,$s,16'
--
Charles DeRykus
------------------------------
Date: Wed, 08 Sep 2010 08:32:22 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Ideal data structure for nested list format?
Message-Id: <4c872df7$0$22913$e4fe514c@news.xs4all.nl>
On 2010-09-08 01:32, Ben Morrow wrote:
> my $filedata = do {
> open my $FH, "<", "...";
> local $/;
> <$FH>;
> };
Alternative:
my $filedata;
{ open my $fh, "<", "..." or die $!;
local $/;
$filedata = <$fh>;
}
which uses less memory.
--
Ruud
------------------------------
Date: Wed, 08 Sep 2010 00:46:24 -0700
From: sln@netherlands.com
Subject: Re: Ideal data structure for nested list format?
Message-Id: <hcfe86htdjkcf5rbvb2sj9112hsvfnsl7b@4ax.com>
On Sat, 4 Sep 2010 20:40:56 +0200, Tuxedo <tuxedo@mailinator.com> wrote:
>In trying to construct a nested html list with perl I have some questions
>what the ideal data structure may be for the specific purpose.
>
>The menu is generated on the fly for each page request. The script lives in
>a perl file named menu.pl and the current page that is accessed is known to
>the script via a server environment variable accessible in a $current_page
>perl variable. The html parts are imported onto individual html pages
>through SSI (includes) and all pages are placed at a root level, so there
>are no sub-directories or chances of any pages having a same name.
>
>This is a simplified html output of the menu in an unordered list format:
>
><ul>
> <li><a href=1.1.html>Level 1.1</a>
> <ul>
> <li><a href=2.1.html>Level 2.1</a></li>
> <li><a href=2.2.html>Level 2.2</a>
> <ul>
> <li><a href=3.1.html>Level 3.1</a></li>
> <li><a href=3.2.html>Level 3.2</a></li>
> </ul>
> </li>
> <li><a href=2.3.html>Level 2.3</a></li>
> </ul>
> </li>
></ul>
>
>So there are three levels and the first two levels have nested lists
>within. If for example page 3.2.html is accessed, the script should write
>out that <li> entry *without* the enclosing <a href=3.2.html>..</a> parts,
>as well as add a css-class to that one list item: <li class=current_page>.
>
>The script should know its nearest parent <li> item, so in case of
>accessing page 3.2.html, that would be the <li> with the 2.2.html link.
>This entry should then have have the css-class: <li class=parent_level_2>.
>
>Additionally, the second nearest parent should be given another css-class,
>such as <li class=parent_level_1> (so it may be styled differently).
>
>If the current page is one that does not exist in the menu, although the
>menu has been imported on such a page, then no special classes or non-link
>formatting is needed for any of the array entries. The html output would
>just appear exactly as the above example.
>
>Without the actual link names, I guess the list may be constructed with a
>LoL or an AoA, such as:
>
>my @AoA = ('1.1.html',
> ['2.1.html',
> '2.2.html',
> ['3.1.html',
> '3.2.html'],
> '2.3.html']);
>
>If so, a parallel AoA's would be needed for the link names to be combined
>in a final loop. Or is some other data structure better suited for the
>purpose? I.e.: sticking all items together in a nested html list in order
>of entry in the perl code, as well as figure out the opening parent <li>
>points (if any) in order to apply custom css-classes to relevant <li>'s.
>
>Looking at perldsc, in addition to AoA, alternatives such as HoA, AoH, HoH
>as well as some more elaborate data structures exist. I'm not sure what's
>best? The maintenance of the link list will involve changing or updating
>entries only very occasionally, so it can be done in separate arrays in
>case that's better. Anyway, it's not important how, as long as it works...
>
>What is the ideal data structure for the particular hierarchial list
>functionality and where the described level specific output can easily be
>incorporated?
>
>Many thanks for any advise or general pointers.
>
>Tuxedo
The rabit hole goes very deep.
-sln
---------------
use strict;
use warnings;
##
my $template1 = <<LIST1;
[
('1.1.html',Level 1.1
[
('2.1.html',Level 2.1)
('2.2.html',Level 2.2)
[
('3.1.html',Level 3.1)
('3.2.html',Level 3.2)
]
('2.3.html',Level 2.3)
]
)
]
LIST1
##
my $regex = qr/
( #1
\(
( #2
(?:
(?> [^()]+ )
| (?1)
)*
)
\)
)
|
( #3
\[
( #4
(?:
(?> [^\[\]]+ )
| (?3)
)*
)
\]
)
|
'\s* ([^']*)\s*'\s*, ([^<\n]*) #5,6
/xs;
##
my $html = $template1;
1 while ($html =~ s/$regex/
defined $1 ? "<li>$2<\/li>" :
defined $3 ? "<ul>$4<\/ul>" :
"<a href=\"$5\">$6<\/a>"/eg);
print "template ->\n$template1\n";
print "html ->\n$html\n";
__END__
-------------------------
Output:
template ->
[
('1.1.html',Level 1.1
[
('2.1.html',Level 2.1)
('2.2.html',Level 2.2)
[
('3.1.html',Level 3.1)
('3.2.html',Level 3.2)
]
('2.3.html',Level 2.3)
]
)
]
html ->
<ul>
<li><a href="1.1.html">Level 1.1</a>
<ul>
<li><a href="2.1.html">Level 2.1</a></li>
<li><a href="2.2.html">Level 2.2</a></li>
<ul>
<li><a href="3.1.html">Level 3.1</a></li>
<li><a href="3.2.html">Level 3.2</a></li>
</ul>
<li><a href="2.3.html">Level 2.3</a></li>
</ul>
</li>
</ul>
------------------------------
Date: Wed, 08 Sep 2010 15:42:08 -0400
From: monkeys paw <monkey@joemoney.net>
Subject: invisible objects
Message-Id: <46idnRRxmLaNehrRnZ2dnUVZ_qOdnZ2d@insightbb.com>
I'm writing an app that obtains info and then
places that info in an array of hashrefs like so:
package Something;
sub new {
my ($class, %opts) = @_;
my $self = \%opts;
bless $self, $class;
return $self;
}
sub execute {
my @return_set;
push @return_set, {id => 'ca345', id_type => 'solid'};
push @return_set, {id => 'ca346', id_type => 'liquid'};
return @return_set;
}
So to use it you have:
use Something;
my $s = Something->new();
my @array = $s->execute();
What i need to do is have each element of @array be
an object that i can call methods on like:
for (@array) {
if ($_->is_solid()) {
print 'yes';
} else {
print 'no';
}
}
I can't seem to get this figured out, i tried the code below
but hit a brick wall, any suggestions?
Here is the Module:
package Sauron::Item;
use strict;
our $AUTOLOAD;
sub new {
my ($class, %opts) = @_;
my $self = \%opts;
bless $self, $class;
return $self;
}
sub set_values {
my ($self, $values) = @_;
foreach (keys %$values) {
$self->{values}->{$_} = $values->{$_};
}
}
sub get_values {
my ($self) = @_;
return $self->{values};
}
sub AUTOLOAD {
my($self) = @_;
(my $func_name = $AUTOLOAD) =~ s/.+://;
if ($func_name =~ /is_(.+)/) {
if ($self->{values}->{id_type} eq $1) {
return 1;
} else {
return 0;
}
} else {
die 'Unknown function: ' . $func_name;
}
}
return 'biscuits and gravy';
========================
Here is the invoking script:
use strict;
use Sauron::Item;
my $item = Sauron::Item->new();
$item->set_values({id_type => 'bill', id => 'CAS1'});
if ($item->is_reg()) {
print 'yes';
} else {
print 'no';
}
my $values = $item->get_values();
use Data::Dumper;die 'DDDEBUG' . Dumper($values);
------------------------------
Date: Wed, 08 Sep 2010 21:49:58 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: invisible objects
Message-Id: <87bp88817d.fsf@vps1.hacking.dk>
monkeys paw <monkey@joemoney.net> writes:
> I'm writing an app that obtains info and then
> places that info in an array of hashrefs like so:
Have you read 'perldoc perlobj'?
Read it and make sure that you understands the difference between
references and objects.
>
>
> package Something;
>
> sub new {
> my ($class, %opts) = @_;
> my $self = \%opts;
> bless $self, $class;
This is what makes $self an object and not just the hasref it was
before.
> return $self;
> }
>
> sub execute {
> my @return_set;
> push @return_set, {id => 'ca345', id_type => 'solid'};
> push @return_set, {id => 'ca346', id_type => 'liquid'};
Here the content of the array are just hashrefs. You need to make them
as objects of some class.
> return @return_set;
> }
>
//Makholm
------------------------------
Date: Wed, 08 Sep 2010 16:03:40 -0400
From: monkeys paw <monkey@joemoney.net>
Subject: Re: invisible objects
Message-Id: <xJKdncYeIc2BcRrRnZ2dnUVZ_jKdnZ2d@insightbb.com>
On 9/8/2010 3:49 PM, Peter Makholm wrote:
> monkeys paw<monkey@joemoney.net> writes:
>
>> I'm writing an app that obtains info and then
>> places that info in an array of hashrefs like so:
>
> Have you read 'perldoc perlobj'?
>
> Read it and make sure that you understands the difference between
> references and objects.
>
>>
>>
>> package Something;
>>
>> sub new {
>> my ($class, %opts) = @_;
>> my $self = \%opts;
>> bless $self, $class;
>
> This is what makes $self an object and not just the hasref it was
> before.
>
>> return $self;
>> }
>>
>> sub execute {
>> my @return_set;
>> push @return_set, {id => 'ca345', id_type => 'solid'};
>> push @return_set, {id => 'ca346', id_type => 'liquid'};
>
> Here the content of the array are just hashrefs. You need to make them
> as objects of some class.
even if i do
use Sauron::Item;
my $item = Sauron::Item->new()
$item->set_values({id => 'cas1'});
push @return_set, $item;
Still the calling program cannot do this:
my @array = Sauron->execute();
for (@array) {
if ($_->is_solid()) { # This fails as unblessed
print 'yes';
}
}
I need the items in @array to have callable methods.
>
>> return @return_set;
>> }
>>
>
> //Makholm
------------------------------
Date: Wed, 08 Sep 2010 13:37:03 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: invisible objects
Message-Id: <8662ygt1jk.fsf@red.stonehenge.com>
>>>>> "monkeys" == monkeys paw <monkey@joemoney.net> writes:
monkeys> I need the items in @array to have callable methods.
Then they need to be objects.
Maybe you want something like:
sub execute {
my $class = shift;
return map { $class->new(%$_) }
{ id => 1, foo => 2 }, { id => 3, foo => 4 };
}
print "Just another Perl hacker,"; # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: Wed, 08 Sep 2010 15:27:15 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: invisible objects
Message-Id: <080920101527158756%jimsgibson@gmail.com>
In article <46idnRRxmLaNehrRnZ2dnUVZ_qOdnZ2d@insightbb.com>, monkeys
paw <monkey@joemoney.net> wrote:
> I'm writing an app that obtains info and then
> places that info in an array of hashrefs like so:
Here is an example that doesn't use autoload:
#!/usr/local/bin/perl
use strict;
use warnings;
package Item;
sub new {
my ($class, %opts) = @_;
my $self = \%opts;
bless $self, $class;
return $self;
}
sub get_attribute
{
my( $self, $key ) = @_;
return $self->{$key};
}
package ItemList;
sub new {
my ($class, %opts) = @_;
my $self = \%opts;
bless $self, $class;
return $self;
}
sub get_list {
my @return_set;
push @return_set, Item->new( id => 'ca345', id_type => 'solid');
push @return_set, Item->new( id => 'ca346', id_type => 'liquid');
return @return_set;
}
package main;
my $list_object = ItemList->new();
my @list = $list_object->get_list();
for my $obj ( @list ) {
print "object ", $obj->get_attribute('id'), " has type ",
$obj->get_attribute('id_type'), "\n";
}
__OUTPUT__
object ca345 has type solid
object ca346 has type liquid
--
Jim Gibson
------------------------------
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 3125
***************************************