[31461] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2713 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 10 14:09:40 2009

Date: Thu, 10 Dec 2009 11:09:07 -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           Thu, 10 Dec 2009     Volume: 11 Number: 2713

Today's topics:
    Re: Any way to tell if a scalar is a string? <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: Any way to tell if a scalar is a string? <rburbrid@cisco.com>
    Re: Any way to tell if a scalar is a string? <jl_post@hotmail.com>
        Best way to replace a set of strings in large files? <ryanchan404@gmail.com>
    Re: Best way to replace a set of strings in large files <source@netcom.com>
        foreach - sorted array the way I want? <mangoo@nospam.wpkg.org>
    Re: foreach - sorted array the way I want? sln@netherlands.com
        How big do your programs get before you modularise most <justin.0911@purestblue.com>
    Re: How big do your programs get before you modularise  <cartercc@gmail.com>
    Re: i18n - and 2-headed strings? <nospam-abuse@ilyaz.org>
    Re: parsing query strings <xhoster@gmail.com>
        The Perl Review <afmcc@btinternet.com>
    Re: The Perl Review <brian.d.foy@gmail.com>
        Trying to avoid passing params to subs through globals <justin.0911@purestblue.com>
    Re: Trying to avoid passing params to subs through glob <tadmc@seesig.invalid>
    Re: Unblessed reference. <justin.0911@purestblue.com>
    Re: Unblessed reference. <justin.0911@purestblue.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 9 Dec 2009 19:47:23 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <cj76v6xsda.ln2@goaway.wombat.san-francisco.ca.us>

On 2009-12-09, sreservoir <sreservoir@gmail.com> wrote:
> use warnings qw(FATAL all);
>
> makes it an error. it's generally good practice to treat warnings as
> errors anyway.

Since when?

--keith

-- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information



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

Date: Thu, 10 Dec 2009 10:38:03 -0500
From: Sir Robert Burbridge <rburbrid@cisco.com>
Subject: Re: Any way to tell if a scalar is a string?
Message-Id: <1260459460.714824@sj-nntpcache-3.cisco.com>

On 12/09/2009 07:52 PM, Ben Morrow wrote:

> 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 {}.

Out of curiosity, why "Don't ever check for class membership with 
'ref'"?  Do you mean, "... unless you want to exclude subclassed 
objects", or is there some more dire problem with it that I don't see...

(sincere question)

-Sir



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

Date: Thu, 10 Dec 2009 08:22:10 -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: <00df6383-f7b0-4619-8210-484885956930@j9g2000prh.googlegroups.com>

On Dec 9, 6:26=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> 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.


   That's very true.  It's just that sometimes I extract text with
unpack() that is all numbers, but represents a name or identifier,
rather than a quantity (examples are "286", "386", and "486" Intel
architectures).

   In such a case, if I extract it as a string, I want to report it as
text (even though I could technically report it as a numerical
value).  So if Data::Dumper reports it as a string, chances are I also
want to report it as a string, as well.

   True, Perl seamlessly converts strings that look like numbers to
numbers and vice-versa (which is very convenient in most cases), but
in this case I'd to know that such a value was extracted from text.

   -- Jean-Luc


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

Date: Thu, 10 Dec 2009 06:21:41 -0800 (PST)
From: Ryan Chan <ryanchan404@gmail.com>
Subject: Best way to replace a set of strings in large files?
Message-Id: <13f0b841-2abb-4206-8f40-43786a90e9e6@u1g2000pre.googlegroups.com>

Hello,

Consider the case:

You have 200 lines of mapping to replace, in a csv format, e.g.

apple,orange
boy,girl
 ...

You have a 500MB file, you want to replace all 200 lines of mapping,
what would be the most efficient way to do it?

Thanks.


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

Date: Thu, 10 Dec 2009 09:49:15 -0800
From: David Harmon <source@netcom.com>
Subject: Re: Best way to replace a set of strings in large files?
Message-Id: <k-idnfmhLqwRqbzWnZ2dnUVZ_qqdnZ2d@earthlink.com>

On Thu, 10 Dec 2009 06:21:41 -0800 (PST) in comp.lang.perl.misc, Ryan
Chan <ryanchan404@gmail.com> wrote,
>Hello,
>
>Consider the case:
>
>You have 200 lines of mapping to replace, in a csv format, e.g.
>
>apple,orange
>boy,girl
>...
>
>You have a 500MB file, you want to replace all 200 lines of mapping,
>what would be the most efficient way to do it?

Since your files are small, the best thing is to write the program in
the most straightforward and obvious way possible.  If, for example, you
can get it to work the first time, that is about twice as efficient as
if you had to fix something and try a second time.
  
 


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

Date: Thu, 10 Dec 2009 16:52:43 +0100
From: Tomasz Chmielewski <mangoo@nospam.wpkg.org>
Subject: foreach - sorted array the way I want?
Message-Id: <hfr5gb$2s9$1@online.de>

I have a script which reads the config file and builds the commands 
which need to be executed.

It pushes each command to an array.

When everything is read from the config file, it is executed like this:


foreach my $exec_command (@exec_commands) {
	execute($exec_command);
}


However, such approach does not guarantee that the commands will be 
executed in a proper order.


Supposing I would like to have the guarantee that the commands 
containing "someword" will be executed before commands containing 
"someotherword" (and this, before "xyz" command), what would be the best 
approach here?


1) build a hash, not an array in the first place? I'm afraid it would 
complicate it too much.


2)


foreach my $exec_command (@exec_commands) {
	if ($exec_command=~m { .... }) {
		push(@execute_first, $exec_command);
	}

	if ($exec_command=~m { .... }) {
		push(@execute_second, $exec_command);
	}

}

And then, simply execute commands from @execute_first, @execute_second, 
and so on?


3) something else?


-- 
Tomasz Chmielewski
http://wpkg.org


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

Date: Thu, 10 Dec 2009 10:24:30 -0800
From: sln@netherlands.com
Subject: Re: foreach - sorted array the way I want?
Message-Id: <rve2i55dknbgcfpa6d0i9cjgvc191mo23r@4ax.com>

On Thu, 10 Dec 2009 16:52:43 +0100, Tomasz Chmielewski <mangoo@nospam.wpkg.org> wrote:

>I have a script which reads the config file and builds the commands 
>which need to be executed.
>
>It pushes each command to an array.
>
>When everything is read from the config file, it is executed like this:
>
>
>foreach my $exec_command (@exec_commands) {
>	execute($exec_command);
>}
>
>
>However, such approach does not guarantee that the commands will be 
>executed in a proper order.
>
>
>Supposing I would like to have the guarantee that the commands 
>containing "someword" will be executed before commands containing 
>"someotherword" (and this, before "xyz" command), what would be the best 
>approach here?
>
>
[snip]

>And then, simply execute commands from @execute_first, @execute_second, 
>and so on?
>
>
>3) something else?

That would work but its a little too specific.
Here is another way to do the same thing (there are many).

-sln

-------------
use strict;
use warnings;

my @cmd_priority = (
  'cmd3',
  'cmd4|cmd5',
  'cmd1|cmd2',
);

my @exec_commands = 
     qw{ cmd2 cmd3 cmd5 cmd3 cmd1 cmd4 };

for my $cmd
   ( sort {
           my $res = 0;
           for (reverse @cmd_priority) {
              $res ||= ($a =~ /$_/ <=> $b =~ /$_/);
           }
          $res;
     } @exec_commands )
{
    print "$cmd\n";
#   execute($cmd);
}
__END__

Output:

cmd3
cmd3
cmd5
cmd4
cmd2
cmd1



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

Date: Thu, 10 Dec 2009 13:02:15 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: How big do your programs get before you modularise most of it?
Message-Id: <56fc.4b20f157.75f97@zem>

I keep writing programs that (for me at least) start getting large. I
know they're small, even by the standard of many of the modules that
I've seen, but how do you decide when to break down your code and
modularise?

Do you only create modules if you think the code is likely to re
re-used, and if it's not then if the program has 10k lines then so
be it?

I fine myself bashing away at the keyboard, and the next thing I know is
that I've got 1000 lines and keeping track of what's where starts
getting complicated (I do love an editor that can fold!). I was just
wondering how others program.

	Justin.

-- 
Justin C, by the sea.


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

Date: Thu, 10 Dec 2009 06:56:47 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: How big do your programs get before you modularise most of it?
Message-Id: <c036447f-1057-45dc-ac30-63bfc701913c@a32g2000yqm.googlegroups.com>

On Dec 10, 8:02=A0am, Justin C <justin.0...@purestblue.com> wrote:
> I keep writing programs that (for me at least) start getting large. I
> know they're small, even by the standard of many of the modules that
> I've seen, but how do you decide when to break down your code and
> modularise?
>
> Do you only create modules if you think the code is likely to re
> re-used, and if it's not then if the program has 10k lines then so
> be it?
>
> I fine myself bashing away at the keyboard, and the next thing I know is
> that I've got 1000 lines and keeping track of what's where starts
> getting complicated (I do love an editor that can fold!). I was just
> wondering how others program.

From my experience, this is an incremental process.

First, you write a small script sequentially. It consists mostly of a
series of statements with maybe a couple of blocks.

After a while as your script grows, you see certain patterns and
repetitions, so you move the similar statements to user defined
functions. Then, your program consists mostly of a series of calls to
user defined functions.

Later on, the number of user defined functions increases so you decide
to place functions with similar functionalities in files, which become
modules. Then, your program 'uses' your modules but still consists of
calls to functions.

The largest monolithic program I've written probably contains several
hundred LOC. By the time you hit KLOCs, you have invariably moved to a
modular program. It's actually pretty easy to move from sequential
scripts to user defined functions to user defined modules - mostly a
matter of cut and paste.

I haven't written any OO Perl, but I have written Java, and I find the
Java development style completely different. With Java, I will
decompose the requirements into classes, write the test cases for each
of the classes, and then write the classes. When the classes behave as
they ought, I'll write the main application. In essence, this turns
the development process I use with Perl on its head.

Interestingly, I've been playing with Lisp for several years, and find
myself writing Lisp a couple of hours a day now. The typical
development style in Lisp is writing and testing the functions that
you use to compose larger structures. The OO development in Lisp also
follows a similar pattern. Since Lisp developers use an interactive
development environment, this style becomes natural and the programs
'grow' more or less in an ad hoc manner (which causes neurotic
reactions in software engineers). Perl 6 supposedly has an interactive
development environment as well, and I'll bet that the Perl community
as a whole will gravitate toward this and away from the typical C
style of designing and developing a program.

CC.


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

Date: Thu, 10 Dec 2009 15:39:32 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: i18n - and 2-headed strings?
Message-Id: <slrnhi25hk.7c4.nospam-abuse@powdermilk.math.berkeley.edu>

On 2009-12-03, Ben Morrow <ben@morrow.me.uk> wrote:
> That is for module authors to use to internationalise their modules. It
> doesn't help with the messages perl generates itself.

Thinking about it more:

  A) I find the argument that Perl is a "lingua franca of science and
     technology" very misplaced.  *A Perl application* may be designed
     for use by children and/or music lovers.

  B) We need something which works as English during parsing, but is
     printed out in Bengali.  This looks like overloading.

     Unfortunately, when I designed overloading, I did not think *at
     all* about overloading objects with string semantic.  There was
     no provision to treat REx matching specially during overloading,
     treat print() specially, treat substr() specially, treat index()
     specially etc...  AFAIK, the current overloading behaves the same
     way.

     Is there code which analyses $@ any other way than doing =~?  If
     not, then making overloaded-stringify inspect whether it is
     called during REx-matching might me possible (at least with some
     modification of perl C code to make the latter condition easier
     recognizable...).

Basically, what I think of is making $@ into a 2-headed beast, with 2
different STRING values.  What do people think?

Ilya


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

Date: Wed, 09 Dec 2009 20:04:35 -0800
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: parsing query strings
Message-Id: <4b2075aa$0$7811$ed362ca5@nr5-q3a.newsreader.com>

sreservoir wrote:
> 
> say you have a string "a=b&b=a" and a cgi object $q.

You already have a cgi object $q?  Then what you are planning on doing 
with it's old contents?

> parse the string to let $q->param("a") eq 'b' && $q->param("b") eq 'a'.
> 
> er, the opposite of URI::QueryParam.
> 

 From CGI docs:

       You can also initialize the query object from
 ....
        or from a properly formatted, URL-escaped query string:

            $query = new CGI('dinosaur=barney&color=purple');



Xho


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

Date: Thu, 10 Dec 2009 12:26:24 +0000
From: TonyMc <afmcc@btinternet.com>
Subject: The Perl Review
Message-Id: <861vj3aubz.fsf@elena.home>

Hi, does anyone know what has happened to The Perl Review?  The website
has not been updated in months.  I have tried emailing them but with no
response.  I enjoyed the articles and it saddens me to think that
another Perl resource has died.

Tony


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

Date: Thu, 10 Dec 2009 12:46:43 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: The Perl Review
Message-Id: <101220091246434877%brian.d.foy@gmail.com>

In article <861vj3aubz.fsf@elena.home>, TonyMc <afmcc@btinternet.com>
wrote:

> Hi, does anyone know what has happened to The Perl Review?  The website
> has not been updated in months. 

I don't recall you emailing me.

The answer is that I got distracted writing the next edition of
Effective Perl Programming, which is now almost over.


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

Date: Thu, 10 Dec 2009 12:56:58 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Trying to avoid passing params to subs through globals
Message-Id: <52bf.4b20f01a.a4abc@zem>


Further to me recent problem of unblessed references, I'm re-writing my
code, putting more of it into subroutines so that the main thread is
more obvious. I'm finding that where I was using global variables there
appear to be a lot of variables that need passing to some sub-routines.

I'm thinking of creating a few hashes of references to these variables,
and then passing the whole hash (or a reference to it) to the
subroutine.

my %excel = (
    "worksheet" => \$ws,
    "workboot" => \$wb,
    "format_1" => \$format_1,
    "format_2" => \$format_2,
);

my %document = (
    "pdf" => \$pdf,
    "table" => \$table,
);

Firstly, if these are defined near the begining of the program they look
just like globals to me! What's the difference? OK, so I can just
declare "my %excel =();" and then have a subroutine(s) populate it on an
ad-hoc basis, that, I suppose, looks less global.

To avoid a whole bunch of vars being global I can see that I'm going to
have to nest a lot of sub-routines so that I can keep scope minimal.

The other thing is, if $ws, for example, was returned as a reference (I
think I really need to see where I should return references, and where I
should return the actual thing... if it's an object return the thing, if
it's scalar, array or hash return a ref? maybe that's too crude) I
surely shouldn't be creating a reference to it (in the hash
assignement), so it should be "worksheet" => $ws" instead of "\$ws",
shouldn't it?

Sorry if the above sounds a bit random, it's hitting the keyboard as
it's coming to mind, and I shouldn't really be doing this now, I'm
supposed to be working... though the work I'm supposed to be doing is
getting this program to do what we want - it's a little chicken and egg
here at the moment.

	Justin.

-- 
Justin C, by the sea.


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

Date: Thu, 10 Dec 2009 08:51:15 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Trying to avoid passing params to subs through globals
Message-Id: <slrnhi22gu.ci2.tadmc@tadbox.sbcglobal.net>

Justin C <justin.0911@purestblue.com> wrote:

> I'm thinking of creating a few hashes of references to these variables,
> and then passing the whole hash (or a reference to it) to the
> subroutine.
>
> my %excel = (
>     "worksheet" => \$ws,
>     "workboot" => \$wb,
>     "format_1" => \$format_1,
>     "format_2" => \$format_2,
> );


You have 5 variables to keep track of there.

Consider using a hash _instead_ of the individual variables:

    my %excel = (
        ws => function_that_returns_worksheet_object(),
        wb => function_that_returns_workbook_object(),
        ...
    );

Then you would have only one variable (the hash) to keep track of.

> The other thing is, if $ws, for example, was returned as a reference (I
> think I really need to see where I should return references, and where I
> should return the actual thing... 


If it is "small" return the actual thing, returning a reference instead
might be a good idea for performance reasons (rather than maintainablility
reasons) if the thing is "large".


> if it's an object return the thing, if
> it's scalar, array or hash return a ref? 


Scalar references are not needed very often. At this point you
should try pretending that they do not even exist.

Note that you cannot return an array or a hash "thing", you can only 
return a list or a scalar.

> surely shouldn't be creating a reference to it (in the hash
> assignement), so it should be "worksheet" => $ws" instead of "\$ws",
> shouldn't it?


yes, but then there would be no need for the $ws variable at all.

whereever you have "$ws" in your code, replace it with "$excel{worksheet}".

-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"


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

Date: Thu, 10 Dec 2009 12:24:49 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: Unblessed reference.
Message-Id: <511a.4b20e891.8dd9@zem>

On 2009-12-09, Tad McClellan <tadmc@seesig.invalid> wrote:
> 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.

Thank you. I've forwarded this to self@home where I will have time to
study the information.

	Justin.

-- 
Justin C, by the sea.


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

Date: Thu, 10 Dec 2009 12:34:45 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: Unblessed reference.
Message-Id: <51c8.4b20eae5.b3a43@zem>

On 2009-12-09, Ben Morrow <ben@morrow.me.uk> wrote:
>
> 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:

I think, now that I'm looking more at OO that perlboot and perltoot (I'm
sure there's a third too) I'll find these two more accessible, and
therfore probably useful. Will add them to my reading list (again).


>     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.

Yup, this is where you're losing me. I'll go to the documentation, I
fear it's going to take a little time reading, re-reading, and
re-re-reading to get some of this stuff. As I'm getting older I find
that practical (physicla) stuff goes in more easily, and abstract stuff 
is getting harder to understand... that's no fun when the practical
stuff is no longer easy to do!

	Justin.

-- 
Justin C, by the sea.


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

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


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